2
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
4
5
- use std:: f64:: consts:: PI ;
6
5
use std:: fmt;
7
6
8
7
use super :: { BasicParseError , ParseError , Parser , ToCss , Token } ;
@@ -103,12 +102,6 @@ impl<'de> Deserialize<'de> for RGBA {
103
102
104
103
// NOTE: `RGBA` should not implement `ToCss` because it is an internal primitive that does not directly represent a CSS value.
105
104
106
- #[ inline]
107
- fn f32_to_f64 ( f : f32 ) -> f64 {
108
- const PRECISION : f64 = 1e6 ;
109
- ( f64:: from ( f) * PRECISION ) . round ( ) / PRECISION
110
- }
111
-
112
105
/// <hue>
113
106
/// <https://w3c.github.io/csswg-drafts/css-color-4/#hue-syntax>
114
107
#[ derive( Clone , Copy , PartialEq , Debug ) ]
@@ -195,11 +188,11 @@ pub enum SrgbColor {
195
188
/// <https://w3c.github.io/csswg-drafts/css-color-4/#numeric-srgb>
196
189
Rgb {
197
190
/// The red channel.
198
- red : Option < f64 > ,
191
+ red : Option < f32 > ,
199
192
/// The green channel.
200
- green : Option < f64 > ,
193
+ green : Option < f32 > ,
201
194
/// The blue channel.
202
- blue : Option < f64 > ,
195
+ blue : Option < f32 > ,
203
196
/// The alpha channel.
204
197
alpha : Option < AlphaValue > ,
205
198
} ,
@@ -230,9 +223,9 @@ pub enum SrgbColor {
230
223
impl SrgbColor {
231
224
/// Construct an sRGB color from its component channels.
232
225
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 > ,
236
229
alpha : Option < AlphaValue > ,
237
230
) -> Self {
238
231
Self :: Rgb {
@@ -276,15 +269,15 @@ impl SrgbColor {
276
269
/// Construct an sRGB color from channels represented as bytes.
277
270
pub fn from_ints ( red : u8 , green : u8 , blue : u8 , alpha : u8 ) -> Self {
278
271
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. ) ,
282
275
Some ( AlphaValue :: new ( alpha as f32 / 255. ) ) ,
283
276
)
284
277
}
285
278
286
279
/// 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 {
288
281
Self :: new (
289
282
Some ( red) ,
290
283
Some ( green) ,
@@ -294,7 +287,7 @@ impl SrgbColor {
294
287
}
295
288
296
289
/// 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 ) {
298
291
match self {
299
292
Self :: Rgb {
300
293
red,
@@ -319,9 +312,9 @@ impl SrgbColor {
319
312
lightness. unwrap_or ( 0. ) . clamp ( 0. , 1. ) ,
320
313
) ;
321
314
(
322
- f32_to_f64 ( r ) ,
323
- f32_to_f64 ( g ) ,
324
- f32_to_f64 ( b ) ,
315
+ r ,
316
+ g ,
317
+ b ,
325
318
alpha. unwrap_or ( AlphaValue :: new ( 0. ) ) . number . clamp ( 0. , 1. ) ,
326
319
)
327
320
}
@@ -337,9 +330,9 @@ impl SrgbColor {
337
330
blackness. unwrap_or ( 0. ) . clamp ( 0. , 1. ) ,
338
331
) ;
339
332
(
340
- f32_to_f64 ( r ) ,
341
- f32_to_f64 ( g ) ,
342
- f32_to_f64 ( b ) ,
333
+ r ,
334
+ g ,
335
+ b ,
343
336
alpha. unwrap_or ( AlphaValue :: new ( 0. ) ) . number . clamp ( 0. , 1. ) ,
344
337
)
345
338
}
@@ -353,7 +346,7 @@ impl SrgbColor {
353
346
354
347
/// Convert an sRGB color to an RGBA color primitive.
355
348
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 ( ) ;
357
350
358
351
RGBA {
359
352
red : ( red * 255. ) . round ( ) as u8 ,
@@ -371,7 +364,7 @@ impl ToCss for SrgbColor {
371
364
where
372
365
W : fmt:: Write ,
373
366
{
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 ( ) ;
375
368
376
369
let serialize_alpha = alpha != 1. ;
377
370
@@ -751,13 +744,10 @@ pub trait ColorComponentParser<'i> {
751
744
Token :: Dimension {
752
745
value : v, ref unit, ..
753
746
} => {
754
- // Expand f32 to f64 to avoid rounding errors during conversion.
755
- let v = v as f64 ;
756
-
757
747
let degrees = match_ignore_ascii_case ! { & * unit,
758
748
"deg" => v,
759
- "grad" => ( v / 400. ) * 360. ,
760
- "rad" => ( v / ( 2. * PI ) ) * 360. ,
749
+ "grad" => v * 0.9 ,
750
+ "rad" => v . to_degrees ( ) ,
761
751
"turn" => v * 360. ,
762
752
_ => return Err ( location. new_unexpected_token_error( Token :: Ident ( unit. clone( ) ) ) ) ,
763
753
} ;
@@ -1187,15 +1177,15 @@ where
1187
1177
// Determine whether this is the number syntax or the percentage syntax.
1188
1178
arguments
1189
1179
. 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. ) ;
1191
1181
1192
1182
input. expect_comma ( ) ?;
1193
1183
1194
- let green = Some ( component_parser. parse_number ( input) ? as f64 / 255. ) ;
1184
+ let green = Some ( component_parser. parse_number ( input) ? / 255. ) ;
1195
1185
1196
1186
input. expect_comma ( ) ?;
1197
1187
1198
- let blue = Some ( component_parser. parse_number ( input) ? as f64 / 255. ) ;
1188
+ let blue = Some ( component_parser. parse_number ( input) ? / 255. ) ;
1199
1189
1200
1190
let alpha = parse_alpha_component ( component_parser, input, true ) ?;
1201
1191
@@ -1204,15 +1194,15 @@ where
1204
1194
) ) )
1205
1195
} )
1206
1196
. 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) ?) ;
1208
1198
1209
1199
input. expect_comma ( ) ?;
1210
1200
1211
- let green = Some ( f32_to_f64 ( component_parser. parse_percentage ( input) ?) ) ;
1201
+ let green = Some ( component_parser. parse_percentage ( input) ?) ;
1212
1202
1213
1203
input. expect_comma ( ) ?;
1214
1204
1215
- let blue = Some ( f32_to_f64 ( component_parser. parse_percentage ( input) ?) ) ;
1205
+ let blue = Some ( component_parser. parse_percentage ( input) ?) ;
1216
1206
1217
1207
let alpha = parse_alpha_component ( component_parser, input, true ) ?;
1218
1208
@@ -1222,17 +1212,17 @@ where
1222
1212
} ) )
1223
1213
. or ( arguments. try_parse ( |input| {
1224
1214
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. ) ,
1226
1216
input,
1227
1217
) ?;
1228
1218
1229
1219
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. ) ,
1231
1221
input,
1232
1222
) ?;
1233
1223
1234
1224
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. ) ,
1236
1226
input,
1237
1227
) ?;
1238
1228
@@ -1244,17 +1234,17 @@ where
1244
1234
} ) )
1245
1235
. or ( arguments. try_parse ( |input| {
1246
1236
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) ?) ,
1248
1238
input,
1249
1239
) ?;
1250
1240
1251
1241
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) ?) ,
1253
1243
input,
1254
1244
) ?;
1255
1245
1256
1246
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) ?) ,
1258
1248
input,
1259
1249
) ?;
1260
1250
0 commit comments