use crate::color::AbsoluteColor;
use crate::values::animated::ToAnimatedZero;
use crate::values::computed::percentage::Percentage;
use crate::values::generics::color::{
GenericCaretColor, GenericColor, GenericColorMix, GenericColorOrAuto,
};
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};
pub use crate::values::specified::color::{ColorScheme, ForcedColorAdjust, PrintColorAdjust};
pub type ColorPropertyValue = AbsoluteColor;
pub type Color = GenericColor<Percentage>;
pub type ColorMix = GenericColorMix<Color, Percentage>;
impl ToCss for Color {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: fmt::Write,
{
match *self {
Self::Absolute(ref c) => c.to_css(dest),
Self::ColorFunction(ref color_function) => color_function.to_css(dest),
Self::CurrentColor => dest.write_str("currentcolor"),
Self::ColorMix(ref m) => m.to_css(dest),
}
}
}
impl Color {
pub const TRANSPARENT_BLACK: Self = Self::Absolute(AbsoluteColor::TRANSPARENT_BLACK);
pub const BLACK: Self = Self::Absolute(AbsoluteColor::BLACK);
pub const WHITE: Self = Self::Absolute(AbsoluteColor::WHITE);
pub fn from_color_mix(color_mix: ColorMix) -> Self {
if let Some(absolute) = color_mix.mix_to_absolute() {
Self::Absolute(absolute)
} else {
Self::ColorMix(Box::new(color_mix))
}
}
pub fn resolve_to_absolute(&self, current_color: &AbsoluteColor) -> AbsoluteColor {
use crate::values::specified::percentage::ToPercentage;
match *self {
Self::Absolute(c) => c,
Self::ColorFunction(ref color_function) => {
color_function.resolve_to_absolute(current_color)
},
Self::CurrentColor => *current_color,
Self::ColorMix(ref mix) => {
let left = mix.left.resolve_to_absolute(current_color);
let right = mix.right.resolve_to_absolute(current_color);
crate::color::mix::mix(
mix.interpolation,
&left,
mix.left_percentage.to_percentage(),
&right,
mix.right_percentage.to_percentage(),
mix.flags,
)
},
}
}
}
impl ToAnimatedZero for AbsoluteColor {
fn to_animated_zero(&self) -> Result<Self, ()> {
Ok(Self::TRANSPARENT_BLACK)
}
}
pub type ColorOrAuto = GenericColorOrAuto<Color>;
pub type CaretColor = GenericCaretColor<Color>;