Clean up number to UIColor conversion

This commit is contained in:
Garth Vander Houwen 2023-04-01 01:37:26 -07:00
parent 6dc7abc42c
commit 6ed72b0a42
6 changed files with 22 additions and 41 deletions

View file

@ -12,7 +12,7 @@ extension Character {
extension CLLocationCoordinate2D {
/// Returns distance from coordianate in meters.
/// - Parameter from: coordinate which will be used as end point.
/// - Returns: Returns distance in meters.
/// - Returns: distance in meters.
func distance(from: CLLocationCoordinate2D) -> CLLocationDistance {
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: self.latitude, longitude: self.longitude)
@ -21,6 +21,8 @@ extension CLLocationCoordinate2D {
}
extension Color {
/// Returns a boolean for a SwiftUI Color to determine what color of text to use
/// - Returns: true if the color is light
func isLight() -> Bool {
guard let components = cgColor?.components, components.count > 2 else {return false}
let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
@ -29,11 +31,22 @@ extension Color {
}
extension UIColor {
/// Returns a boolean for a UIColor to determine what color of text to use
/// - Returns: true if the color is light
func isLight() -> Bool {
guard let components = cgColor.components, components.count > 2 else {return false}
let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
return (brightness > 0.5)
}
/// Returns a UIColor from a UInt32 value
/// - Parameter hex: UInt32 value to convert to a color
/// - Returns: UIColor
convenience init(hex: UInt32) {
let red = CGFloat((hex & 0xFF0000) >> 16)
let green = CGFloat((hex & 0x00FF00) >> 8)
let blue = CGFloat((hex & 0x0000FF))
self.init(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: 1.0)
}
}
extension Data {
@ -88,38 +101,6 @@ extension Int {
}
}
extension Int64 {
func uiColor() -> UIColor {
let color = UIColor(
red: CGFloat((UInt32(self) & 0xFF0000) >> 16) / 255.0,
green: CGFloat((UInt32(self) & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(UInt32(self) & 0x0000FF) / 255.0,
alpha: CGFloat(1.0))
return color
}
}
//extension Int64 {
//
// func uiColor() -> UIColor {
//
// let bytes = withUnsafeBytes(of: UInt32(self).littleEndian, Array.init)
// let redBytes = bytes[0]
// let greenBytes = bytes[1]
// let blueBytes = bytes[2]
// let color = UIColor(
//
// red: CGFloat(redBytes),
// green: CGFloat(greenBytes),
// blue: CGFloat(blueBytes),
// alpha: CGFloat(1.0))
//
// return color
// }
//}
extension UIImage {
func rotate(radians: Float) -> UIImage? {
var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).size

View file

@ -197,7 +197,7 @@ struct MapViewSwiftUI: UIViewRepresentable {
annotationView.displayPriority = .required
annotationView.titleVisibility = .visible
} else {
annotationView.markerTintColor = positionAnnotation.nodePosition?.num.uiColor()
annotationView.markerTintColor = UIColor(hex: UInt32(positionAnnotation.nodePosition?.num ?? 0))
annotationView.displayPriority = .defaultHigh
annotationView.titleVisibility = .adaptive
}
@ -352,7 +352,7 @@ struct MapViewSwiftUI: UIViewRepresentable {
let titleString = routePolyline.title ?? "0"
let index = Int(titleString.components(separatedBy: "-").last ?? "0")
let renderer = MKPolylineRenderer(polyline: routePolyline)
renderer.strokeColor = Int64(titleString)?.uiColor()
renderer.strokeColor = UIColor(hex: UInt32(titleString) ?? 0)
renderer.lineWidth = 8
return renderer
}

View file

@ -150,7 +150,7 @@ struct Contacts: View {
HStack {
VStack {
HStack {
CircleText(text: user.shortName ?? "???", color: Color(user.num.uiColor()), circleSize: 60, fontSize: 18, textColor: user.num.uiColor().isLight() ? .black : .white)
CircleText(text: user.shortName ?? "???", color: Color(UIColor(hex: UInt32(user.num))), circleSize: 60, fontSize: 18, textColor: UIColor(hex: UInt32(user.num)).isLight() ? .black : .white)
.padding(.trailing, 5)
VStack {
HStack {

View file

@ -58,7 +58,7 @@ struct UserMessageList: View {
HStack(alignment: .top) {
if currentUser { Spacer(minLength: 50) }
if !currentUser {
CircleText(text: message.fromUser?.shortName ?? "????", color: currentUser ? .accentColor : Color(.gray), circleSize: 44, fontSize: 14)
CircleText(text: message.fromUser?.shortName ?? "????", color: currentUser ? .accentColor : Color(.gray))
.padding(.all, 5)
.offset(y: -5)
}
@ -360,7 +360,7 @@ struct UserMessageList: View {
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
CircleText(text: user.shortName ?? "???", color: Color(user.num.uiColor()), circleSize: 44, fontSize: 14, textColor: user.num.uiColor().isLight() ? .black : .white ).fixedSize()
CircleText(text: user.shortName ?? "???", color: Color(UIColor(hex: UInt32(user.num))), circleSize: 44, fontSize: 14, textColor: UIColor(hex: UInt32(user.num)).isLight() ? .black : .white ).fixedSize()
Text(user.longName ?? NSLocalizedString("unknown", comment: "Unknown")).font(.headline)
}
}

View file

@ -145,7 +145,7 @@ struct NodeDetail: View {
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
HStack {
VStack(alignment: .center) {
CircleText(text: node.user?.shortName ?? "???", color: Color(node.num.uiColor()), circleSize: 75, fontSize: 26, textColor: node.num.uiColor().isLight() ? .black : .white )
CircleText(text: node.user?.shortName ?? "???", color: Color(UIColor(hex: UInt32(node.num))), circleSize: 75, fontSize: 24, textColor: UIColor(hex: UInt32(node.num)).isLight() ? .black : .white )
}
Divider()
VStack {
@ -259,7 +259,7 @@ struct NodeDetail: View {
HStack {
VStack(alignment: .center) {
CircleText(text: node.user?.shortName ?? "???", color: Color(node.num.uiColor()), textColor: node.num.uiColor().isLight() ? .black : .white )
CircleText(text: node.user?.shortName ?? "???", color: Color(UIColor(hex: UInt32(node.num))), circleSize: 65, fontSize: 20, textColor: UIColor(hex: UInt32(node.num)).isLight() ? .black : .white )
}
Divider()
VStack {

View file

@ -36,7 +36,7 @@ struct NodeList: View {
let connected: Bool = (bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral?.num ?? -1 == node.num)
VStack(alignment: .leading) {
HStack {
CircleText(text: node.user?.shortName ?? "???", color: Color(node.num.uiColor()), circleSize: 65, fontSize: 20, brightness: 0.0, textColor: node.num.uiColor().isLight() ? .black : .white)
CircleText(text: node.user?.shortName ?? "???", color: Color(UIColor(hex: UInt32(node.num))), circleSize: 65, fontSize: 20, brightness: 0.0, textColor: UIColor(hex: UInt32(node.num)).isLight() ? .black : .white)
.padding(.trailing, 5)
VStack(alignment: .leading) {
Text(node.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown")).font(.headline)