@@ -31,24 +31,43 @@ public struct Color {
3131}
3232
3333extension Color {
34-
35- public func withAlpha( _ alpha: Float ) -> Color {
36- var color = self
37- color. a = alpha
38- return color
39- }
40-
41- public func linearBlend( with other: Color , offset: Float ) -> Color {
42- return Color (
43- ( other. r - r) * offset + r,
44- ( other. g - g) * offset + g,
45- ( other. b - b) * offset + b,
46- ( other. a - a) * offset + a)
47- }
4834
35+ /// Returns a `Color` whose RBGA components are generated by given `RandomNumberGenerator`.
36+ ///
37+ public static func random< RNG: RandomNumberGenerator > ( using generator: inout RNG ) -> Color {
38+ return Color ( . random( in: 0 ... 1.0 , using: & generator) ,
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+ }
43+
44+ /// Returns a `Color` whose RBGA components are generated by the system's default `RandomNumberGenerator`.
45+ ///
4946 public static func random( ) -> Color {
50- return Color ( . random( in: 0 ... 1.0 ) , . random( in: 0 ... 1.0 ) ,
51- . random( in: 0 ... 1.0 ) , . random( in: 0 ... 1.0 ) )
47+ var generator = SystemRandomNumberGenerator ( )
48+ return Color . random ( using: & generator)
49+ }
50+
51+ /// Returns a `Color` whose RGB components are given by this color, and whose alpha component is `alpha`.
52+ ///
53+ public func withAlpha( _ alpha: Float ) -> Color {
54+ var color = self
55+ color. a = alpha
56+ return color
57+ }
58+
59+ /// Returns a `Color` whose components are a distance `offset` between this and another color.
60+ ///
61+ /// - parameters:
62+ /// - other: The color to blend with.
63+ /// - offset: The fractional distance between this color and `other`, between 0 and 1.
64+ /// A value of 0 always returns this color, and a value of 1 always returns `other`.
65+ ///
66+ public func linearBlend( with other: Color , offset: Float ) -> Color {
67+ return Color ( ( other. r - r) * offset + r,
68+ ( other. g - g) * offset + g,
69+ ( other. b - b) * offset + b,
70+ ( other. a - a) * offset + a)
5271 }
5372}
5473
0 commit comments