From 976a6098030dbe4347f84e258c105b0eb416121e Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Thu, 31 Aug 2023 22:48:52 -0700 Subject: [PATCH] Add functions to make lighter and darker UIColors for the map --- Meshtastic.xcodeproj/project.pbxproj | 4 ++ Meshtastic/Extensions/UIColor.swift | 41 +++++++++++++++++++ .../Views/Map/Custom/MapViewSwiftUI.swift | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Meshtastic/Extensions/UIColor.swift diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index 6850954b..02aeccf1 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -68,6 +68,7 @@ DD6193792863875F00E59241 /* SerialConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6193782863875F00E59241 /* SerialConfig.swift */; }; DD73FD1128750779000852D6 /* PositionLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD73FD1028750779000852D6 /* PositionLog.swift */; }; DD769E0328D18BF1001A3F05 /* DeviceMetricsLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD769E0228D18BF0001A3F05 /* DeviceMetricsLog.swift */; }; + DD77093F2AA1B146007A8BF0 /* UIColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD77093E2AA1B146007A8BF0 /* UIColor.swift */; }; DD798B072915928D005217CD /* ChannelMessageList.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD798B062915928D005217CD /* ChannelMessageList.swift */; }; DD8169F9271F1A6100F4AB02 /* MeshLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8169F8271F1A6100F4AB02 /* MeshLogger.swift */; }; DD8169FB271F1F3A00F4AB02 /* MeshLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8169FA271F1F3A00F4AB02 /* MeshLog.swift */; }; @@ -268,6 +269,7 @@ DD6193782863875F00E59241 /* SerialConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SerialConfig.swift; sourceTree = ""; }; DD73FD1028750779000852D6 /* PositionLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionLog.swift; sourceTree = ""; }; DD769E0228D18BF0001A3F05 /* DeviceMetricsLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceMetricsLog.swift; sourceTree = ""; }; + DD77093E2AA1B146007A8BF0 /* UIColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColor.swift; sourceTree = ""; }; DD798B062915928D005217CD /* ChannelMessageList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelMessageList.swift; sourceTree = ""; }; DD8169F8271F1A6100F4AB02 /* MeshLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeshLogger.swift; sourceTree = ""; }; DD8169FA271F1F3A00F4AB02 /* MeshLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeshLog.swift; sourceTree = ""; }; @@ -790,6 +792,7 @@ DDDB444329F8A8DD00EE2349 /* Float.swift */, DDDB444D29F8AB0E00EE2349 /* Int.swift */, DDDB444729F8A9C900EE2349 /* String.swift */, + DD77093E2AA1B146007A8BF0 /* UIColor.swift */, DDDB444F29F8AC9C00EE2349 /* UIImage.swift */, DDDB443F29F79AB000EE2349 /* UserDefaults.swift */, DDB75A0E2A05920E006ED576 /* FileManager.swift */, @@ -1145,6 +1148,7 @@ DD3CC6BC28E366DF00FA9159 /* Meshtastic.xcdatamodeld in Sources */, DDC4C9FF2A8D982900CE201C /* DetectionSensorConfig.swift in Sources */, DD5E5210298EE33B00D21B61 /* telemetry.pb.swift in Sources */, + DD77093F2AA1B146007A8BF0 /* UIColor.swift in Sources */, DD5E5205298EE33B00D21B61 /* mesh.pb.swift in Sources */, DDF6B2482A9AEBF500BA6931 /* StoreForward.swift in Sources */, DD8169F9271F1A6100F4AB02 /* MeshLogger.swift in Sources */, diff --git a/Meshtastic/Extensions/UIColor.swift b/Meshtastic/Extensions/UIColor.swift new file mode 100644 index 00000000..482ddc1b --- /dev/null +++ b/Meshtastic/Extensions/UIColor.swift @@ -0,0 +1,41 @@ +// +// UIColor.swift +// Meshtastic +// +// Copyright(c) Garth Vander Houwen 8/31/23. +// +import Foundation +import Swift +import UIKit + +extension UIColor { + + private func makeColor(componentDelta: CGFloat) -> UIColor { + var red: CGFloat = 0 + var blue: CGFloat = 0 + var green: CGFloat = 0 + var alpha: CGFloat = 0 + + getRed(&red, green: &green, blue: &blue, alpha: &alpha) + + return UIColor( + red: add(componentDelta, toComponent: red), + green: add(componentDelta, toComponent: green), + blue: add(componentDelta, toComponent: blue), + alpha: alpha + ) + } + + func lighter(componentDelta: CGFloat = 0.1) -> UIColor { + return makeColor(componentDelta: componentDelta) + } + + func darker(componentDelta: CGFloat = 0.1) -> UIColor { + return makeColor(componentDelta: -3*componentDelta) + } + + private func add(_ value: CGFloat, toComponent: CGFloat) -> CGFloat { + return max(0, min(1, toComponent + value)) + } + +} diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 1e0a767b..f6d4a9e6 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -290,7 +290,7 @@ struct MapViewSwiftUI: UIViewRepresentable { annotationView.tag = -1 annotationView.canShowCallout = true if positionAnnotation.latest { - annotationView.markerTintColor = .systemRed + annotationView.markerTintColor = UIColor(hex: UInt32(positionAnnotation.nodePosition?.num ?? 0)).darker() annotationView.displayPriority = .required annotationView.titleVisibility = .visible } else {