Skip to content

Commit 53ace36

Browse files
authored
Auto merge of servo#329 - tiaanl:next, r=emilio
Cleanup for crates.io release - Add documentation where needed. - Remove #[repr(C)] from structs. These are not needed any more and doesn't really do anything now that the types are Option<_>. - Fix links so that they work in docs. - Small clippy fixes. @emilio For publishing all the new color changes as 0.30 to crates.io, any other suggestions on what to clean up?
2 parents e88763d + 51cb57c commit 53ace36

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

src/color.rs

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
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+
// Allow text like <color> in docs.
6+
#![allow(rustdoc::invalid_html_tags)]
7+
58
use std::f32::consts::PI;
69
use std::fmt;
710
use std::str::FromStr;
@@ -23,7 +26,7 @@ where
2326
}
2427
}
2528

26-
/// https://drafts.csswg.org/css-color-4/#serializing-alpha-values
29+
/// <https://drafts.csswg.org/css-color-4/#serializing-alpha-values>
2730
#[inline]
2831
fn serialize_alpha(
2932
dest: &mut impl fmt::Write,
@@ -53,14 +56,13 @@ fn serialize_alpha(
5356

5457
// Guaratees hue in [0..360)
5558
fn normalize_hue(hue: f32) -> f32 {
56-
// https://drafts.csswg.org/css-values/#angles
59+
// <https://drafts.csswg.org/css-values/#angles>
5760
// Subtract an integer before rounding, to avoid some rounding errors:
5861
hue - 360.0 * (hue / 360.0).floor()
5962
}
6063

6164
/// A color with red, green, blue, and alpha components, in a byte each.
6265
#[derive(Clone, Copy, PartialEq, Debug)]
63-
#[repr(C)]
6466
pub struct RGBA {
6567
/// The red component.
6668
pub red: Option<u8>,
@@ -150,6 +152,7 @@ impl ToCss for RGBA {
150152
}
151153
}
152154

155+
/// Color specified by hue, saturation and lightness components.
153156
#[derive(Clone, Copy, PartialEq, Debug)]
154157
pub struct Hsl {
155158
/// The hue component.
@@ -163,6 +166,7 @@ pub struct Hsl {
163166
}
164167

165168
impl Hsl {
169+
/// Construct a new HSL color from it's components.
166170
pub fn new(
167171
hue: Option<f32>,
168172
saturation: Option<f32>,
@@ -215,6 +219,7 @@ impl<'de> Deserialize<'de> for Hsl {
215219
}
216220
}
217221

222+
/// Color specified by hue, whiteness and blackness components.
218223
#[derive(Clone, Copy, PartialEq, Debug)]
219224
pub struct Hwb {
220225
/// The hue component.
@@ -228,6 +233,7 @@ pub struct Hwb {
228233
}
229234

230235
impl Hwb {
236+
/// Construct a new HWB color from it's components.
231237
pub fn new(
232238
hue: Option<f32>,
233239
whiteness: Option<f32>,
@@ -285,7 +291,6 @@ impl<'de> Deserialize<'de> for Hwb {
285291

286292
/// Color specified by lightness, a- and b-axis components.
287293
#[derive(Clone, Copy, PartialEq, Debug)]
288-
#[repr(C)]
289294
pub struct Lab {
290295
/// The lightness component.
291296
pub lightness: Option<f32>,
@@ -299,7 +304,6 @@ pub struct Lab {
299304

300305
/// Color specified by lightness, a- and b-axis components.
301306
#[derive(Clone, Copy, PartialEq, Debug)]
302-
#[repr(C)]
303307
pub struct Oklab {
304308
/// The lightness component.
305309
pub lightness: Option<f32>,
@@ -378,7 +382,6 @@ impl_lab_like!(Oklab, "oklab");
378382

379383
/// Color specified by lightness, chroma and hue components.
380384
#[derive(Clone, Copy, PartialEq, Debug)]
381-
#[repr(C)]
382385
pub struct Lch {
383386
/// The lightness component.
384387
pub lightness: Option<f32>,
@@ -392,7 +395,6 @@ pub struct Lch {
392395

393396
/// Color specified by lightness, chroma and hue components.
394397
#[derive(Clone, Copy, PartialEq, Debug)]
395-
#[repr(C)]
396398
pub struct Oklch {
397399
/// The lightness component.
398400
pub lightness: Option<f32>,
@@ -467,24 +469,24 @@ impl_lch_like!(Lch, "lch");
467469
impl_lch_like!(Oklch, "oklch");
468470

469471
/// A Predefined color space specified in:
470-
/// https://drafts.csswg.org/css-color-4/#predefined
472+
/// <https://drafts.csswg.org/css-color-4/#predefined>
471473
#[derive(Clone, Copy, PartialEq, Debug)]
472474
pub enum PredefinedColorSpace {
473-
/// https://drafts.csswg.org/css-color-4/#predefined-sRGB
475+
/// <https://drafts.csswg.org/css-color-4/#predefined-sRGB>
474476
Srgb,
475-
/// https://drafts.csswg.org/css-color-4/#predefined-sRGB-linear
477+
/// <https://drafts.csswg.org/css-color-4/#predefined-sRGB-linear>
476478
SrgbLinear,
477-
/// https://drafts.csswg.org/css-color-4/#predefined-display-p3
479+
/// <https://drafts.csswg.org/css-color-4/#predefined-display-p3>
478480
DisplayP3,
479-
/// https://drafts.csswg.org/css-color-4/#predefined-a98-rgb
481+
/// <https://drafts.csswg.org/css-color-4/#predefined-a98-rgb>
480482
A98Rgb,
481-
/// https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb
483+
/// <https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb>
482484
ProphotoRgb,
483-
/// https://drafts.csswg.org/css-color-4/#predefined-rec2020
485+
/// <https://drafts.csswg.org/css-color-4/#predefined-rec2020>
484486
Rec2020,
485-
/// https://drafts.csswg.org/css-color-4/#predefined-xyz
487+
/// <https://drafts.csswg.org/css-color-4/#predefined-xyz>
486488
XyzD50,
487-
/// https://drafts.csswg.org/css-color-4/#predefined-xyz
489+
/// <https://drafts.csswg.org/css-color-4/#predefined-xyz>
488490
XyzD65,
489491
}
490492

@@ -533,7 +535,7 @@ impl ToCss for PredefinedColorSpace {
533535
}
534536

535537
/// 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>
537539
#[derive(Clone, Copy, PartialEq, Debug)]
538540
pub struct ColorFunction {
539541
/// The color space for this color.
@@ -588,8 +590,13 @@ impl ToCss for ColorFunction {
588590
}
589591
}
590592

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>
593600
#[derive(Clone, Copy, PartialEq, Debug)]
594601
pub enum Color {
595602
/// The 'currentcolor' keyword.
@@ -715,12 +722,14 @@ pub trait ColorParser<'i> {
715722
Token::Dimension {
716723
value: v, ref unit, ..
717724
} => {
718-
let degrees = match_ignore_ascii_case! { &*unit,
725+
let degrees = match_ignore_ascii_case! { unit,
719726
"deg" => v,
720727
"grad" => v * 360. / 400.,
721728
"rad" => v * 360. / (2. * PI),
722729
"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+
}
724733
};
725734

726735
AngleOrNumber::Angle { degrees }
@@ -773,7 +782,7 @@ impl Color {
773782
/// Parse a <color> value, per CSS Color Module Level 3.
774783
///
775784
/// 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, ()>> {
777786
parse_color_with(&DefaultColorParser, input)
778787
}
779788
}
@@ -842,7 +851,7 @@ pub trait FromParsedColor {
842851

843852
/// Parse a color hash, without the leading '#' character.
844853
#[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, ()>
846855
where
847856
O: FromParsedColor,
848857
{
@@ -875,8 +884,8 @@ where
875884
})
876885
}
877886

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.
880889
pub fn parse_color_with<'i, 't, P>(
881890
color_parser: &P,
882891
input: &mut Parser<'i, 't>,
@@ -888,11 +897,11 @@ where
888897
let token = input.next()?;
889898
match *token {
890899
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),
892901
Token::Function(ref name) => {
893902
let name = name.clone();
894903
return input.parse_nested_block(|arguments| {
895-
parse_color_function(color_parser, &*name, arguments)
904+
parse_color_function(color_parser, &name, arguments)
896905
});
897906
}
898907
_ => Err(()),
@@ -1173,12 +1182,12 @@ fn clamp_unit_f32(val: f32) -> u8 {
11731182
// Chrome does something similar for the alpha value, but not
11741183
// the rgb values.
11751184
//
1176-
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1340484
1185+
// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1340484>
11771186
//
11781187
// Clamping to 256 and rounding after would let 1.0 map to 256, and
11791188
// `256.0_f32 as u8` is undefined behavior:
11801189
//
1181-
// https://github.com/rust-lang/rust/issues/10184
1190+
// <https://github.com/rust-lang/rust/issues/10184>
11821191
clamp_floor_256_f32(val * 255.)
11831192
}
11841193

@@ -1347,7 +1356,7 @@ where
13471356

13481357
/// Parses hsl syntax.
13491358
///
1350-
/// https://drafts.csswg.org/css-color/#the-hsl-notation
1359+
/// <https://drafts.csswg.org/css-color/#the-hsl-notation>
13511360
#[inline]
13521361
fn parse_hsl<'i, 't, P>(
13531362
color_parser: &P,
@@ -1386,7 +1395,7 @@ where
13861395

13871396
/// Parses hwb syntax.
13881397
///
1389-
/// https://drafts.csswg.org/css-color/#the-hbw-notation
1398+
/// <https://drafts.csswg.org/css-color/#the-hbw-notation>
13901399
#[inline]
13911400
fn parse_hwb<'i, 't, P>(
13921401
color_parser: &P,
@@ -1410,7 +1419,7 @@ where
14101419
Ok(P::Output::from_hwb(hue, whiteness, blackness, alpha))
14111420
}
14121421

1413-
/// https://drafts.csswg.org/css-color-4/#hwb-to-rgb
1422+
/// <https://drafts.csswg.org/css-color-4/#hwb-to-rgb>
14141423
#[inline]
14151424
pub fn hwb_to_rgb(h: f32, w: f32, b: f32) -> (f32, f32, f32) {
14161425
if w + b >= 1.0 {
@@ -1427,11 +1436,11 @@ pub fn hwb_to_rgb(h: f32, w: f32, b: f32) -> (f32, f32, f32) {
14271436
(red, green, blue)
14281437
}
14291438

1430-
/// https://drafts.csswg.org/css-color/#hsl-color
1439+
/// <https://drafts.csswg.org/css-color/#hsl-color>
14311440
/// except with h pre-multiplied by 3, to avoid some rounding errors.
14321441
#[inline]
14331442
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));
14351444

14361445
fn hue_to_rgb(m1: f32, m2: f32, mut h3: f32) -> f32 {
14371446
if h3 < 0. {
@@ -1463,13 +1472,16 @@ pub fn hsl_to_rgb(hue: f32, saturation: f32, lightness: f32) -> (f32, f32, f32)
14631472
(red, green, blue)
14641473
}
14651474

1475+
type IntoColorFn<Output> =
1476+
fn(l: Option<f32>, a: Option<f32>, b: Option<f32>, alpha: Option<f32>) -> Output;
1477+
14661478
#[inline]
14671479
fn parse_lab_like<'i, 't, P>(
14681480
color_parser: &P,
14691481
arguments: &mut Parser<'i, 't>,
14701482
lightness_range: f32,
14711483
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>,
14731485
) -> Result<P::Output, ParseError<'i, P::Error>>
14741486
where
14751487
P: ColorParser<'i>,
@@ -1495,7 +1507,7 @@ fn parse_lch_like<'i, 't, P>(
14951507
arguments: &mut Parser<'i, 't>,
14961508
lightness_range: f32,
14971509
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>,
14991511
) -> Result<P::Output, ParseError<'i, P::Error>>
15001512
where
15011513
P: ColorParser<'i>,
@@ -1553,14 +1565,17 @@ where
15531565
))
15541566
}
15551567

1568+
type ComponentParseResult<'i, R1, R2, R3, Error> =
1569+
Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<'i, Error>>;
1570+
15561571
/// Parse the color components and alpha with the modern [color-4] syntax.
15571572
pub fn parse_components<'i, 't, P, F1, F2, F3, R1, R2, R3>(
15581573
color_parser: &P,
15591574
input: &mut Parser<'i, 't>,
15601575
f1: F1,
15611576
f2: F2,
15621577
f3: F3,
1563-
) -> Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<'i, P::Error>>
1578+
) -> ComponentParseResult<'i, R1, R2, R3, P::Error>
15641579
where
15651580
P: ColorParser<'i>,
15661581
F1: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R1, ParseError<'i, P::Error>>,

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
6969

7070
pub use crate::color::{
7171
hsl_to_rgb, hwb_to_rgb, parse_color_keyword, parse_color_with, parse_hash_color, AngleOrNumber,
72-
Color, ColorFunction, ColorParser, FromParsedColor, Lab, Lch, NumberOrPercentage, Oklab, Oklch,
73-
PredefinedColorSpace, RGBA,
72+
Color, ColorFunction, ColorParser, FromParsedColor, Hsl, Hwb, Lab, Lch, NumberOrPercentage,
73+
Oklab, Oklch, PredefinedColorSpace, RGBA,
7474
};
7575
pub use crate::cow_rc_str::CowRcStr;
7676
pub use crate::from_bytes::{stylesheet_encoding, EncodingSupport};

0 commit comments

Comments
 (0)