Meshtastic-Apple/Meshtastic/Extensions/UIColor.swift
Blake McAnally 4d547e48db This change fixes several lint errors throughout the project, and moves the SwiftLint build phase to before compilation.
After this change, a developer can now clone the project and run without the build failing due to lint errors! 😃

* I ran `swiftlint --fix` to resolve many auto-correctable issues (mostly whitespace)
* Excluded the `Meshtastic/Protobufs` directory from lint, since that code is automatically generated.
* Converted some single letter method parameters to lowercase.
* Converted several instances `force_cast` to instead use `guard` or `if let` to unwrap optional values. During this change, some of the SwiftUI views became "too complex to be solved in a reasonable time", so I broke up the views into distinct sub-expressions.

I was able to build and run the app on an iOS simulator.
2024-05-31 21:48:50 -05:00

49 lines
1 KiB
Swift

//
// 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: -1*componentDelta)
}
private func add(_ value: CGFloat, toComponent: CGFloat) -> CGFloat {
return max(0, min(1, toComponent + value))
}
static var random: UIColor {
return UIColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1.0
)
}
}