diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:35:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:35:29 +0000 |
commit | 59203c63bb777a3bacec32fb8830fba33540e809 (patch) | |
tree | 58298e711c0ff0575818c30485b44a2f21bf28a0 /servo/components/style/color/mod.rs | |
parent | Adding upstream version 126.0.1. (diff) | |
download | firefox-59203c63bb777a3bacec32fb8830fba33540e809.tar.xz firefox-59203c63bb777a3bacec32fb8830fba33540e809.zip |
Adding upstream version 127.0.upstream/127.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'servo/components/style/color/mod.rs')
-rw-r--r-- | servo/components/style/color/mod.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/servo/components/style/color/mod.rs b/servo/components/style/color/mod.rs index f7e661cddf..7e427faa84 100644 --- a/servo/components/style/color/mod.rs +++ b/servo/components/style/color/mod.rs @@ -15,6 +15,7 @@ pub mod mix; pub mod parsing; mod to_css; +use self::parsing::ChannelKeyword; use component::ColorComponent; use cssparser::color::PredefinedColorSpace; @@ -449,6 +450,76 @@ impl AbsoluteColor { } } + /// Return the value of a component by its channel keyword. + pub fn get_component_by_channel_keyword( + &self, + channel_keyword: ChannelKeyword, + ) -> Result<Option<f32>, ()> { + if channel_keyword == ChannelKeyword::Alpha { + return Ok(self.alpha()); + } + + Ok(match self.color_space { + ColorSpace::Srgb => { + if self.flags.contains(ColorFlags::IS_LEGACY_SRGB) { + match channel_keyword { + ChannelKeyword::R => self.c0().map(|v| v * 255.0), + ChannelKeyword::G => self.c1().map(|v| v * 255.0), + ChannelKeyword::B => self.c2().map(|v| v * 255.0), + _ => return Err(()), + } + } else { + match channel_keyword { + ChannelKeyword::R => self.c0(), + ChannelKeyword::G => self.c1(), + ChannelKeyword::B => self.c2(), + _ => return Err(()), + } + } + }, + ColorSpace::Hsl => match channel_keyword { + ChannelKeyword::H => self.c0(), + ChannelKeyword::S => self.c1(), + ChannelKeyword::L => self.c2(), + _ => return Err(()), + }, + ColorSpace::Hwb => match channel_keyword { + ChannelKeyword::H => self.c0(), + ChannelKeyword::W => self.c1(), + ChannelKeyword::B => self.c2(), + _ => return Err(()), + }, + ColorSpace::Lab | ColorSpace::Oklab => match channel_keyword { + ChannelKeyword::L => self.c0(), + ChannelKeyword::A => self.c1(), + ChannelKeyword::B => self.c2(), + _ => return Err(()), + }, + ColorSpace::Lch | ColorSpace::Oklch => match channel_keyword { + ChannelKeyword::L => self.c0(), + ChannelKeyword::C => self.c1(), + ChannelKeyword::H => self.c2(), + _ => return Err(()), + }, + ColorSpace::SrgbLinear | + ColorSpace::DisplayP3 | + ColorSpace::A98Rgb | + ColorSpace::ProphotoRgb | + ColorSpace::Rec2020 => match channel_keyword { + ChannelKeyword::R => self.c0(), + ChannelKeyword::G => self.c1(), + ChannelKeyword::B => self.c2(), + _ => return Err(()), + }, + ColorSpace::XyzD50 | ColorSpace::XyzD65 => match channel_keyword { + ChannelKeyword::X => self.c0(), + ChannelKeyword::Y => self.c1(), + ChannelKeyword::Z => self.c2(), + _ => return Err(()), + }, + }) + } + /// Convert this color to the specified color space. pub fn to_color_space(&self, color_space: ColorSpace) -> Self { use ColorSpace::*; |