Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address some clippy suggestions
  • Loading branch information
tiaanl committed Mar 11, 2023
commit a44c83a0ac81219daefc025726f97f082c007bb6
30 changes: 20 additions & 10 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub struct Hsl {
}

impl Hsl {
/// Construct a new HSL color from it's components.
pub fn new(
hue: Option<f32>,
saturation: Option<f32>,
Expand Down Expand Up @@ -229,6 +230,7 @@ pub struct Hwb {
}

impl Hwb {
/// Construct a new HWB color from it's components.
pub fn new(
hue: Option<f32>,
whiteness: Option<f32>,
Expand Down Expand Up @@ -717,12 +719,14 @@ pub trait ColorParser<'i> {
Token::Dimension {
value: v, ref unit, ..
} => {
let degrees = match_ignore_ascii_case! { &*unit,
let degrees = match_ignore_ascii_case! { unit,
"deg" => v,
"grad" => v * 360. / 400.,
"rad" => v * 360. / (2. * PI),
"turn" => v * 360.,
_ => return Err(location.new_unexpected_token_error(Token::Ident(unit.clone()))),
_ => {
return Err(location.new_unexpected_token_error(Token::Ident(unit.clone())))
}
};

AngleOrNumber::Angle { degrees }
Expand Down Expand Up @@ -775,7 +779,7 @@ impl Color {
/// Parse a <color> value, per CSS Color Module Level 3.
///
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, ParseError<'i, ()>> {
pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Color, ParseError<'i, ()>> {
parse_color_with(&DefaultColorParser, input)
}
}
Expand Down Expand Up @@ -844,7 +848,7 @@ pub trait FromParsedColor {

/// Parse a color hash, without the leading '#' character.
#[inline]
pub fn parse_hash_color<'i, 't, O>(value: &[u8]) -> Result<O, ()>
pub fn parse_hash_color<'i, O>(value: &[u8]) -> Result<O, ()>
where
O: FromParsedColor,
{
Expand Down Expand Up @@ -890,11 +894,11 @@ where
let token = input.next()?;
match *token {
Token::Hash(ref value) | Token::IDHash(ref value) => parse_hash_color(value.as_bytes()),
Token::Ident(ref value) => parse_color_keyword(&*value),
Token::Ident(ref value) => parse_color_keyword(value),
Token::Function(ref name) => {
let name = name.clone();
return input.parse_nested_block(|arguments| {
parse_color_function(color_parser, &*name, arguments)
parse_color_function(color_parser, &name, arguments)
});
}
_ => Err(()),
Expand Down Expand Up @@ -1433,7 +1437,7 @@ pub fn hwb_to_rgb(h: f32, w: f32, b: f32) -> (f32, f32, f32) {
/// except with h pre-multiplied by 3, to avoid some rounding errors.
#[inline]
pub fn hsl_to_rgb(hue: f32, saturation: f32, lightness: f32) -> (f32, f32, f32) {
debug_assert!(hue >= 0.0 && hue <= 1.0);
debug_assert!((0.0..=1.0).contains(&hue));

fn hue_to_rgb(m1: f32, m2: f32, mut h3: f32) -> f32 {
if h3 < 0. {
Expand Down Expand Up @@ -1465,13 +1469,16 @@ pub fn hsl_to_rgb(hue: f32, saturation: f32, lightness: f32) -> (f32, f32, f32)
(red, green, blue)
}

type IntoColorFn<Output> =
fn(l: Option<f32>, a: Option<f32>, b: Option<f32>, alpha: Option<f32>) -> Output;

#[inline]
fn parse_lab_like<'i, 't, P>(
color_parser: &P,
arguments: &mut Parser<'i, 't>,
lightness_range: f32,
a_b_range: f32,
into_color: fn(l: Option<f32>, a: Option<f32>, b: Option<f32>, alpha: Option<f32>) -> P::Output,
into_color: IntoColorFn<P::Output>,
) -> Result<P::Output, ParseError<'i, P::Error>>
where
P: ColorParser<'i>,
Expand All @@ -1497,7 +1504,7 @@ fn parse_lch_like<'i, 't, P>(
arguments: &mut Parser<'i, 't>,
lightness_range: f32,
chroma_range: f32,
into_color: fn(l: Option<f32>, c: Option<f32>, h: Option<f32>, alpha: Option<f32>) -> P::Output,
into_color: IntoColorFn<P::Output>,
) -> Result<P::Output, ParseError<'i, P::Error>>
where
P: ColorParser<'i>,
Expand Down Expand Up @@ -1555,14 +1562,17 @@ where
))
}

type ComponentParseResult<'i, R1, R2, R3, Error> =
Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<'i, Error>>;

/// Parse the color components and alpha with the modern [color-4] syntax.
pub fn parse_components<'i, 't, P, F1, F2, F3, R1, R2, R3>(
color_parser: &P,
input: &mut Parser<'i, 't>,
f1: F1,
f2: F2,
f3: F3,
) -> Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<'i, P::Error>>
) -> ComponentParseResult<'i, R1, R2, R3, P::Error>
where
P: ColorParser<'i>,
F1: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R1, ParseError<'i, P::Error>>,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)

pub use crate::color::{
hsl_to_rgb, hwb_to_rgb, parse_color_keyword, parse_color_with, parse_hash_color, AngleOrNumber,
Color, ColorFunction, ColorParser, FromParsedColor, Lab, Lch, NumberOrPercentage, Oklab, Oklch,
PredefinedColorSpace, RGBA,
Color, ColorFunction, ColorParser, FromParsedColor, Hsl, Hwb, Lab, Lch, NumberOrPercentage,
Oklab, Oklch, PredefinedColorSpace, RGBA,
};
pub use crate::cow_rc_str::CowRcStr;
pub use crate::from_bytes::{stylesheet_encoding, EncodingSupport};
Expand Down