From 8dd16259287f58f9273002717ec4d27e97127719 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:43:14 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- servo/components/style/values/specified/color.rs | 216 +---------------------- 1 file changed, 7 insertions(+), 209 deletions(-) (limited to 'servo/components/style/values/specified/color.rs') diff --git a/servo/components/style/values/specified/color.rs b/servo/components/style/values/specified/color.rs index f823ba7d30..289d89e44e 100644 --- a/servo/components/style/values/specified/color.rs +++ b/servo/components/style/values/specified/color.rs @@ -5,12 +5,8 @@ //! Specified color values. use super::AllowQuirks; -use crate::color::component::ColorComponent; -use crate::color::convert::normalize_hue; -use crate::color::parsing::{ - self, ColorParser, FromParsedColor, NumberOrAngle, NumberOrPercentage, -}; -use crate::color::{mix::ColorInterpolationMethod, AbsoluteColor, ColorSpace}; +use crate::color::mix::ColorInterpolationMethod; +use crate::color::{parsing, AbsoluteColor, ColorSpace}; use crate::media_queries::Device; use crate::parser::{Parse, ParserContext}; use crate::values::computed::{Color as ComputedColor, Context, ToComputedValue}; @@ -19,8 +15,7 @@ use crate::values::generics::color::{ }; use crate::values::specified::Percentage; use crate::values::{normalize, CustomIdent}; -use cssparser::color::OPAQUE; -use cssparser::{color::PredefinedColorSpace, BasicParseErrorKind, ParseErrorKind, Parser, Token}; +use cssparser::{BasicParseErrorKind, ParseErrorKind, Parser, Token}; use std::fmt::{self, Write}; use std::io::Write as IoWrite; use style_traits::{CssType, CssWriter, KeywordsCollectFn, ParseError, StyleParseErrorKind}; @@ -429,199 +424,6 @@ impl SystemColor { } } -impl From> for Option { - fn from(value: ColorComponent) -> Self { - match value { - ColorComponent::None => None, - ColorComponent::Value(value) => Some(value), - } - } -} - -impl ColorComponent { - #[inline] - fn into_alpha(self) -> Option { - match self { - ColorComponent::None => None, - ColorComponent::Value(number_or_percentage) => { - Some(normalize(number_or_percentage.to_number(1.0)).clamp(0.0, OPAQUE)) - }, - } - } -} - -impl FromParsedColor for Color { - fn from_current_color() -> Self { - Color::CurrentColor - } - - fn from_rgba( - red: ColorComponent, - green: ColorComponent, - blue: ColorComponent, - alpha: ColorComponent, - ) -> Self { - macro_rules! c { - ($c:expr) => {{ - match $c { - ColorComponent::None => 0u8, - ColorComponent::Value(value) => value, - } - }}; - } - - // Legacy rgb() doesn't support "none" alpha values and falls back to 0. - let alpha = alpha.into_alpha().unwrap_or(0.0); - - AbsoluteColor::srgb_legacy(c!(red), c!(green), c!(blue), alpha).into() - } - - fn from_hsl( - hue: ColorComponent, - saturation: ColorComponent, - lightness: ColorComponent, - alpha: ColorComponent, - ) -> Self { - // Percent reference range for S and L: 0% = 0.0, 100% = 100.0 - const LIGHTNESS_RANGE: f32 = 100.0; - const SATURATION_RANGE: f32 = 100.0; - - let hue = hue.map_value(|angle| normalize_hue(angle.degrees())); - let saturation = - saturation.map_value(|s| s.to_number(SATURATION_RANGE).clamp(0.0, SATURATION_RANGE)); - let lightness = - lightness.map_value(|l| l.to_number(LIGHTNESS_RANGE).clamp(0.0, LIGHTNESS_RANGE)); - - AbsoluteColor::new( - ColorSpace::Hsl, - hue, - saturation, - lightness, - alpha.into_alpha(), - ) - .into() - } - - fn from_hwb( - hue: ColorComponent, - whiteness: ColorComponent, - blackness: ColorComponent, - alpha: ColorComponent, - ) -> Self { - // Percent reference range for W and B: 0% = 0.0, 100% = 100.0 - const WHITENESS_RANGE: f32 = 100.0; - const BLACKNESS_RANGE: f32 = 100.0; - - let hue = hue.map_value(|angle| normalize_hue(angle.degrees())); - let whiteness = - whiteness.map_value(|w| w.to_number(WHITENESS_RANGE).clamp(0.0, WHITENESS_RANGE)); - let blackness = - blackness.map_value(|b| b.to_number(BLACKNESS_RANGE).clamp(0.0, BLACKNESS_RANGE)); - - AbsoluteColor::new( - ColorSpace::Hwb, - hue, - whiteness, - blackness, - alpha.into_alpha(), - ) - .into() - } - - fn from_lab( - lightness: ColorComponent, - a: ColorComponent, - b: ColorComponent, - alpha: ColorComponent, - ) -> Self { - // for L: 0% = 0.0, 100% = 100.0 - // for a and b: -100% = -125, 100% = 125 - const LIGHTNESS_RANGE: f32 = 100.0; - const A_B_RANGE: f32 = 125.0; - - let lightness = lightness.map_value(|l| l.to_number(LIGHTNESS_RANGE)); - let a = a.map_value(|a| a.to_number(A_B_RANGE)); - let b = b.map_value(|b| b.to_number(A_B_RANGE)); - - AbsoluteColor::new(ColorSpace::Lab, lightness, a, b, alpha.into_alpha()).into() - } - - fn from_lch( - lightness: ColorComponent, - chroma: ColorComponent, - hue: ColorComponent, - alpha: ColorComponent, - ) -> Self { - // for L: 0% = 0.0, 100% = 100.0 - // for C: 0% = 0, 100% = 150 - const LIGHTNESS_RANGE: f32 = 100.0; - const CHROMA_RANGE: f32 = 150.0; - - let lightness = lightness.map_value(|l| l.to_number(LIGHTNESS_RANGE)); - let chroma = chroma.map_value(|c| c.to_number(CHROMA_RANGE)); - let hue = hue.map_value(|angle| normalize_hue(angle.degrees())); - - AbsoluteColor::new(ColorSpace::Lch, lightness, chroma, hue, alpha.into_alpha()).into() - } - - fn from_oklab( - lightness: ColorComponent, - a: ColorComponent, - b: ColorComponent, - alpha: ColorComponent, - ) -> Self { - // for L: 0% = 0.0, 100% = 1.0 - // for a and b: -100% = -0.4, 100% = 0.4 - const LIGHTNESS_RANGE: f32 = 1.0; - const A_B_RANGE: f32 = 0.4; - - let lightness = lightness.map_value(|l| l.to_number(LIGHTNESS_RANGE)); - let a = a.map_value(|a| a.to_number(A_B_RANGE)); - let b = b.map_value(|b| b.to_number(A_B_RANGE)); - - AbsoluteColor::new(ColorSpace::Oklab, lightness, a, b, alpha.into_alpha()).into() - } - - fn from_oklch( - lightness: ColorComponent, - chroma: ColorComponent, - hue: ColorComponent, - alpha: ColorComponent, - ) -> Self { - // for L: 0% = 0.0, 100% = 1.0 - // for C: 0% = 0.0 100% = 0.4 - const LIGHTNESS_RANGE: f32 = 1.0; - const CHROMA_RANGE: f32 = 0.4; - - let lightness = lightness.map_value(|l| l.to_number(LIGHTNESS_RANGE)); - let chroma = chroma.map_value(|c| c.to_number(CHROMA_RANGE)); - let hue = hue.map_value(|angle| normalize_hue(angle.degrees())); - - AbsoluteColor::new( - ColorSpace::Oklch, - lightness, - chroma, - hue, - alpha.into_alpha(), - ) - .into() - } - - fn from_color_function( - color_space: PredefinedColorSpace, - c1: ColorComponent, - c2: ColorComponent, - c3: ColorComponent, - alpha: ColorComponent, - ) -> Self { - let c1 = c1.map_value(|c| c.to_number(1.0)); - let c2 = c2.map_value(|c| c.to_number(1.0)); - let c3 = c3.map_value(|c| c.to_number(1.0)); - - AbsoluteColor::new(color_space.into(), c1, c2, c3, alpha.into_alpha()).into() - } -} - /// Whether to preserve authored colors during parsing. That's useful only if we /// plan to serialize the color back. #[derive(Copy, Clone)] @@ -658,8 +460,7 @@ impl Color { }, }; - let color_parser = ColorParser { context: &context }; - match input.try_parse(|i| parsing::parse_color_with(&color_parser, i)) { + match input.try_parse(|i| parsing::parse_color_with(context, i)) { Ok(mut color) => { if let Color::Absolute(ref mut absolute) = color { // Because we can't set the `authored` value at construction time, we have to set it @@ -827,12 +628,9 @@ impl Color { loc: &cssparser::SourceLocation, ) -> Result> { match cssparser::color::parse_hash_color(bytes) { - Ok((r, g, b, a)) => Ok(Self::from_rgba( - r.into(), - g.into(), - b.into(), - ColorComponent::Value(NumberOrPercentage::Number { value: a }), - )), + Ok((r, g, b, a)) => Ok(Self::from_absolute_color(AbsoluteColor::srgb_legacy( + r, g, b, a, + ))), Err(()) => Err(loc.new_custom_error(StyleParseErrorKind::UnspecifiedError)), } } -- cgit v1.2.3