@@ -10,36 +10,24 @@ use super::{BasicParseError, ParseError, Parser, ToCss, Token};
10
10
#[ cfg( feature = "serde" ) ]
11
11
use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
12
12
13
- /// Implement for colors with an alpha channel.
14
- pub trait WithAlpha {
15
- const LEGACY_SYNTAX : bool = false ;
16
-
17
- fn alpha ( & self ) -> u8 ;
18
-
19
- /// Returns the alpha channel in a floating point number form, from 0 to 1.
20
- #[ inline]
21
- fn alpha_f32 ( & self ) -> f32 {
22
- self . alpha ( ) as f32 / 255.0
13
+ /// https://w3c.github.io/csswg-drafts/css-color-4/#serializing-alpha-values
14
+ #[ inline]
15
+ fn serialize_alpha ( dest : & mut impl fmt:: Write , alpha : u8 , legacy_syntax : bool ) -> fmt:: Result {
16
+ // If the alpha component is full opaque, don't emit the alpha value in CSS.
17
+ if alpha == 255 {
18
+ return Ok ( ( ) ) ;
23
19
}
24
20
25
- /// https://w3c.github.io/csswg-drafts/css-color-4/#serializing-alpha-values
26
- #[ inline]
27
- fn serialize_alpha ( & self , dest : & mut impl fmt:: Write ) -> fmt:: Result {
28
- // If the alpha component is full opaque, don't emit the alpha value in CSS.
29
- if self . alpha ( ) == 255 {
30
- return Ok ( ( ) ) ;
31
- }
32
-
33
- dest. write_str ( if Self :: LEGACY_SYNTAX { ", " } else { " / " } ) ?;
34
-
35
- // Try first with two decimal places, then with three.
36
- let mut rounded_alpha = ( self . alpha_f32 ( ) * 100. ) . round ( ) / 100. ;
37
- if clamp_unit_f32 ( rounded_alpha) != self . alpha ( ) {
38
- rounded_alpha = ( self . alpha_f32 ( ) * 1000. ) . round ( ) / 1000. ;
39
- }
21
+ dest. write_str ( if legacy_syntax { ", " } else { " / " } ) ?;
40
22
41
- rounded_alpha. to_css ( dest)
23
+ // Try first with two decimal places, then with three.
24
+ let alpha_f32 = alpha as f32 / 255.0 ;
25
+ let mut rounded_alpha = ( alpha_f32 * 100. ) . round ( ) / 100. ;
26
+ if clamp_unit_f32 ( rounded_alpha) != alpha {
27
+ rounded_alpha = ( alpha_f32 * 1000. ) . round ( ) / 1000. ;
42
28
}
29
+
30
+ rounded_alpha. to_css ( dest)
43
31
}
44
32
45
33
/// A color with red, green, blue, and alpha components, in a byte each.
@@ -56,15 +44,6 @@ pub struct RGBA {
56
44
pub alpha : u8 ,
57
45
}
58
46
59
- impl WithAlpha for RGBA {
60
- const LEGACY_SYNTAX : bool = true ;
61
-
62
- #[ inline]
63
- fn alpha ( & self ) -> u8 {
64
- self . alpha
65
- }
66
- }
67
-
68
47
impl RGBA {
69
48
/// Constructs a new RGBA value from float components. It expects the red,
70
49
/// green, blue and alpha channels in that order, and all values will be
@@ -179,23 +158,23 @@ impl ToCss for RGBA {
179
158
where
180
159
W : fmt:: Write ,
181
160
{
182
- let serialize_alpha = self . alpha != 255 ;
161
+ let has_alpha = self . alpha != 255 ;
183
162
184
- dest. write_str ( if serialize_alpha { "rgba(" } else { "rgb(" } ) ?;
163
+ dest. write_str ( if has_alpha { "rgba(" } else { "rgb(" } ) ?;
185
164
self . red . to_css ( dest) ?;
186
165
dest. write_str ( ", " ) ?;
187
166
self . green . to_css ( dest) ?;
188
167
dest. write_str ( ", " ) ?;
189
168
self . blue . to_css ( dest) ?;
190
169
191
- self . serialize_alpha ( dest) ?;
170
+ serialize_alpha ( dest, self . alpha , true ) ?;
192
171
193
172
dest. write_char ( ')' )
194
173
}
195
174
}
196
175
197
- // NOTE: LAB and OKLAB is not declared inside the macro, because it causes
198
- // cbindgen to ignore them.
176
+ // NOTE: LAB and OKLAB is not declared inside the [impl_lab_like] macro,
177
+ // because it causes cbindgen to ignore them.
199
178
200
179
/// Color specified by lightness, a- and b-axis components.
201
180
#[ derive( Clone , Copy , PartialEq , Debug ) ]
@@ -227,13 +206,6 @@ pub struct OKLAB {
227
206
228
207
macro_rules! impl_lab_like {
229
208
( $cls: ident, $fname: literal) => {
230
- impl WithAlpha for $cls {
231
- #[ inline]
232
- fn alpha( & self ) -> u8 {
233
- self . alpha
234
- }
235
- }
236
-
237
209
impl $cls {
238
210
/// Construct a new Lab color format with lightness, a, b and alpha components.
239
211
pub fn new( lightness: f32 , a: f32 , b: f32 , alpha: u8 ) -> Self {
@@ -263,7 +235,7 @@ macro_rules! impl_lab_like {
263
235
D : Deserializer <' de>,
264
236
{
265
237
let ( lightness, a, b, alpha) = Deserialize :: deserialize( deserializer) ?;
266
- Ok ( LAB :: new( lightness, a, b, alpha) )
238
+ Ok ( Self :: new( lightness, a, b, alpha) )
267
239
}
268
240
}
269
241
@@ -275,13 +247,11 @@ macro_rules! impl_lab_like {
275
247
dest. write_str( $fname) ?;
276
248
dest. write_str( "(" ) ?;
277
249
self . lightness. to_css( dest) ?;
278
- dest. write_str ( " " ) ?;
250
+ dest. write_char ( ' ' ) ?;
279
251
self . a. to_css( dest) ?;
280
- dest. write_str ( " " ) ?;
252
+ dest. write_char ( ' ' ) ?;
281
253
self . b. to_css( dest) ?;
282
- if self . alpha( ) != 255 {
283
- self . serialize_alpha( dest) ?;
284
- }
254
+ serialize_alpha( dest, self . alpha, false ) ?;
285
255
dest. write_char( ')' )
286
256
}
287
257
}
@@ -291,8 +261,8 @@ macro_rules! impl_lab_like {
291
261
impl_lab_like ! ( LAB , "lab" ) ;
292
262
impl_lab_like ! ( OKLAB , "oklab" ) ;
293
263
294
- // NOTE: LCH and OKLCH is not declared inside the macro, because it causes
295
- // cbindgen to ignore them.
264
+ // NOTE: LCH and OKLCH is not declared inside the [impl_lch_like] macro,
265
+ // because it causes cbindgen to ignore them.
296
266
297
267
/// Color specified by lightness, chroma and hue components.
298
268
#[ derive( Clone , Copy , PartialEq , Debug ) ]
@@ -324,13 +294,6 @@ pub struct OKLCH {
324
294
325
295
macro_rules! impl_lch_like {
326
296
( $cls: ident, $fname: literal) => {
327
- impl WithAlpha for $cls {
328
- #[ inline]
329
- fn alpha( & self ) -> u8 {
330
- self . alpha
331
- }
332
- }
333
-
334
297
impl $cls {
335
298
/// Construct a new color with lightness, chroma and hue components.
336
299
pub fn new( lightness: f32 , chroma: f32 , hue: f32 , alpha: u8 ) -> Self {
@@ -360,7 +323,7 @@ macro_rules! impl_lch_like {
360
323
D : Deserializer <' de>,
361
324
{
362
325
let ( lightness, chroma, hue, alpha) = Deserialize :: deserialize( deserializer) ?;
363
- Ok ( LCH :: new( lightness, chroma, hue, alpha) )
326
+ Ok ( Self :: new( lightness, chroma, hue, alpha) )
364
327
}
365
328
}
366
329
@@ -372,13 +335,11 @@ macro_rules! impl_lch_like {
372
335
dest. write_str( $fname) ?;
373
336
dest. write_str( "(" ) ?;
374
337
self . lightness. to_css( dest) ?;
375
- dest. write_str ( " " ) ?;
338
+ dest. write_char ( ' ' ) ?;
376
339
self . chroma. to_css( dest) ?;
377
- dest. write_str ( " " ) ?;
340
+ dest. write_char ( ' ' ) ?;
378
341
self . hue. to_css( dest) ?;
379
- if self . alpha( ) != 255 {
380
- self . serialize_alpha( dest) ?;
381
- }
342
+ serialize_alpha( dest, self . alpha, false ) ?;
382
343
dest. write_char( ')' )
383
344
}
384
345
}
@@ -414,11 +375,11 @@ impl AbsoluteColor {
414
375
/// Return the alpha component of any of the schemes within.
415
376
pub fn alpha ( & self ) -> u8 {
416
377
match self {
417
- Self :: RGBA ( c) => c. alpha ( ) ,
418
- Self :: LAB ( c) => c. alpha ( ) ,
419
- Self :: LCH ( c) => c. alpha ( ) ,
420
- Self :: OKLAB ( c) => c. alpha ( ) ,
421
- Self :: OKLCH ( c) => c. alpha ( ) ,
378
+ Self :: RGBA ( c) => c. alpha ,
379
+ Self :: LAB ( c) => c. alpha ,
380
+ Self :: LCH ( c) => c. alpha ,
381
+ Self :: OKLAB ( c) => c. alpha ,
382
+ Self :: OKLCH ( c) => c. alpha ,
422
383
}
423
384
}
424
385
}
0 commit comments