Skip to content

Commit 02295d9

Browse files
committed
Change parse_hash_color to output a FromParsedColor.
1 parent 9bdf3bc commit 02295d9

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

src/color.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -843,30 +843,30 @@ pub trait FromParsedColor {
843843
/// Parse a color hash, without the leading '#' character.
844844
#[inline]
845845

846-
pub fn parse_hash_color<'i, 't, P>(value: &[u8]) -> Result<P::Output, ()>
846+
pub fn parse_hash_color<'i, 't, O>(value: &[u8]) -> Result<O, ()>
847847
where
848-
P: ColorParser<'i>,
848+
O: FromParsedColor,
849849
{
850850
Ok(match value.len() {
851-
8 => P::Output::from_rgba(
851+
8 => O::from_rgba(
852852
Some(from_hex(value[0])? * 16 + from_hex(value[1])?),
853853
Some(from_hex(value[2])? * 16 + from_hex(value[3])?),
854854
Some(from_hex(value[4])? * 16 + from_hex(value[5])?),
855855
Some((from_hex(value[6])? * 16 + from_hex(value[7])?) as f32 / 255.0),
856856
),
857-
6 => P::Output::from_rgba(
857+
6 => O::from_rgba(
858858
Some(from_hex(value[0])? * 16 + from_hex(value[1])?),
859859
Some(from_hex(value[2])? * 16 + from_hex(value[3])?),
860860
Some(from_hex(value[4])? * 16 + from_hex(value[5])?),
861861
Some(OPAQUE),
862862
),
863-
4 => P::Output::from_rgba(
863+
4 => O::from_rgba(
864864
Some(from_hex(value[0])? * 17),
865865
Some(from_hex(value[1])? * 17),
866866
Some(from_hex(value[2])? * 17),
867867
Some((from_hex(value[3])? * 17) as f32 / 255.0),
868868
),
869-
3 => P::Output::from_rgba(
869+
3 => O::from_rgba(
870870
Some(from_hex(value[0])? * 17),
871871
Some(from_hex(value[1])? * 17),
872872
Some(from_hex(value[2])? * 17),
@@ -888,9 +888,7 @@ where
888888
let location = input.current_source_location();
889889
let token = input.next()?;
890890
match *token {
891-
Token::Hash(ref value) | Token::IDHash(ref value) => {
892-
parse_hash_color::<P>(value.as_bytes())
893-
}
891+
Token::Hash(ref value) | Token::IDHash(ref value) => parse_hash_color(value.as_bytes()),
894892
Token::Ident(ref value) => parse_color_keyword(&*value),
895893
Token::Function(ref name) => {
896894
let name = name.clone();

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
6868
#![recursion_limit = "200"] // For color::parse_color_keyword
6969

7070
pub use crate::color::{
71-
hsl_to_rgb, hwb_to_rgb, parse_color_keyword, parse_color_with, AngleOrNumber, Color,
72-
ColorFunction, ColorParser, FromParsedColor, Lab, Lch, NumberOrPercentage, Oklab, Oklch,
71+
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,
7373
PredefinedColorSpace, RGBA,
7474
};
7575
pub use crate::cow_rc_str::CowRcStr;

src/tests.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1626,20 +1626,20 @@ fn generic_parser() {
16261626
("rgba(1, 2, 3, 0.4)", OutputType::Rgba(Some(1), Some(2), Some(3), Some(0.4))),
16271627
("rgb(none none none / none)", OutputType::Rgba(None, None, None, None)),
16281628
("rgb(1 none 3 / none)", OutputType::Rgba(Some(1), None, Some(3), None)),
1629-
1629+
16301630
("hsla(45deg, 20%, 30%, 0.4)", OutputType::Hsl(Some(45.0), Some(0.2), Some(0.3), Some(0.4))),
16311631
("hsl(45deg none none)", OutputType::Hsl(Some(45.0), None, None, Some(1.0))),
16321632
("hsl(none 10% none / none)", OutputType::Hsl(None, Some(0.1), None, None)),
16331633
("hsl(120 100.0% 50.0%)", OutputType::Hsl(Some(120.0), Some(1.0), Some(0.5), Some(1.0))),
1634-
1634+
16351635
("hwb(45deg 20% 30% / 0.4)", OutputType::Hwb(Some(45.0), Some(0.2), Some(0.3), Some(0.4))),
1636-
1636+
16371637
("lab(100 20 30 / 0.4)", OutputType::Lab(Some(100.0), Some(20.0), Some(30.0), Some(0.4))),
16381638
("lch(100 20 30 / 0.4)", OutputType::Lch(Some(100.0), Some(20.0), Some(30.0), Some(0.4))),
1639-
1639+
16401640
("oklab(100 20 30 / 0.4)", OutputType::Oklab(Some(100.0), Some(20.0), Some(30.0), Some(0.4))),
16411641
("oklch(100 20 30 / 0.4)", OutputType::Oklch(Some(100.0), Some(20.0), Some(30.0), Some(0.4))),
1642-
1642+
16431643
("color(srgb 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::Srgb, Some(0.1), Some(0.2), Some(0.3), Some(0.4))),
16441644
("color(srgb none none none)", OutputType::ColorFunction(PredefinedColorSpace::Srgb, None, None, None, Some(1.0))),
16451645
("color(srgb none none none / none)", OutputType::ColorFunction(PredefinedColorSpace::Srgb, None, None, None, None)),

0 commit comments

Comments
 (0)