55// Created by Coen ten Thije Boonkkamp on 17/09/2024.
66//
77
8- import Color_Standard
8+ import CSS
9+ import CSS_Standard
10+
11+ /// Backward-compatible typealias for DarkModeColor.
12+ /// HTMLColor was the previous name, now unified as DarkModeColor in CSS module.
13+ @available ( * , deprecated, renamed: " DarkModeColor " )
14+ public typealias HTMLColor = DarkModeColor
15+
916
1017// MARK: - Text Color Definitions
1118
12- // extension HTMLColor {
19+ // extension DarkModeColor {
1320// public static let buttonText: Self = .primary.reverse()
1421// public static let primary: Self = .black.withDarkColor(.white)
1522// public static let text: Self = primary
@@ -19,12 +26,12 @@ import Color_Standard
1926
2027// MARK: - Background Color Definitions
2128//
22- // extension HTMLColor {
29+ // extension DarkModeColor {
2330// public static let offBackground: Self = .offWhite.withDarkColor(.offBlack)
2431// public static let background: Self = .white.withDarkColor(.black)
2532// }
2633
27- // MARK: - HTMLColor Extensions
34+ // MARK: - DarkModeColor Extensions
2835
2936extension DarkModeColor {
3037 private typealias sRGB = IEC_61966 . `2` . `1` . sRGB
@@ -38,7 +45,7 @@ extension DarkModeColor {
3845 _ c1: CSS_Standard . Color . Value ,
3946 _ c2: CSS_Standard . Color . Value
4047 ) -> CSS_Standard . Color . Value ? {
41- guard let srgb1 = toSRGB ( c1) , let srgb2 = toSRGB ( c2) else { return nil }
48+ guard let srgb1 = sRGB ( c1) , let srgb2 = sRGB ( c2) else { return nil }
4249
4350 let midR = ( srgb1. r255 + srgb2. r255) / 2
4451 let midG = ( srgb1. g255 + srgb2. g255) / 2
@@ -55,7 +62,7 @@ extension DarkModeColor {
5562 }
5663
5764 public static func readablePrimaryColor( on backgroundColor: DarkModeColor ) -> Self {
58- guard let srgb = toSRGB ( backgroundColor. light) else {
65+ guard let srgb = sRGB ( backgroundColor. light) else {
5966 return . init( . hex( " 000000 " ) )
6067 }
6168 // Perceived brightness using ITU-R BT.601 coefficients
@@ -71,8 +78,8 @@ extension DarkModeColor {
7178extension HTML . View {
7279 /// Applies a gradient background to the HTML element
7380 public func gradient(
74- bottom: HTMLColor ,
75- top: HTMLColor
81+ bottom: DarkModeColor ,
82+ top: DarkModeColor
7683 ) -> some HTML . View {
7784 self
7885 . css
@@ -89,9 +96,9 @@ extension HTML.View {
8996 }
9097
9198 public func gradient(
92- bottom: HTMLColor ,
93- middle: HTMLColor ,
94- top: HTMLColor
99+ bottom: DarkModeColor ,
100+ middle: DarkModeColor ,
101+ top: DarkModeColor
95102 ) -> some HTML . View {
96103 self
97104 . css
@@ -107,69 +114,3 @@ extension HTML.View {
107114 }
108115 }
109116}
110-
111- // MARK: - Private Helper Functions
112-
113- extension DarkModeColor {
114- fileprivate static func toSRGB( _ color: CSS_Standard . Color . Value ) -> IEC_61966 . `2` . `1` . sRGB ? {
115- typealias sRGB = IEC_61966 . `2` . `1` . sRGB
116-
117- switch color {
118- case . named( let namedColor) :
119- return namedColor. toSRGB ( )
120- case . hex( let hexColor) :
121- return sRGB ( hex: hexColor. value)
122- case . rgb( let r, let g, let b) :
123- return sRGB ( r255: r, g255: g, b255: b)
124- case . rgba( let r, let g, let b, _) :
125- return sRGB ( r255: r, g255: g, b255: b)
126- case . hsl( let h, let s, let l) :
127- return sRGB ( h: h. normalizedDegrees ( ) , s: s / 100 , l: l / 100 )
128- case . hsla( let h, let s, let l, _) :
129- return sRGB ( h: h. normalizedDegrees ( ) , s: s / 100 , l: l / 100 )
130- case . hwb( let h, let w, let b) :
131- return sRGB ( hue: h. normalizedDegrees ( ) , whiteness: w / 100 , blackness: b / 100 )
132- case . lab( let l, let a, let b) :
133- return Color . LAB ( l: l, a: a, b: b) . converted ( to: sRGB. self)
134- case . lch( let l, let c, let h) :
135- return Color . LCH ( l: l, c: c, h: h) . converted ( to: sRGB. self)
136- case . oklab( let l, let a, let b) :
137- return Color . Oklab ( l: l, a: a, b: b) . converted ( to: sRGB. self)
138- case . oklch( let l, let c, let h) :
139- return Color . Oklch ( l: l, c: c, h: h) . converted ( to: sRGB. self)
140- default :
141- return nil
142- }
143- }
144- }
145-
146- // MARK: - Named Color sRGB Mapping
147-
148- extension W3C_CSS_Values . NamedColor {
149- /// Maps CSS named colors to sRGB values
150- fileprivate func toSRGB( ) -> IEC_61966 . `2` . `1` . sRGB ? {
151- typealias sRGB = IEC_61966 . `2` . `1` . sRGB
152-
153- switch self {
154- case . black: return . black
155- case . white: return . white
156- case . red: return . red
157- case . green: return sRGB ( r255: 0 , g255: 128 , b255: 0 )
158- case . lime: return sRGB ( r255: 0 , g255: 255 , b255: 0 )
159- case . blue: return . blue
160- case . yellow: return sRGB ( r255: 255 , g255: 255 , b255: 0 )
161- case . cyan: return sRGB ( r255: 0 , g255: 255 , b255: 255 )
162- case . magenta: return sRGB ( r255: 255 , g255: 0 , b255: 255 )
163- case . silver: return sRGB ( r255: 192 , g255: 192 , b255: 192 )
164- case . gray: return sRGB ( r255: 128 , g255: 128 , b255: 128 )
165- case . maroon: return sRGB ( r255: 128 , g255: 0 , b255: 0 )
166- case . purple: return sRGB ( r255: 128 , g255: 0 , b255: 128 )
167- case . fuchsia: return sRGB ( r255: 255 , g255: 0 , b255: 255 )
168- case . olive: return sRGB ( r255: 128 , g255: 128 , b255: 0 )
169- case . navy: return sRGB ( r255: 0 , g255: 0 , b255: 128 )
170- case . teal: return sRGB ( r255: 0 , g255: 128 , b255: 128 )
171- case . aqua: return sRGB ( r255: 0 , g255: 255 , b255: 255 )
172- default : return nil
173- }
174- }
175- }
0 commit comments