Skip to content

Commit 4582f86

Browse files
committed
Change parse_hash_color to output a FromParsedColor.
1 parent 9bdf3bc commit 4582f86

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

src/color.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -842,31 +842,30 @@ pub trait FromParsedColor {
842842

843843
/// Parse a color hash, without the leading '#' character.
844844
#[inline]
845-
846-
pub fn parse_hash_color<'i, 't, P>(value: &[u8]) -> Result<P::Output, ()>
845+
pub fn parse_hash_color<'i, 't, O>(value: &[u8]) -> Result<O, ()>
847846
where
848-
P: ColorParser<'i>,
847+
O: FromParsedColor,
849848
{
850849
Ok(match value.len() {
851-
8 => P::Output::from_rgba(
850+
8 => O::from_rgba(
852851
Some(from_hex(value[0])? * 16 + from_hex(value[1])?),
853852
Some(from_hex(value[2])? * 16 + from_hex(value[3])?),
854853
Some(from_hex(value[4])? * 16 + from_hex(value[5])?),
855854
Some((from_hex(value[6])? * 16 + from_hex(value[7])?) as f32 / 255.0),
856855
),
857-
6 => P::Output::from_rgba(
856+
6 => O::from_rgba(
858857
Some(from_hex(value[0])? * 16 + from_hex(value[1])?),
859858
Some(from_hex(value[2])? * 16 + from_hex(value[3])?),
860859
Some(from_hex(value[4])? * 16 + from_hex(value[5])?),
861860
Some(OPAQUE),
862861
),
863-
4 => P::Output::from_rgba(
862+
4 => O::from_rgba(
864863
Some(from_hex(value[0])? * 17),
865864
Some(from_hex(value[1])? * 17),
866865
Some(from_hex(value[2])? * 17),
867866
Some((from_hex(value[3])? * 17) as f32 / 255.0),
868867
),
869-
3 => P::Output::from_rgba(
868+
3 => O::from_rgba(
870869
Some(from_hex(value[0])? * 17),
871870
Some(from_hex(value[1])? * 17),
872871
Some(from_hex(value[2])? * 17),
@@ -888,9 +887,7 @@ where
888887
let location = input.current_source_location();
889888
let token = input.next()?;
890889
match *token {
891-
Token::Hash(ref value) | Token::IDHash(ref value) => {
892-
parse_hash_color::<P>(value.as_bytes())
893-
}
890+
Token::Hash(ref value) | Token::IDHash(ref value) => parse_hash_color(value.as_bytes()),
894891
Token::Ident(ref value) => parse_color_keyword(&*value),
895892
Token::Function(ref name) => {
896893
let name = name.clone();

src/lib.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 5 deletions
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)