@@ -81,44 +81,34 @@ fn normalize_hue(hue: f32) -> f32 {
81
81
82
82
/// A color with red, green, blue, and alpha components, in a byte each.
83
83
#[ derive( Clone , Copy , PartialEq , Debug ) ]
84
- pub struct RGBA {
84
+ pub struct Rgba {
85
85
/// The red component.
86
- pub red : Option < u8 > ,
86
+ pub red : u8 ,
87
87
/// The green component.
88
- pub green : Option < u8 > ,
88
+ pub green : u8 ,
89
89
/// The blue component.
90
- pub blue : Option < u8 > ,
90
+ pub blue : u8 ,
91
91
/// The alpha component.
92
- pub alpha : Option < f32 > ,
92
+ pub alpha : f32 ,
93
93
}
94
94
95
- impl RGBA {
95
+ impl Rgba {
96
96
/// Constructs a new RGBA value from float components. It expects the red,
97
97
/// green, blue and alpha channels in that order, and all values will be
98
98
/// clamped to the 0.0 ... 1.0 range.
99
99
#[ inline]
100
- pub fn from_floats (
101
- red : Option < f32 > ,
102
- green : Option < f32 > ,
103
- blue : Option < f32 > ,
104
- alpha : Option < f32 > ,
105
- ) -> Self {
100
+ pub fn from_floats ( red : f32 , green : f32 , blue : f32 , alpha : f32 ) -> Self {
106
101
Self :: new (
107
- red . map ( clamp_unit_f32) ,
108
- green . map ( clamp_unit_f32) ,
109
- blue . map ( clamp_unit_f32) ,
110
- alpha. map ( |a| a . clamp ( 0.0 , OPAQUE ) ) ,
102
+ clamp_unit_f32 ( red ) ,
103
+ clamp_unit_f32 ( green ) ,
104
+ clamp_unit_f32 ( blue ) ,
105
+ alpha. clamp ( 0.0 , OPAQUE ) ,
111
106
)
112
107
}
113
108
114
109
/// Same thing, but with `u8` values instead of floats in the 0 to 1 range.
115
110
#[ inline]
116
- pub const fn new (
117
- red : Option < u8 > ,
118
- green : Option < u8 > ,
119
- blue : Option < u8 > ,
120
- alpha : Option < f32 > ,
121
- ) -> Self {
111
+ pub const fn new ( red : u8 , green : u8 , blue : u8 , alpha : f32 ) -> Self {
122
112
Self {
123
113
red,
124
114
green,
@@ -129,7 +119,7 @@ impl RGBA {
129
119
}
130
120
131
121
#[ cfg( feature = "serde" ) ]
132
- impl Serialize for RGBA {
122
+ impl Serialize for Rgba {
133
123
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
134
124
where
135
125
S : Serializer ,
@@ -139,32 +129,32 @@ impl Serialize for RGBA {
139
129
}
140
130
141
131
#[ cfg( feature = "serde" ) ]
142
- impl < ' de > Deserialize < ' de > for RGBA {
132
+ impl < ' de > Deserialize < ' de > for Rgba {
143
133
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
144
134
where
145
135
D : Deserializer < ' de > ,
146
136
{
147
137
let ( r, g, b, a) = Deserialize :: deserialize ( deserializer) ?;
148
- Ok ( RGBA :: new ( r, g, b, a) )
138
+ Ok ( Rgba :: new ( r, g, b, a) )
149
139
}
150
140
}
151
141
152
- impl ToCss for RGBA {
142
+ impl ToCss for Rgba {
153
143
fn to_css < W > ( & self , dest : & mut W ) -> fmt:: Result
154
144
where
155
145
W : fmt:: Write ,
156
146
{
157
- let has_alpha = self . alpha . unwrap_or ( 0.0 ) != OPAQUE ;
147
+ let has_alpha = self . alpha != OPAQUE ;
158
148
159
149
dest. write_str ( if has_alpha { "rgba(" } else { "rgb(" } ) ?;
160
- self . red . unwrap_or ( 0 ) . to_css ( dest) ?;
150
+ self . red . to_css ( dest) ?;
161
151
dest. write_str ( ", " ) ?;
162
- self . green . unwrap_or ( 0 ) . to_css ( dest) ?;
152
+ self . green . to_css ( dest) ?;
163
153
dest. write_str ( ", " ) ?;
164
- self . blue . unwrap_or ( 0 ) . to_css ( dest) ?;
154
+ self . blue . to_css ( dest) ?;
165
155
166
156
// Legacy syntax does not allow none components.
167
- serialize_color_alpha ( dest, Some ( self . alpha . unwrap_or ( 0.0 ) ) , true ) ?;
157
+ serialize_color_alpha ( dest, Some ( self . alpha ) , true ) ?;
168
158
169
159
dest. write_char ( ')' )
170
160
}
@@ -212,7 +202,7 @@ impl ToCss for Hsl {
212
202
self . lightness . unwrap_or ( 0.0 ) ,
213
203
) ;
214
204
215
- RGBA :: from_floats ( Some ( red) , Some ( green) , Some ( blue) , self . alpha ) . to_css ( dest)
205
+ Rgba :: from_floats ( red, green, blue, self . alpha . unwrap_or ( OPAQUE ) ) . to_css ( dest)
216
206
}
217
207
}
218
208
@@ -279,7 +269,7 @@ impl ToCss for Hwb {
279
269
self . blackness . unwrap_or ( 0.0 ) ,
280
270
) ;
281
271
282
- RGBA :: from_floats ( Some ( red) , Some ( green) , Some ( blue) , self . alpha ) . to_css ( dest)
272
+ Rgba :: from_floats ( red, green, blue, self . alpha . unwrap_or ( OPAQUE ) ) . to_css ( dest)
283
273
}
284
274
}
285
275
@@ -620,7 +610,7 @@ pub enum Color {
620
610
/// The 'currentcolor' keyword.
621
611
CurrentColor ,
622
612
/// Specify sRGB colors directly by their red/green/blue/alpha chanels.
623
- Rgba ( RGBA ) ,
613
+ Rgba ( Rgba ) ,
624
614
/// Specifies a color in sRGB using hue, saturation and lightness components.
625
615
Hsl ( Hsl ) ,
626
616
/// Specifies a color in sRGB using hue, whiteness and blackness components.
@@ -811,7 +801,7 @@ pub trait FromParsedColor {
811
801
fn from_current_color ( ) -> Self ;
812
802
813
803
/// Construct a new color from red, green, blue and alpha components.
814
- fn from_rgba ( red : Option < u8 > , green : Option < u8 > , blue : Option < u8 > , alpha : Option < f32 > ) -> Self ;
804
+ fn from_rgba ( red : u8 , green : u8 , blue : u8 , alpha : f32 ) -> Self ;
815
805
816
806
/// Construct a new color from hue, saturation, lightness and alpha components.
817
807
fn from_hsl (
@@ -875,28 +865,28 @@ where
875
865
{
876
866
Ok ( match value. len ( ) {
877
867
8 => O :: from_rgba (
878
- Some ( from_hex ( value[ 0 ] ) ? * 16 + from_hex ( value[ 1 ] ) ?) ,
879
- Some ( from_hex ( value[ 2 ] ) ? * 16 + from_hex ( value[ 3 ] ) ?) ,
880
- Some ( from_hex ( value[ 4 ] ) ? * 16 + from_hex ( value[ 5 ] ) ?) ,
881
- Some ( ( from_hex ( value[ 6 ] ) ? * 16 + from_hex ( value[ 7 ] ) ?) as f32 / 255.0 ) ,
868
+ from_hex ( value[ 0 ] ) ? * 16 + from_hex ( value[ 1 ] ) ?,
869
+ from_hex ( value[ 2 ] ) ? * 16 + from_hex ( value[ 3 ] ) ?,
870
+ from_hex ( value[ 4 ] ) ? * 16 + from_hex ( value[ 5 ] ) ?,
871
+ ( from_hex ( value[ 6 ] ) ? * 16 + from_hex ( value[ 7 ] ) ?) as f32 / 255.0 ,
882
872
) ,
883
873
6 => O :: from_rgba (
884
- Some ( from_hex ( value[ 0 ] ) ? * 16 + from_hex ( value[ 1 ] ) ?) ,
885
- Some ( from_hex ( value[ 2 ] ) ? * 16 + from_hex ( value[ 3 ] ) ?) ,
886
- Some ( from_hex ( value[ 4 ] ) ? * 16 + from_hex ( value[ 5 ] ) ?) ,
887
- Some ( OPAQUE ) ,
874
+ from_hex ( value[ 0 ] ) ? * 16 + from_hex ( value[ 1 ] ) ?,
875
+ from_hex ( value[ 2 ] ) ? * 16 + from_hex ( value[ 3 ] ) ?,
876
+ from_hex ( value[ 4 ] ) ? * 16 + from_hex ( value[ 5 ] ) ?,
877
+ OPAQUE ,
888
878
) ,
889
879
4 => O :: from_rgba (
890
- Some ( from_hex ( value[ 0 ] ) ? * 17 ) ,
891
- Some ( from_hex ( value[ 1 ] ) ? * 17 ) ,
892
- Some ( from_hex ( value[ 2 ] ) ? * 17 ) ,
893
- Some ( ( from_hex ( value[ 3 ] ) ? * 17 ) as f32 / 255.0 ) ,
880
+ from_hex ( value[ 0 ] ) ? * 17 ,
881
+ from_hex ( value[ 1 ] ) ? * 17 ,
882
+ from_hex ( value[ 2 ] ) ? * 17 ,
883
+ ( from_hex ( value[ 3 ] ) ? * 17 ) as f32 / 255.0 ,
894
884
) ,
895
885
3 => O :: from_rgba (
896
- Some ( from_hex ( value[ 0 ] ) ? * 17 ) ,
897
- Some ( from_hex ( value[ 1 ] ) ? * 17 ) ,
898
- Some ( from_hex ( value[ 2 ] ) ? * 17 ) ,
899
- Some ( OPAQUE ) ,
886
+ from_hex ( value[ 0 ] ) ? * 17 ,
887
+ from_hex ( value[ 1 ] ) ? * 17 ,
888
+ from_hex ( value[ 2 ] ) ? * 17 ,
889
+ OPAQUE ,
900
890
) ,
901
891
_ => return Err ( ( ) ) ,
902
892
} )
@@ -934,8 +924,8 @@ impl FromParsedColor for Color {
934
924
}
935
925
936
926
#[ inline]
937
- fn from_rgba ( red : Option < u8 > , green : Option < u8 > , blue : Option < u8 > , alpha : Option < f32 > ) -> Self {
938
- Color :: Rgba ( RGBA :: new ( red, green, blue, alpha) )
927
+ fn from_rgba ( red : u8 , green : u8 , blue : u8 , alpha : f32 ) -> Self {
928
+ Color :: Rgba ( Rgba :: new ( red, green, blue, alpha) )
939
929
}
940
930
941
931
fn from_hsl (
@@ -1173,10 +1163,10 @@ where
1173
1163
}
1174
1164
1175
1165
match_ignore_ascii_case ! { ident ,
1176
- "transparent" => Ok ( Output :: from_rgba( Some ( 0 ) , Some ( 0 ) , Some ( 0 ) , Some ( 0.0 ) ) ) ,
1166
+ "transparent" => Ok ( Output :: from_rgba( 0 , 0 , 0 , 0.0 ) ) ,
1177
1167
"currentcolor" => Ok ( Output :: from_current_color( ) ) ,
1178
1168
_ => keyword( ident)
1179
- . map( |( r, g, b) | Output :: from_rgba( Some ( * r ) , Some ( * g ) , Some ( * b ) , Some ( 1.0 ) ) )
1169
+ . map( |( r, g, b) | Output :: from_rgba( * r , * g , * b , 1.0 ) )
1180
1170
. ok_or( ( ) ) ,
1181
1171
}
1182
1172
}
@@ -1326,34 +1316,35 @@ where
1326
1316
// are parsing the legacy syntax.
1327
1317
let is_legacy_syntax = maybe_red. is_some ( ) && arguments. try_parse ( |p| p. expect_comma ( ) ) . is_ok ( ) ;
1328
1318
1329
- let red: Option < u8 > ;
1330
- let green: Option < u8 > ;
1331
- let blue: Option < u8 > ;
1319
+ let red: u8 ;
1320
+ let green: u8 ;
1321
+ let blue: u8 ;
1332
1322
1333
1323
let alpha = if is_legacy_syntax {
1334
1324
match maybe_red. unwrap ( ) {
1335
1325
NumberOrPercentage :: Number { value } => {
1336
- red = Some ( clamp_floor_256_f32 ( value) ) ;
1337
- green = Some ( clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ) ;
1326
+ red = clamp_floor_256_f32 ( value) ;
1327
+ green = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1338
1328
arguments. expect_comma ( ) ?;
1339
- blue = Some ( clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ) ;
1329
+ blue = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1340
1330
}
1341
1331
NumberOrPercentage :: Percentage { unit_value } => {
1342
- red = Some ( clamp_unit_f32 ( unit_value) ) ;
1343
- green = Some ( clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ) ;
1332
+ red = clamp_unit_f32 ( unit_value) ;
1333
+ green = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1344
1334
arguments. expect_comma ( ) ?;
1345
- blue = Some ( clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ) ;
1335
+ blue = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1346
1336
}
1347
1337
}
1348
1338
1349
- Some ( parse_legacy_alpha ( color_parser, arguments) ?)
1339
+ parse_legacy_alpha ( color_parser, arguments) ?
1350
1340
} else {
1351
1341
#[ inline]
1352
- fn get_component_value ( c : Option < NumberOrPercentage > ) -> Option < u8 > {
1342
+ fn get_component_value ( c : Option < NumberOrPercentage > ) -> u8 {
1353
1343
c. map ( |c| match c {
1354
1344
NumberOrPercentage :: Number { value } => clamp_floor_256_f32 ( value) ,
1355
1345
NumberOrPercentage :: Percentage { unit_value } => clamp_unit_f32 ( unit_value) ,
1356
1346
} )
1347
+ . unwrap_or ( 0 )
1357
1348
}
1358
1349
1359
1350
red = get_component_value ( maybe_red) ;
@@ -1366,7 +1357,7 @@ where
1366
1357
color_parser. parse_number_or_percentage ( p)
1367
1358
} ) ?) ;
1368
1359
1369
- parse_modern_alpha ( color_parser, arguments) ?
1360
+ parse_modern_alpha ( color_parser, arguments) ?. unwrap_or ( 0.0 )
1370
1361
} ;
1371
1362
1372
1363
Ok ( P :: Output :: from_rgba ( red, green, blue, alpha) )
0 commit comments