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
+ // Allow text like <color> in docs.
6
+ #![ allow( rustdoc:: invalid_html_tags) ]
7
+
5
8
use std:: f32:: consts:: PI ;
6
9
use std:: fmt;
7
10
use std:: str:: FromStr ;
23
26
}
24
27
}
25
28
26
- /// https://drafts.csswg.org/css-color-4/#serializing-alpha-values
29
+ /// < https://drafts.csswg.org/css-color-4/#serializing-alpha-values>
27
30
#[ inline]
28
31
fn serialize_alpha (
29
32
dest : & mut impl fmt:: Write ,
@@ -53,14 +56,13 @@ fn serialize_alpha(
53
56
54
57
// Guaratees hue in [0..360)
55
58
fn normalize_hue ( hue : f32 ) -> f32 {
56
- // https://drafts.csswg.org/css-values/#angles
59
+ // < https://drafts.csswg.org/css-values/#angles>
57
60
// Subtract an integer before rounding, to avoid some rounding errors:
58
61
hue - 360.0 * ( hue / 360.0 ) . floor ( )
59
62
}
60
63
61
64
/// A color with red, green, blue, and alpha components, in a byte each.
62
65
#[ derive( Clone , Copy , PartialEq , Debug ) ]
63
- #[ repr( C ) ]
64
66
pub struct RGBA {
65
67
/// The red component.
66
68
pub red : Option < u8 > ,
@@ -150,6 +152,7 @@ impl ToCss for RGBA {
150
152
}
151
153
}
152
154
155
+ /// Color specified by hue, saturation and lightness components.
153
156
#[ derive( Clone , Copy , PartialEq , Debug ) ]
154
157
pub struct Hsl {
155
158
/// The hue component.
@@ -163,6 +166,7 @@ pub struct Hsl {
163
166
}
164
167
165
168
impl Hsl {
169
+ /// Construct a new HSL color from it's components.
166
170
pub fn new (
167
171
hue : Option < f32 > ,
168
172
saturation : Option < f32 > ,
@@ -215,6 +219,7 @@ impl<'de> Deserialize<'de> for Hsl {
215
219
}
216
220
}
217
221
222
+ /// Color specified by hue, whiteness and blackness components.
218
223
#[ derive( Clone , Copy , PartialEq , Debug ) ]
219
224
pub struct Hwb {
220
225
/// The hue component.
@@ -228,6 +233,7 @@ pub struct Hwb {
228
233
}
229
234
230
235
impl Hwb {
236
+ /// Construct a new HWB color from it's components.
231
237
pub fn new (
232
238
hue : Option < f32 > ,
233
239
whiteness : Option < f32 > ,
@@ -285,7 +291,6 @@ impl<'de> Deserialize<'de> for Hwb {
285
291
286
292
/// Color specified by lightness, a- and b-axis components.
287
293
#[ derive( Clone , Copy , PartialEq , Debug ) ]
288
- #[ repr( C ) ]
289
294
pub struct Lab {
290
295
/// The lightness component.
291
296
pub lightness : Option < f32 > ,
@@ -299,7 +304,6 @@ pub struct Lab {
299
304
300
305
/// Color specified by lightness, a- and b-axis components.
301
306
#[ derive( Clone , Copy , PartialEq , Debug ) ]
302
- #[ repr( C ) ]
303
307
pub struct Oklab {
304
308
/// The lightness component.
305
309
pub lightness : Option < f32 > ,
@@ -378,7 +382,6 @@ impl_lab_like!(Oklab, "oklab");
378
382
379
383
/// Color specified by lightness, chroma and hue components.
380
384
#[ derive( Clone , Copy , PartialEq , Debug ) ]
381
- #[ repr( C ) ]
382
385
pub struct Lch {
383
386
/// The lightness component.
384
387
pub lightness : Option < f32 > ,
@@ -392,7 +395,6 @@ pub struct Lch {
392
395
393
396
/// Color specified by lightness, chroma and hue components.
394
397
#[ derive( Clone , Copy , PartialEq , Debug ) ]
395
- #[ repr( C ) ]
396
398
pub struct Oklch {
397
399
/// The lightness component.
398
400
pub lightness : Option < f32 > ,
@@ -467,24 +469,24 @@ impl_lch_like!(Lch, "lch");
467
469
impl_lch_like ! ( Oklch , "oklch" ) ;
468
470
469
471
/// A Predefined color space specified in:
470
- /// https://drafts.csswg.org/css-color-4/#predefined
472
+ /// < https://drafts.csswg.org/css-color-4/#predefined>
471
473
#[ derive( Clone , Copy , PartialEq , Debug ) ]
472
474
pub enum PredefinedColorSpace {
473
- /// https://drafts.csswg.org/css-color-4/#predefined-sRGB
475
+ /// < https://drafts.csswg.org/css-color-4/#predefined-sRGB>
474
476
Srgb ,
475
- /// https://drafts.csswg.org/css-color-4/#predefined-sRGB-linear
477
+ /// < https://drafts.csswg.org/css-color-4/#predefined-sRGB-linear>
476
478
SrgbLinear ,
477
- /// https://drafts.csswg.org/css-color-4/#predefined-display-p3
479
+ /// < https://drafts.csswg.org/css-color-4/#predefined-display-p3>
478
480
DisplayP3 ,
479
- /// https://drafts.csswg.org/css-color-4/#predefined-a98-rgb
481
+ /// < https://drafts.csswg.org/css-color-4/#predefined-a98-rgb>
480
482
A98Rgb ,
481
- /// https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb
483
+ /// < https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb>
482
484
ProphotoRgb ,
483
- /// https://drafts.csswg.org/css-color-4/#predefined-rec2020
485
+ /// < https://drafts.csswg.org/css-color-4/#predefined-rec2020>
484
486
Rec2020 ,
485
- /// https://drafts.csswg.org/css-color-4/#predefined-xyz
487
+ /// < https://drafts.csswg.org/css-color-4/#predefined-xyz>
486
488
XyzD50 ,
487
- /// https://drafts.csswg.org/css-color-4/#predefined-xyz
489
+ /// < https://drafts.csswg.org/css-color-4/#predefined-xyz>
488
490
XyzD65 ,
489
491
}
490
492
@@ -533,7 +535,7 @@ impl ToCss for PredefinedColorSpace {
533
535
}
534
536
535
537
/// A color specified by the color() function.
536
- /// https://drafts.csswg.org/css-color-4/#color-function
538
+ /// < https://drafts.csswg.org/css-color-4/#color-function>
537
539
#[ derive( Clone , Copy , PartialEq , Debug ) ]
538
540
pub struct ColorFunction {
539
541
/// The color space for this color.
@@ -588,8 +590,13 @@ impl ToCss for ColorFunction {
588
590
}
589
591
}
590
592
591
- /// A <color> value.
592
- /// https://drafts.csswg.org/css-color-4/#color-type
593
+ /// Describes one of the value <color> values according to the CSS
594
+ /// specification.
595
+ ///
596
+ /// Most components are `Option<_>`, so when the value is `None`, that component
597
+ /// serializes to the "none" keyword.
598
+ ///
599
+ /// <https://drafts.csswg.org/css-color-4/#color-type>
593
600
#[ derive( Clone , Copy , PartialEq , Debug ) ]
594
601
pub enum Color {
595
602
/// The 'currentcolor' keyword.
@@ -715,12 +722,14 @@ pub trait ColorParser<'i> {
715
722
Token :: Dimension {
716
723
value : v, ref unit, ..
717
724
} => {
718
- let degrees = match_ignore_ascii_case ! { & * unit,
725
+ let degrees = match_ignore_ascii_case ! { unit,
719
726
"deg" => v,
720
727
"grad" => v * 360. / 400. ,
721
728
"rad" => v * 360. / ( 2. * PI ) ,
722
729
"turn" => v * 360. ,
723
- _ => return Err ( location. new_unexpected_token_error( Token :: Ident ( unit. clone( ) ) ) ) ,
730
+ _ => {
731
+ return Err ( location. new_unexpected_token_error( Token :: Ident ( unit. clone( ) ) ) )
732
+ }
724
733
} ;
725
734
726
735
AngleOrNumber :: Angle { degrees }
@@ -773,7 +782,7 @@ impl Color {
773
782
/// Parse a <color> value, per CSS Color Module Level 3.
774
783
///
775
784
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
776
- pub fn parse < ' i , ' t > ( input : & mut Parser < ' i , ' t > ) -> Result < Color , ParseError < ' i , ( ) > > {
785
+ pub fn parse < ' i > ( input : & mut Parser < ' i , ' _ > ) -> Result < Color , ParseError < ' i , ( ) > > {
777
786
parse_color_with ( & DefaultColorParser , input)
778
787
}
779
788
}
@@ -842,7 +851,7 @@ pub trait FromParsedColor {
842
851
843
852
/// Parse a color hash, without the leading '#' character.
844
853
#[ inline]
845
- pub fn parse_hash_color < ' i , ' t , O > ( value : & [ u8 ] ) -> Result < O , ( ) >
854
+ pub fn parse_hash_color < ' i , O > ( value : & [ u8 ] ) -> Result < O , ( ) >
846
855
where
847
856
O : FromParsedColor ,
848
857
{
@@ -875,8 +884,8 @@ where
875
884
} )
876
885
}
877
886
878
- /// Parse a CSS color with the specified [`ColorComponentParser `] and return a
879
- /// new color value on success.
887
+ /// Parse a CSS color using the specified [`ColorParser `] and return a new color
888
+ /// value on success.
880
889
pub fn parse_color_with < ' i , ' t , P > (
881
890
color_parser : & P ,
882
891
input : & mut Parser < ' i , ' t > ,
@@ -888,11 +897,11 @@ where
888
897
let token = input. next ( ) ?;
889
898
match * token {
890
899
Token :: Hash ( ref value) | Token :: IDHash ( ref value) => parse_hash_color ( value. as_bytes ( ) ) ,
891
- Token :: Ident ( ref value) => parse_color_keyword ( & * value) ,
900
+ Token :: Ident ( ref value) => parse_color_keyword ( value) ,
892
901
Token :: Function ( ref name) => {
893
902
let name = name. clone ( ) ;
894
903
return input. parse_nested_block ( |arguments| {
895
- parse_color_function ( color_parser, & * name, arguments)
904
+ parse_color_function ( color_parser, & name, arguments)
896
905
} ) ;
897
906
}
898
907
_ => Err ( ( ) ) ,
@@ -1173,12 +1182,12 @@ fn clamp_unit_f32(val: f32) -> u8 {
1173
1182
// Chrome does something similar for the alpha value, but not
1174
1183
// the rgb values.
1175
1184
//
1176
- // See https://bugzilla.mozilla.org/show_bug.cgi?id=1340484
1185
+ // See < https://bugzilla.mozilla.org/show_bug.cgi?id=1340484>
1177
1186
//
1178
1187
// Clamping to 256 and rounding after would let 1.0 map to 256, and
1179
1188
// `256.0_f32 as u8` is undefined behavior:
1180
1189
//
1181
- // https://github.com/rust-lang/rust/issues/10184
1190
+ // < https://github.com/rust-lang/rust/issues/10184>
1182
1191
clamp_floor_256_f32 ( val * 255. )
1183
1192
}
1184
1193
@@ -1347,7 +1356,7 @@ where
1347
1356
1348
1357
/// Parses hsl syntax.
1349
1358
///
1350
- /// https://drafts.csswg.org/css-color/#the-hsl-notation
1359
+ /// < https://drafts.csswg.org/css-color/#the-hsl-notation>
1351
1360
#[ inline]
1352
1361
fn parse_hsl < ' i , ' t , P > (
1353
1362
color_parser : & P ,
@@ -1386,7 +1395,7 @@ where
1386
1395
1387
1396
/// Parses hwb syntax.
1388
1397
///
1389
- /// https://drafts.csswg.org/css-color/#the-hbw-notation
1398
+ /// < https://drafts.csswg.org/css-color/#the-hbw-notation>
1390
1399
#[ inline]
1391
1400
fn parse_hwb < ' i , ' t , P > (
1392
1401
color_parser : & P ,
@@ -1410,7 +1419,7 @@ where
1410
1419
Ok ( P :: Output :: from_hwb ( hue, whiteness, blackness, alpha) )
1411
1420
}
1412
1421
1413
- /// https://drafts.csswg.org/css-color-4/#hwb-to-rgb
1422
+ /// < https://drafts.csswg.org/css-color-4/#hwb-to-rgb>
1414
1423
#[ inline]
1415
1424
pub fn hwb_to_rgb ( h : f32 , w : f32 , b : f32 ) -> ( f32 , f32 , f32 ) {
1416
1425
if w + b >= 1.0 {
@@ -1427,11 +1436,11 @@ pub fn hwb_to_rgb(h: f32, w: f32, b: f32) -> (f32, f32, f32) {
1427
1436
( red, green, blue)
1428
1437
}
1429
1438
1430
- /// https://drafts.csswg.org/css-color/#hsl-color
1439
+ /// < https://drafts.csswg.org/css-color/#hsl-color>
1431
1440
/// except with h pre-multiplied by 3, to avoid some rounding errors.
1432
1441
#[ inline]
1433
1442
pub fn hsl_to_rgb ( hue : f32 , saturation : f32 , lightness : f32 ) -> ( f32 , f32 , f32 ) {
1434
- debug_assert ! ( hue >= 0.0 && hue <= 1.0 ) ;
1443
+ debug_assert ! ( ( 0.0 ..= 1.0 ) . contains ( & hue ) ) ;
1435
1444
1436
1445
fn hue_to_rgb ( m1 : f32 , m2 : f32 , mut h3 : f32 ) -> f32 {
1437
1446
if h3 < 0. {
@@ -1463,13 +1472,16 @@ pub fn hsl_to_rgb(hue: f32, saturation: f32, lightness: f32) -> (f32, f32, f32)
1463
1472
( red, green, blue)
1464
1473
}
1465
1474
1475
+ type IntoColorFn < Output > =
1476
+ fn ( l : Option < f32 > , a : Option < f32 > , b : Option < f32 > , alpha : Option < f32 > ) -> Output ;
1477
+
1466
1478
#[ inline]
1467
1479
fn parse_lab_like < ' i , ' t , P > (
1468
1480
color_parser : & P ,
1469
1481
arguments : & mut Parser < ' i , ' t > ,
1470
1482
lightness_range : f32 ,
1471
1483
a_b_range : f32 ,
1472
- into_color : fn ( l : Option < f32 > , a : Option < f32 > , b : Option < f32 > , alpha : Option < f32 > ) -> P :: Output ,
1484
+ into_color : IntoColorFn < P :: Output > ,
1473
1485
) -> Result < P :: Output , ParseError < ' i , P :: Error > >
1474
1486
where
1475
1487
P : ColorParser < ' i > ,
@@ -1495,7 +1507,7 @@ fn parse_lch_like<'i, 't, P>(
1495
1507
arguments : & mut Parser < ' i , ' t > ,
1496
1508
lightness_range : f32 ,
1497
1509
chroma_range : f32 ,
1498
- into_color : fn ( l : Option < f32 > , c : Option < f32 > , h : Option < f32 > , alpha : Option < f32 > ) -> P :: Output ,
1510
+ into_color : IntoColorFn < P :: Output > ,
1499
1511
) -> Result < P :: Output , ParseError < ' i , P :: Error > >
1500
1512
where
1501
1513
P : ColorParser < ' i > ,
@@ -1553,14 +1565,17 @@ where
1553
1565
) )
1554
1566
}
1555
1567
1568
+ type ComponentParseResult < ' i , R1 , R2 , R3 , Error > =
1569
+ Result < ( Option < R1 > , Option < R2 > , Option < R3 > , Option < f32 > ) , ParseError < ' i , Error > > ;
1570
+
1556
1571
/// Parse the color components and alpha with the modern [color-4] syntax.
1557
1572
pub fn parse_components < ' i , ' t , P , F1 , F2 , F3 , R1 , R2 , R3 > (
1558
1573
color_parser : & P ,
1559
1574
input : & mut Parser < ' i , ' t > ,
1560
1575
f1 : F1 ,
1561
1576
f2 : F2 ,
1562
1577
f3 : F3 ,
1563
- ) -> Result < ( Option < R1 > , Option < R2 > , Option < R3 > , Option < f32 > ) , ParseError < ' i , P :: Error > >
1578
+ ) -> ComponentParseResult < ' i , R1 , R2 , R3 , P :: Error >
1564
1579
where
1565
1580
P : ColorParser < ' i > ,
1566
1581
F1 : FnOnce ( & P , & mut Parser < ' i , ' t > ) -> Result < R1 , ParseError < ' i , P :: Error > > ,
0 commit comments