|
| 1 | +public struct Color { |
| 2 | + public var r: Float |
| 3 | + public var g: Float |
| 4 | + public var b: Float |
| 5 | + public var a: Float |
| 6 | + public init(_ r: Float, _ g: Float, _ b: Float, _ a: Float) { |
| 7 | + self.r = r |
| 8 | + self.g = g |
| 9 | + self.b = b |
| 10 | + self.a = a |
| 11 | + } |
| 12 | + public static let transparent = Color(0, 0, 0, 0) |
| 13 | + public static let black: Color = Color(0.0, 0.0, 0.0, 1.0) |
| 14 | + public static let white: Color = Color(1.0, 1.0, 1.0, 1.0) |
| 15 | + public static let transluscentWhite: Color = Color(1.0, 1.0, 1.0, 0.7) |
| 16 | + public static let purple: Color = Color(0.5, 0.0, 0.5, 1.0) |
| 17 | + public static let lightBlue: Color = Color(0.529, 0.808, 0.922, 1.0) |
| 18 | + public static let blue: Color = Color(0.0, 0.0, 1.0, 1.0) |
| 19 | + public static let darkBlue: Color = Color(0.0, 0.0, 0.54, 1.0) |
| 20 | + public static let green: Color = Color(0.0, 0.5, 0.0, 1.0) |
| 21 | + public static let darkGreen: Color = Color(0.0, 0.39, 0.0, 1.0) |
| 22 | + public static let yellow: Color = Color(1.0, 1.0, 0.0, 1.0) |
| 23 | + public static let gold: Color = Color(1.0, 0.84, 0.0, 1.0) |
| 24 | + public static let orange: Color = Color(1.0, 0.647, 0.0, 1.0) |
| 25 | + public static let red: Color = Color(1.0, 0.0, 0.0, 1.0) |
| 26 | + public static let darkRed: Color = Color(0.54, 0.0, 0.0, 1.0) |
| 27 | + public static let brown: Color = Color(0.54, 0.27, 0.1, 1.0) |
| 28 | + public static let pink: Color = Color(1.0, 0.75, 0.79, 1.0) |
| 29 | + public static let gray: Color = Color(0.5, 0.5, 0.5, 1.0) |
| 30 | + public static let darkGray: Color = Color(0.66, 0.66, 0.66, 1.0) |
| 31 | +} |
| 32 | + |
| 33 | +extension Color { |
| 34 | + |
| 35 | + /// Returns a `Color` whose RBGA components are generated by the given `RandomNumberGenerator`. |
| 36 | + /// |
| 37 | + public static func random<RNG: RandomNumberGenerator>(using generator: inout RNG) -> Color { |
| 38 | + return Color( |
| 39 | + .random(in: 0...1.0, using: &generator), |
| 40 | + .random(in: 0...1.0, using: &generator), |
| 41 | + .random(in: 0...1.0, using: &generator), |
| 42 | + .random(in: 0...1.0, using: &generator)) |
| 43 | + } |
| 44 | + |
| 45 | + /// Returns a `Color` whose RBGA components are generated by the system's default `RandomNumberGenerator`. |
| 46 | + /// |
| 47 | + public static func random() -> Color { |
| 48 | + var generator = SystemRandomNumberGenerator() |
| 49 | + return Color.random(using: &generator) |
| 50 | + } |
| 51 | + |
| 52 | + /// Returns a `Color` whose RGB components are given by this color, and whose alpha component is `alpha`. |
| 53 | + /// |
| 54 | + public func withAlpha(_ alpha: Float) -> Color { |
| 55 | + var color = self |
| 56 | + color.a = alpha |
| 57 | + return color |
| 58 | + } |
| 59 | + |
| 60 | + /// Returns a `Color` whose components are a distance `offset` between this and another color. |
| 61 | + /// |
| 62 | + /// - parameters: |
| 63 | + /// - other: The color to blend with. |
| 64 | + /// - offset: The fractional distance between this color and `other`, between 0 and 1. |
| 65 | + /// A value of 0 always returns this color, and a value of 1 always returns `other`. |
| 66 | + /// |
| 67 | + public func linearBlend(with other: Color, offset: Float) -> Color { |
| 68 | + return Color( |
| 69 | + (other.r - r) * offset + r, |
| 70 | + (other.g - g) * offset + g, |
| 71 | + (other.b - b) * offset + b, |
| 72 | + (other.a - a) * offset + a) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#if canImport(CoreGraphics) |
| 77 | + import CoreGraphics |
| 78 | + |
| 79 | + fileprivate let RGBColorSpace = CGColorSpaceCreateDeviceRGB() |
| 80 | + |
| 81 | + public extension Color { |
| 82 | + @available(tvOS 13.0, watchOS 6.0, *) |
| 83 | + var cgColor: CGColor { |
| 84 | + if #available(OSX 10.15, iOS 13.0, *) { |
| 85 | + return CGColor( |
| 86 | + srgbRed: CGFloat(r), |
| 87 | + green: CGFloat(g), |
| 88 | + blue: CGFloat(b), |
| 89 | + alpha: CGFloat(a)) |
| 90 | + } else { |
| 91 | + var tuple = (CGFloat(r), CGFloat(g), CGFloat(b), CGFloat(a)) |
| 92 | + return withUnsafePointer(to: &tuple) { tupPtr in |
| 93 | + return tupPtr.withMemoryRebound(to: CGFloat.self, capacity: 4) { floatPtr in |
| 94 | + return CGColor(colorSpace: RGBColorSpace, components: floatPtr)! |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +#endif |
0 commit comments