1414/// The opaque alpha value of 1.0.
1515pub const OPAQUE : f32 = 1.0 ;
1616
17- use crate :: ToCss ;
17+ use crate :: { BasicParseError , Parser , ToCss , Token } ;
1818use std:: fmt;
19- use std:: str:: FromStr ;
2019
2120/// Clamp a 0..1 number to a 0..255 range to u8.
2221///
@@ -99,36 +98,21 @@ pub enum PredefinedColorSpace {
9998}
10099
101100impl PredefinedColorSpace {
102- /// Returns the string value of the predefined color space.
103- pub fn as_str ( & self ) -> & str {
104- match self {
105- PredefinedColorSpace :: Srgb => "srgb" ,
106- PredefinedColorSpace :: SrgbLinear => "srgb-linear" ,
107- PredefinedColorSpace :: DisplayP3 => "display-p3" ,
108- PredefinedColorSpace :: A98Rgb => "a98-rgb" ,
109- PredefinedColorSpace :: ProphotoRgb => "prophoto-rgb" ,
110- PredefinedColorSpace :: Rec2020 => "rec2020" ,
111- PredefinedColorSpace :: XyzD50 => "xyz-d50" ,
112- PredefinedColorSpace :: XyzD65 => "xyz-d65" ,
113- }
114- }
115- }
116-
117- impl FromStr for PredefinedColorSpace {
118- type Err = ( ) ;
101+ /// Parse a PredefinedColorSpace from the given input.
102+ pub fn parse < ' i > ( input : & mut Parser < ' i , ' _ > ) -> Result < Self , BasicParseError < ' i > > {
103+ let location = input. current_source_location ( ) ;
119104
120- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
121- Ok ( match_ignore_ascii_case ! { s,
122- "srgb" => PredefinedColorSpace :: Srgb ,
123- "srgb-linear" => PredefinedColorSpace :: SrgbLinear ,
124- "display-p3" => PredefinedColorSpace :: DisplayP3 ,
125- "a98-rgb" => PredefinedColorSpace :: A98Rgb ,
126- "prophoto-rgb" => PredefinedColorSpace :: ProphotoRgb ,
127- "rec2020" => PredefinedColorSpace :: Rec2020 ,
128- "xyz-d50" => PredefinedColorSpace :: XyzD50 ,
129- "xyz" | "xyz-d65" => PredefinedColorSpace :: XyzD65 ,
130-
131- _ => return Err ( ( ) ) ,
105+ let ident = input. expect_ident ( ) ?;
106+ Ok ( match_ignore_ascii_case ! { ident,
107+ "srgb" => Self :: Srgb ,
108+ "srgb-linear" => Self :: SrgbLinear ,
109+ "display-p3" => Self :: DisplayP3 ,
110+ "a98-rgb" => Self :: A98Rgb ,
111+ "prophoto-rgb" => Self :: ProphotoRgb ,
112+ "rec2020" => Self :: Rec2020 ,
113+ "xyz-d50" => Self :: XyzD50 ,
114+ "xyz" | "xyz-d65" => Self :: XyzD65 ,
115+ _ => return Err ( location. new_basic_unexpected_token_error( Token :: Ident ( ident. clone( ) ) ) ) ,
132116 } )
133117 }
134118}
@@ -138,7 +122,16 @@ impl ToCss for PredefinedColorSpace {
138122 where
139123 W : fmt:: Write ,
140124 {
141- dest. write_str ( self . as_str ( ) )
125+ dest. write_str ( match self {
126+ Self :: Srgb => "srgb" ,
127+ Self :: SrgbLinear => "srgb-linear" ,
128+ Self :: DisplayP3 => "display-p3" ,
129+ Self :: A98Rgb => "a98-rgb" ,
130+ Self :: ProphotoRgb => "prophoto-rgb" ,
131+ Self :: Rec2020 => "rec2020" ,
132+ Self :: XyzD50 => "xyz-d50" ,
133+ Self :: XyzD65 => "xyz-d65" ,
134+ } )
142135 }
143136}
144137
0 commit comments