|
7 | 7 |
|
8 | 8 | public struct CSSColor: ExpressibleByStringLiteral { |
9 | 9 |
|
10 | | - private var css: String |
| 10 | + private var colorValue: String |
11 | 11 |
|
12 | 12 | init(raw: String) { |
13 | | - css = raw |
| 13 | + colorValue = raw |
14 | 14 | } |
15 | 15 |
|
16 | 16 | // init(hex: String) { |
17 | 17 | // css = hex |
18 | 18 | // } |
19 | | - |
| 19 | + |
20 | 20 | public init(stringLiteral value: StringLiteralType) { |
21 | | - css = value |
| 21 | + colorValue = value |
22 | 22 | /// check if length is valid (000, #000, cafe00, #cafe00) |
23 | 23 | assert([3,4,6,7].contains(value.count), "Invalid hex string") |
24 | 24 |
|
25 | 25 | /// add # prefix if missing |
26 | | - if !css.hasPrefix("#") { |
27 | | - css = "#" + css |
| 26 | + if !colorValue.hasPrefix("#") { |
| 27 | + colorValue = "#" + colorValue |
28 | 28 | } |
29 | 29 | } |
30 | 30 |
|
31 | 31 | public init(r: Int, g: Int, b: Int, a: Double? = nil) { |
32 | | - css = "\(r),\(g),\(b)" |
| 32 | + colorValue = "\(r),\(g),\(b)" |
33 | 33 | if let a = a { |
34 | | - css = "rgba(" + css + ", \(a))" |
| 34 | + colorValue = "rgba(" + colorValue + ", \(a))" |
35 | 35 | } |
36 | 36 | else { |
37 | | - css = "rgb(" + css + ")" |
| 37 | + colorValue = "rgb(" + colorValue + ")" |
38 | 38 | } |
39 | 39 | } |
40 | 40 |
|
41 | 41 | public init(r: Double, g: Double, b: Double, a: Double? = nil) { |
42 | | - css = "\(r)%,\(g)%,\(b)%" |
| 42 | + colorValue = "\(r)%,\(g)%,\(b)%" |
43 | 43 | if let a = a { |
44 | | - css = "rgba(" + css + ", \(a))" |
| 44 | + colorValue = "rgba(" + colorValue + ", \(a))" |
45 | 45 | } |
46 | 46 | else { |
47 | | - css = "rgb(" + css + ")" |
| 47 | + colorValue = "rgb(" + colorValue + ")" |
48 | 48 | } |
49 | 49 | } |
50 | 50 |
|
51 | 51 | public init(h: Int, s: Int, l: Int, a: Double? = nil) { |
52 | | - css = "\(h),\(s),\(l)" |
| 52 | + colorValue = "\(h),\(s),\(l)" |
53 | 53 | if let a = a { |
54 | | - css = "hsla(" + css + ", \(a))" |
| 54 | + colorValue = "hsla(" + colorValue + ", \(a))" |
55 | 55 | } |
56 | 56 | else { |
57 | | - css = "hsl(" + css + ")" |
| 57 | + colorValue = "hsl(" + colorValue + ")" |
58 | 58 | } |
59 | 59 | } |
60 | 60 |
|
61 | 61 | public init(h: Double, s: Double, l: Double, a: Double? = nil) { |
62 | | - css = "\(h)%,\(s)%,\(l)%" |
| 62 | + colorValue = "\(h)%,\(s)%,\(l)%" |
63 | 63 | if let a = a { |
64 | | - css = "hsla(" + css + ", \(a))" |
| 64 | + colorValue = "hsla(" + colorValue + ", \(a))" |
65 | 65 | } |
66 | 66 | else { |
67 | | - css = "hsl(" + css + ")" |
| 67 | + colorValue = "hsl(" + colorValue + ")" |
68 | 68 | } |
69 | 69 | } |
70 | 70 |
|
71 | 71 | var rawValue: String { |
72 | | - css |
| 72 | + colorValue |
73 | 73 | } |
74 | 74 | } |
75 | 75 |
|
0 commit comments