summaryrefslogtreecommitdiffstats
path: root/servo/components/style/values/specified/color.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:43:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:43:14 +0000
commit8dd16259287f58f9273002717ec4d27e97127719 (patch)
tree3863e62a53829a84037444beab3abd4ed9dfc7d0 /servo/components/style/values/specified/color.rs
parentReleasing progress-linux version 126.0.1-1~progress7.99u1. (diff)
downloadfirefox-8dd16259287f58f9273002717ec4d27e97127719.tar.xz
firefox-8dd16259287f58f9273002717ec4d27e97127719.zip
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'servo/components/style/values/specified/color.rs')
-rw-r--r--servo/components/style/values/specified/color.rs216
1 files changed, 7 insertions, 209 deletions
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<T> From<ColorComponent<T>> for Option<T> {
- fn from(value: ColorComponent<T>) -> Self {
- match value {
- ColorComponent::None => None,
- ColorComponent::Value(value) => Some(value),
- }
- }
-}
-
-impl ColorComponent<NumberOrPercentage> {
- #[inline]
- fn into_alpha(self) -> Option<f32> {
- 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<u8>,
- green: ColorComponent<u8>,
- blue: ColorComponent<u8>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrAngle>,
- saturation: ColorComponent<NumberOrPercentage>,
- lightness: ColorComponent<NumberOrPercentage>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrAngle>,
- whiteness: ColorComponent<NumberOrPercentage>,
- blackness: ColorComponent<NumberOrPercentage>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrPercentage>,
- a: ColorComponent<NumberOrPercentage>,
- b: ColorComponent<NumberOrPercentage>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrPercentage>,
- chroma: ColorComponent<NumberOrPercentage>,
- hue: ColorComponent<NumberOrAngle>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrPercentage>,
- a: ColorComponent<NumberOrPercentage>,
- b: ColorComponent<NumberOrPercentage>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrPercentage>,
- chroma: ColorComponent<NumberOrPercentage>,
- hue: ColorComponent<NumberOrAngle>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<NumberOrPercentage>,
- c2: ColorComponent<NumberOrPercentage>,
- c3: ColorComponent<NumberOrPercentage>,
- alpha: ColorComponent<NumberOrPercentage>,
- ) -> 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<Self, ParseError<'i>> {
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)),
}
}