Skip to main content

style/values/resolved/
color.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Resolved color values.
6
7use super::{Context, ToResolvedValue};
8
9use crate::properties::{PropertyId, ShorthandId};
10use crate::values::computed::color as computed;
11use crate::values::generics::color as generics;
12
13impl ToResolvedValue for computed::Color {
14    // A resolved color value is (almost always) a rgba color, with currentcolor resolved.
15    type ResolvedValue = Self;
16
17    #[inline]
18    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
19        if context.for_property == PropertyId::NonCustom(ShorthandId::TextDecoration.into())
20            && matches!(self, Self::CurrentColor)
21        {
22            return self;
23        }
24        generics::Color::Absolute(context.style.resolve_color(&self))
25    }
26
27    #[inline]
28    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
29        resolved
30    }
31}
32
33impl ToResolvedValue for computed::CaretColor {
34    // A resolved caret-color value is an rgba color, with auto resolving to
35    // currentcolor.
36    type ResolvedValue = computed::Color;
37
38    #[inline]
39    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
40        let color = match self.0 {
41            generics::ColorOrAuto::Color(color) => color,
42            generics::ColorOrAuto::Auto => generics::Color::currentcolor(),
43        };
44        color.to_resolved_value(context)
45    }
46
47    #[inline]
48    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
49        generics::CaretColor(generics::ColorOrAuto::Color(
50            computed::Color::from_resolved_value(resolved),
51        ))
52    }
53}