diff options
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::*; |