22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5- use std:: f64:: consts:: PI ;
65use std:: fmt;
76
87use super :: { BasicParseError , ParseError , Parser , ToCss , Token } ;
@@ -103,12 +102,6 @@ impl<'de> Deserialize<'de> for RGBA {
103102
104103// NOTE: `RGBA` should not implement `ToCss` because it is an internal primitive that does not directly represent a CSS value.
105104
106- #[ inline]
107- fn f32_to_f64 ( f : f32 ) -> f64 {
108- const PRECISION : f64 = 1e6 ;
109- ( f64:: from ( f) * PRECISION ) . round ( ) / PRECISION
110- }
111-
112105/// <hue>
113106/// <https://w3c.github.io/csswg-drafts/css-color-4/#hue-syntax>
114107#[ derive( Clone , Copy , PartialEq , Debug ) ]
@@ -195,11 +188,11 @@ pub enum SrgbColor {
195188 /// <https://w3c.github.io/csswg-drafts/css-color-4/#numeric-srgb>
196189 Rgb {
197190 /// The red channel.
198- red : Option < f64 > ,
191+ red : Option < f32 > ,
199192 /// The green channel.
200- green : Option < f64 > ,
193+ green : Option < f32 > ,
201194 /// The blue channel.
202- blue : Option < f64 > ,
195+ blue : Option < f32 > ,
203196 /// The alpha channel.
204197 alpha : Option < AlphaValue > ,
205198 } ,
@@ -230,9 +223,9 @@ pub enum SrgbColor {
230223impl SrgbColor {
231224 /// Construct an sRGB color from its component channels.
232225 pub fn new (
233- red : Option < f64 > ,
234- green : Option < f64 > ,
235- blue : Option < f64 > ,
226+ red : Option < f32 > ,
227+ green : Option < f32 > ,
228+ blue : Option < f32 > ,
236229 alpha : Option < AlphaValue > ,
237230 ) -> Self {
238231 Self :: Rgb {
@@ -276,15 +269,15 @@ impl SrgbColor {
276269 /// Construct an sRGB color from channels represented as bytes.
277270 pub fn from_ints ( red : u8 , green : u8 , blue : u8 , alpha : u8 ) -> Self {
278271 Self :: new (
279- Some ( red as f64 / 255. ) ,
280- Some ( green as f64 / 255. ) ,
281- Some ( blue as f64 / 255. ) ,
272+ Some ( red as f32 / 255. ) ,
273+ Some ( green as f32 / 255. ) ,
274+ Some ( blue as f32 / 255. ) ,
282275 Some ( AlphaValue :: new ( alpha as f32 / 255. ) ) ,
283276 )
284277 }
285278
286279 /// Construct an sRGB color from channels represented as floats in the range 0 to 1.
287- pub fn from_floats ( red : f64 , green : f64 , blue : f64 , alpha : f32 ) -> Self {
280+ pub fn from_floats ( red : f32 , green : f32 , blue : f32 , alpha : f32 ) -> Self {
288281 Self :: new (
289282 Some ( red) ,
290283 Some ( green) ,
@@ -294,7 +287,7 @@ impl SrgbColor {
294287 }
295288
296289 /// Extract sRGB color channels, with missing components converted to zero.
297- pub fn to_floats ( self : Self ) -> ( f64 , f64 , f64 , f32 ) {
290+ pub fn to_floats ( self : Self ) -> ( f32 , f32 , f32 , f32 ) {
298291 match self {
299292 Self :: Rgb {
300293 red,
@@ -319,9 +312,9 @@ impl SrgbColor {
319312 lightness. unwrap_or ( 0. ) . clamp ( 0. , 1. ) ,
320313 ) ;
321314 (
322- f32_to_f64 ( r ) ,
323- f32_to_f64 ( g ) ,
324- f32_to_f64 ( b ) ,
315+ r ,
316+ g ,
317+ b ,
325318 alpha. unwrap_or ( AlphaValue :: new ( 0. ) ) . number . clamp ( 0. , 1. ) ,
326319 )
327320 }
@@ -337,9 +330,9 @@ impl SrgbColor {
337330 blackness. unwrap_or ( 0. ) . clamp ( 0. , 1. ) ,
338331 ) ;
339332 (
340- f32_to_f64 ( r ) ,
341- f32_to_f64 ( g ) ,
342- f32_to_f64 ( b ) ,
333+ r ,
334+ g ,
335+ b ,
343336 alpha. unwrap_or ( AlphaValue :: new ( 0. ) ) . number . clamp ( 0. , 1. ) ,
344337 )
345338 }
@@ -353,7 +346,7 @@ impl SrgbColor {
353346
354347 /// Convert an sRGB color to an RGBA color primitive.
355348 pub fn to_rgba ( self : Self ) -> RGBA {
356- let ( red, green, blue, alpha) : ( f64 , f64 , f64 , f32 ) = self . to_floats ( ) ;
349+ let ( red, green, blue, alpha) : ( f32 , f32 , f32 , f32 ) = self . to_floats ( ) ;
357350
358351 RGBA {
359352 red : ( red * 255. ) . round ( ) as u8 ,
@@ -371,7 +364,7 @@ impl ToCss for SrgbColor {
371364 where
372365 W : fmt:: Write ,
373366 {
374- let ( red, green, blue, alpha) : ( f64 , f64 , f64 , f32 ) = self . to_floats ( ) ;
367+ let ( red, green, blue, alpha) : ( f32 , f32 , f32 , f32 ) = self . to_floats ( ) ;
375368
376369 let serialize_alpha = alpha != 1. ;
377370
@@ -751,13 +744,10 @@ pub trait ColorComponentParser<'i> {
751744 Token :: Dimension {
752745 value : v, ref unit, ..
753746 } => {
754- // Expand f32 to f64 to avoid rounding errors during conversion.
755- let v = v as f64 ;
756-
757747 let degrees = match_ignore_ascii_case ! { & * unit,
758748 "deg" => v,
759- "grad" => ( v / 400. ) * 360. ,
760- "rad" => ( v / ( 2. * PI ) ) * 360. ,
749+ "grad" => v * 0.9 ,
750+ "rad" => v . to_degrees ( ) ,
761751 "turn" => v * 360. ,
762752 _ => return Err ( location. new_unexpected_token_error( Token :: Ident ( unit. clone( ) ) ) ) ,
763753 } ;
@@ -1187,15 +1177,15 @@ where
11871177 // Determine whether this is the number syntax or the percentage syntax.
11881178 arguments
11891179 . try_parse ( |input| {
1190- let red = Some ( component_parser. parse_number ( input) ? as f64 / 255. ) ;
1180+ let red = Some ( component_parser. parse_number ( input) ? / 255. ) ;
11911181
11921182 input. expect_comma ( ) ?;
11931183
1194- let green = Some ( component_parser. parse_number ( input) ? as f64 / 255. ) ;
1184+ let green = Some ( component_parser. parse_number ( input) ? / 255. ) ;
11951185
11961186 input. expect_comma ( ) ?;
11971187
1198- let blue = Some ( component_parser. parse_number ( input) ? as f64 / 255. ) ;
1188+ let blue = Some ( component_parser. parse_number ( input) ? / 255. ) ;
11991189
12001190 let alpha = parse_alpha_component ( component_parser, input, true ) ?;
12011191
@@ -1204,15 +1194,15 @@ where
12041194 ) ) )
12051195 } )
12061196 . or ( arguments. try_parse ( |input| {
1207- let red = Some ( f32_to_f64 ( component_parser. parse_percentage ( input) ?) ) ;
1197+ let red = Some ( component_parser. parse_percentage ( input) ?) ;
12081198
12091199 input. expect_comma ( ) ?;
12101200
1211- let green = Some ( f32_to_f64 ( component_parser. parse_percentage ( input) ?) ) ;
1201+ let green = Some ( component_parser. parse_percentage ( input) ?) ;
12121202
12131203 input. expect_comma ( ) ?;
12141204
1215- let blue = Some ( f32_to_f64 ( component_parser. parse_percentage ( input) ?) ) ;
1205+ let blue = Some ( component_parser. parse_percentage ( input) ?) ;
12161206
12171207 let alpha = parse_alpha_component ( component_parser, input, true ) ?;
12181208
@@ -1222,17 +1212,17 @@ where
12221212 } ) )
12231213 . or ( arguments. try_parse ( |input| {
12241214 let red = component_parser. parse_none_or (
1225- |component_parser, input| Ok ( component_parser. parse_number ( input) ? as f64 / 255. ) ,
1215+ |component_parser, input| Ok ( component_parser. parse_number ( input) ? / 255. ) ,
12261216 input,
12271217 ) ?;
12281218
12291219 let green = component_parser. parse_none_or (
1230- |component_parser, input| Ok ( component_parser. parse_number ( input) ? as f64 / 255. ) ,
1220+ |component_parser, input| Ok ( component_parser. parse_number ( input) ? / 255. ) ,
12311221 input,
12321222 ) ?;
12331223
12341224 let blue = component_parser. parse_none_or (
1235- |component_parser, input| Ok ( component_parser. parse_number ( input) ? as f64 / 255. ) ,
1225+ |component_parser, input| Ok ( component_parser. parse_number ( input) ? / 255. ) ,
12361226 input,
12371227 ) ?;
12381228
@@ -1244,17 +1234,17 @@ where
12441234 } ) )
12451235 . or ( arguments. try_parse ( |input| {
12461236 let red = component_parser. parse_none_or (
1247- |component_parser, input| Ok ( component_parser. parse_percentage ( input) ? as f64 ) ,
1237+ |component_parser, input| Ok ( component_parser. parse_percentage ( input) ?) ,
12481238 input,
12491239 ) ?;
12501240
12511241 let green = component_parser. parse_none_or (
1252- |component_parser, input| Ok ( component_parser. parse_percentage ( input) ? as f64 ) ,
1242+ |component_parser, input| Ok ( component_parser. parse_percentage ( input) ?) ,
12531243 input,
12541244 ) ?;
12551245
12561246 let blue = component_parser. parse_none_or (
1257- |component_parser, input| Ok ( component_parser. parse_percentage ( input) ? as f64 ) ,
1247+ |component_parser, input| Ok ( component_parser. parse_percentage ( input) ?) ,
12581248 input,
12591249 ) ?;
12601250
0 commit comments