@@ -10,36 +10,24 @@ use super::{BasicParseError, ParseError, Parser, ToCss, Token};
1010#[ cfg( feature = "serde" ) ]
1111use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
1212
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 ( ( ) ) ;
2319 }
2420
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 { " / " } ) ?;
4022
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. ;
4228 }
29+
30+ rounded_alpha. to_css ( dest)
4331}
4432
4533/// A color with red, green, blue, and alpha components, in a byte each.
@@ -56,15 +44,6 @@ pub struct RGBA {
5644 pub alpha : u8 ,
5745}
5846
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-
6847impl RGBA {
6948 /// Constructs a new RGBA value from float components. It expects the red,
7049 /// green, blue and alpha channels in that order, and all values will be
@@ -179,23 +158,23 @@ impl ToCss for RGBA {
179158 where
180159 W : fmt:: Write ,
181160 {
182- let serialize_alpha = self . alpha != 255 ;
161+ let has_alpha = self . alpha != 255 ;
183162
184- dest. write_str ( if serialize_alpha { "rgba(" } else { "rgb(" } ) ?;
163+ dest. write_str ( if has_alpha { "rgba(" } else { "rgb(" } ) ?;
185164 self . red . to_css ( dest) ?;
186165 dest. write_str ( ", " ) ?;
187166 self . green . to_css ( dest) ?;
188167 dest. write_str ( ", " ) ?;
189168 self . blue . to_css ( dest) ?;
190169
191- self . serialize_alpha ( dest) ?;
170+ serialize_alpha ( dest, self . alpha , true ) ?;
192171
193172 dest. write_char ( ')' )
194173 }
195174}
196175
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.
199178
200179/// Color specified by lightness, a- and b-axis components.
201180#[ derive( Clone , Copy , PartialEq , Debug ) ]
@@ -227,13 +206,6 @@ pub struct OKLAB {
227206
228207macro_rules! impl_lab_like {
229208 ( $cls: ident, $fname: literal) => {
230- impl WithAlpha for $cls {
231- #[ inline]
232- fn alpha( & self ) -> u8 {
233- self . alpha
234- }
235- }
236-
237209 impl $cls {
238210 /// Construct a new Lab color format with lightness, a, b and alpha components.
239211 pub fn new( lightness: f32 , a: f32 , b: f32 , alpha: u8 ) -> Self {
@@ -263,7 +235,7 @@ macro_rules! impl_lab_like {
263235 D : Deserializer <' de>,
264236 {
265237 let ( lightness, a, b, alpha) = Deserialize :: deserialize( deserializer) ?;
266- Ok ( LAB :: new( lightness, a, b, alpha) )
238+ Ok ( Self :: new( lightness, a, b, alpha) )
267239 }
268240 }
269241
@@ -275,13 +247,11 @@ macro_rules! impl_lab_like {
275247 dest. write_str( $fname) ?;
276248 dest. write_str( "(" ) ?;
277249 self . lightness. to_css( dest) ?;
278- dest. write_str ( " " ) ?;
250+ dest. write_char ( ' ' ) ?;
279251 self . a. to_css( dest) ?;
280- dest. write_str ( " " ) ?;
252+ dest. write_char ( ' ' ) ?;
281253 self . b. to_css( dest) ?;
282- if self . alpha( ) != 255 {
283- self . serialize_alpha( dest) ?;
284- }
254+ serialize_alpha( dest, self . alpha, false ) ?;
285255 dest. write_char( ')' )
286256 }
287257 }
@@ -291,8 +261,8 @@ macro_rules! impl_lab_like {
291261impl_lab_like ! ( LAB , "lab" ) ;
292262impl_lab_like ! ( OKLAB , "oklab" ) ;
293263
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.
296266
297267/// Color specified by lightness, chroma and hue components.
298268#[ derive( Clone , Copy , PartialEq , Debug ) ]
@@ -324,13 +294,6 @@ pub struct OKLCH {
324294
325295macro_rules! impl_lch_like {
326296 ( $cls: ident, $fname: literal) => {
327- impl WithAlpha for $cls {
328- #[ inline]
329- fn alpha( & self ) -> u8 {
330- self . alpha
331- }
332- }
333-
334297 impl $cls {
335298 /// Construct a new color with lightness, chroma and hue components.
336299 pub fn new( lightness: f32 , chroma: f32 , hue: f32 , alpha: u8 ) -> Self {
@@ -360,7 +323,7 @@ macro_rules! impl_lch_like {
360323 D : Deserializer <' de>,
361324 {
362325 let ( lightness, chroma, hue, alpha) = Deserialize :: deserialize( deserializer) ?;
363- Ok ( LCH :: new( lightness, chroma, hue, alpha) )
326+ Ok ( Self :: new( lightness, chroma, hue, alpha) )
364327 }
365328 }
366329
@@ -372,13 +335,11 @@ macro_rules! impl_lch_like {
372335 dest. write_str( $fname) ?;
373336 dest. write_str( "(" ) ?;
374337 self . lightness. to_css( dest) ?;
375- dest. write_str ( " " ) ?;
338+ dest. write_char ( ' ' ) ?;
376339 self . chroma. to_css( dest) ?;
377- dest. write_str ( " " ) ?;
340+ dest. write_char ( ' ' ) ?;
378341 self . hue. to_css( dest) ?;
379- if self . alpha( ) != 255 {
380- self . serialize_alpha( dest) ?;
381- }
342+ serialize_alpha( dest, self . alpha, false ) ?;
382343 dest. write_char( ')' )
383344 }
384345 }
@@ -414,11 +375,11 @@ impl AbsoluteColor {
414375 /// Return the alpha component of any of the schemes within.
415376 pub fn alpha ( & self ) -> u8 {
416377 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 ,
422383 }
423384 }
424385}
0 commit comments