style: Expose colors via cbindgen.

Also s/Foreground/CurrentColor.

Differential Revision: https://phabricator.services.mozilla.com/D25975
This commit is contained in:
Emilio Cobos Álvarez 2019-04-04 11:34:56 +00:00
parent a6f32bc762
commit 691eb36ffe
6 changed files with 50 additions and 37 deletions

View file

@ -49,8 +49,8 @@ impl From<ComputedColor> for StyleComplexColor {
fn from(other: ComputedColor) -> Self { fn from(other: ComputedColor) -> Self {
match other { match other {
GenericColor::Numeric(color) => color.into(), GenericColor::Numeric(color) => color.into(),
GenericColor::Foreground => Self::current_color(), GenericColor::CurrentColor => Self::current_color(),
GenericColor::Complex(color, ratios) => { GenericColor::Complex { color, ratios } => {
debug_assert!(ratios != ComplexColorRatios::NUMERIC); debug_assert!(ratios != ComplexColorRatios::NUMERIC);
debug_assert!(ratios != ComplexColorRatios::FOREGROUND); debug_assert!(ratios != ComplexColorRatios::FOREGROUND);
StyleComplexColor { StyleComplexColor {
@ -73,18 +73,18 @@ impl From<StyleComplexColor> for ComputedColor {
}, },
Tag::eForeground => { Tag::eForeground => {
debug_assert!(other.mBgRatio == 0. && other.mFgRatio == 1.); debug_assert!(other.mBgRatio == 0. && other.mFgRatio == 1.);
GenericColor::Foreground GenericColor::CurrentColor
}, },
Tag::eComplex => { Tag::eComplex => {
debug_assert!(other.mBgRatio != 1. || other.mFgRatio != 0.); debug_assert!(other.mBgRatio != 1. || other.mFgRatio != 0.);
debug_assert!(other.mBgRatio != 0. || other.mFgRatio != 1.); debug_assert!(other.mBgRatio != 0. || other.mFgRatio != 1.);
GenericColor::Complex( GenericColor::Complex {
convert_nscolor_to_rgba(other.mColor), color: convert_nscolor_to_rgba(other.mColor),
ComplexColorRatios { ratios: ComplexColorRatios {
bg: other.mBgRatio, bg: other.mBgRatio,
fg: other.mFgRatio, fg: other.mFgRatio,
}, },
) }
}, },
Tag::eAuto => unreachable!("Unsupport StyleComplexColor with tag eAuto"), Tag::eAuto => unreachable!("Unsupport StyleComplexColor with tag eAuto"),
} }

View file

@ -63,6 +63,7 @@ ${helpers.predefined_type(
"generics::color::ColorOrAuto::Auto", "generics::color::ColorOrAuto::Auto",
spec="https://drafts.csswg.org/css-ui/#caret-color", spec="https://drafts.csswg.org/css-ui/#caret-color",
animation_value_type="AnimatedCaretColor", animation_value_type="AnimatedCaretColor",
boxed=True,
ignored_when_colors_disabled=True, ignored_when_colors_disabled=True,
products="gecko", products="gecko",
)} )}

View file

@ -100,8 +100,8 @@ impl Color {
fn effective_intermediate_rgba(&self) -> RGBA { fn effective_intermediate_rgba(&self) -> RGBA {
match *self { match *self {
GenericColor::Numeric(color) => color, GenericColor::Numeric(color) => color,
GenericColor::Foreground => RGBA::transparent(), GenericColor::CurrentColor => RGBA::transparent(),
GenericColor::Complex(color, ratios) => RGBA { GenericColor::Complex { color, ratios } => RGBA {
alpha: color.alpha * ratios.bg, alpha: color.alpha * ratios.bg,
..color.clone() ..color.clone()
}, },
@ -111,8 +111,8 @@ impl Color {
fn effective_ratios(&self) -> ComplexColorRatios { fn effective_ratios(&self) -> ComplexColorRatios {
match *self { match *self {
GenericColor::Numeric(..) => ComplexColorRatios::NUMERIC, GenericColor::Numeric(..) => ComplexColorRatios::NUMERIC,
GenericColor::Foreground => ComplexColorRatios::FOREGROUND, GenericColor::CurrentColor => ComplexColorRatios::CURRENT_COLOR,
GenericColor::Complex(.., ratios) => ratios, GenericColor::Complex { ratios, .. } => ratios,
} }
} }
} }
@ -128,18 +128,18 @@ impl Animate for Color {
Ok(match (*self, *other, procedure) { Ok(match (*self, *other, procedure) {
// Any interpolation of currentcolor with currentcolor returns currentcolor. // Any interpolation of currentcolor with currentcolor returns currentcolor.
(Foreground, Foreground, Procedure::Interpolate { .. }) => Foreground, (CurrentColor, CurrentColor, Procedure::Interpolate { .. }) => CurrentColor,
// Animating two numeric colors. // Animating two numeric colors.
(Numeric(c1), Numeric(c2), _) => Numeric(c1.animate(&c2, procedure)?), (Numeric(c1), Numeric(c2), _) => Numeric(c1.animate(&c2, procedure)?),
// Combinations of numeric color and currentcolor // Combinations of numeric color and currentcolor
(Foreground, Numeric(color), _) => Self::with_ratios( (CurrentColor, Numeric(color), _) => Self::with_ratios(
color, color,
ComplexColorRatios { ComplexColorRatios {
bg: other_weight as f32, bg: other_weight as f32,
fg: this_weight as f32, fg: this_weight as f32,
}, },
), ),
(Numeric(color), Foreground, _) => Self::with_ratios( (Numeric(color), CurrentColor, _) => Self::with_ratios(
color, color,
ComplexColorRatios { ComplexColorRatios {
bg: this_weight as f32, bg: this_weight as f32,
@ -148,7 +148,7 @@ impl Animate for Color {
), ),
// Any other animation of currentcolor with currentcolor. // Any other animation of currentcolor with currentcolor.
(Foreground, Foreground, _) => Self::with_ratios( (CurrentColor, CurrentColor, _) => Self::with_ratios(
RGBA::transparent(), RGBA::transparent(),
ComplexColorRatios { ComplexColorRatios {
bg: 0., bg: 0.,
@ -162,8 +162,8 @@ impl Animate for Color {
fn scaled_rgba(color: &Color) -> RGBA { fn scaled_rgba(color: &Color) -> RGBA {
match *color { match *color {
GenericColor::Numeric(color) => color, GenericColor::Numeric(color) => color,
GenericColor::Foreground => RGBA::transparent(), GenericColor::CurrentColor => RGBA::transparent(),
GenericColor::Complex(color, ratios) => RGBA { GenericColor::Complex { color, ratios } => RGBA {
red: color.red * ratios.bg, red: color.red * ratios.bg,
green: color.green * ratios.bg, green: color.green * ratios.bg,
blue: color.blue * ratios.bg, blue: color.blue * ratios.bg,
@ -236,9 +236,9 @@ impl ComputeSquaredDistance for Color {
// All comments from the Animate impl also applies here. // All comments from the Animate impl also applies here.
Ok(match (*self, *other) { Ok(match (*self, *other) {
(Foreground, Foreground) => SquaredDistance::from_sqrt(0.), (CurrentColor, CurrentColor) => SquaredDistance::from_sqrt(0.),
(Numeric(c1), Numeric(c2)) => c1.compute_squared_distance(&c2)?, (Numeric(c1), Numeric(c2)) => c1.compute_squared_distance(&c2)?,
(Foreground, Numeric(color)) | (Numeric(color), Foreground) => { (CurrentColor, Numeric(color)) | (Numeric(color), CurrentColor) => {
// `computed_squared_distance` is symmetric. // `computed_squared_distance` is symmetric.
color.compute_squared_distance(&RGBA::transparent())? + color.compute_squared_distance(&RGBA::transparent())? +
SquaredDistance::from_sqrt(1.) SquaredDistance::from_sqrt(1.)

View file

@ -33,8 +33,8 @@ impl Color {
// Common cases that the complex color is either pure numeric // Common cases that the complex color is either pure numeric
// color or pure currentcolor. // color or pure currentcolor.
GenericColor::Numeric(color) => return color, GenericColor::Numeric(color) => return color,
GenericColor::Foreground => return fg_color, GenericColor::CurrentColor => return fg_color,
GenericColor::Complex(color, ratios) => (color, ratios), GenericColor::Complex { color, ratios } => (color, ratios),
}; };
// For the more complicated case that the alpha value differs, // For the more complicated case that the alpha value differs,
@ -76,7 +76,7 @@ impl ToCss for Color {
{ {
match *self { match *self {
GenericColor::Numeric(color) => color.to_css(dest), GenericColor::Numeric(color) => color.to_css(dest),
GenericColor::Foreground => CSSParserColor::CurrentColor.to_css(dest), GenericColor::CurrentColor => CSSParserColor::CurrentColor.to_css(dest),
_ => Ok(()), _ => Ok(()),
} }
} }

View file

@ -7,44 +7,53 @@
/// Ratios representing the contribution of color and currentcolor to /// Ratios representing the contribution of color and currentcolor to
/// the final color value. /// the final color value.
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)] #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
#[repr(C)]
pub struct ComplexColorRatios { pub struct ComplexColorRatios {
/// Numeric color contribution. /// Numeric color contribution.
pub bg: f32, pub bg: f32,
/// Foreground color, aka currentcolor, contribution. /// currentcolor contribution.
pub fg: f32, pub fg: f32,
} }
impl ComplexColorRatios { impl ComplexColorRatios {
/// Ratios representing a `Numeric` color. /// Ratios representing a `Numeric` color.
pub const NUMERIC: ComplexColorRatios = ComplexColorRatios { bg: 1., fg: 0. }; pub const NUMERIC: ComplexColorRatios = ComplexColorRatios { bg: 1., fg: 0. };
/// Ratios representing the `Foreground` color. /// Ratios representing the `CurrentColor` color.
pub const FOREGROUND: ComplexColorRatios = ComplexColorRatios { bg: 0., fg: 1. }; pub const CURRENT_COLOR: ComplexColorRatios = ComplexColorRatios { bg: 0., fg: 1. };
} }
/// This enum represents a combined color from a numeric color and /// This enum represents a combined color from a numeric color and
/// the current foreground color (currentcolor keyword). /// the current foreground color (currentcolor keyword).
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)] #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
pub enum Color<RGBA> { #[repr(C, u8)]
pub enum GenericColor<RGBA> {
/// Numeric RGBA color. /// Numeric RGBA color.
Numeric(RGBA), Numeric(RGBA),
/// The current foreground color. /// The current foreground color.
Foreground, CurrentColor,
/// A linear combination of numeric color and currentcolor. /// A linear combination of numeric color and currentcolor.
/// The formula is: `color * ratios.bg + currentcolor * ratios.fg`. /// The formula is: `color * ratios.bg + currentcolor * ratios.fg`.
Complex(RGBA, ComplexColorRatios), Complex {
/// The actual numeric color.
color: RGBA,
/// The ratios of mixing between numeric and currentcolor.
ratios: ComplexColorRatios,
}
} }
pub use self::GenericColor as Color;
impl<RGBA> Color<RGBA> { impl<RGBA> Color<RGBA> {
/// Create a color based upon the specified ratios. /// Create a color based upon the specified ratios.
pub fn with_ratios(color: RGBA, ratios: ComplexColorRatios) -> Self { pub fn with_ratios(color: RGBA, ratios: ComplexColorRatios) -> Self {
if ratios == ComplexColorRatios::NUMERIC { if ratios == ComplexColorRatios::NUMERIC {
Color::Numeric(color) Color::Numeric(color)
} else if ratios == ComplexColorRatios::FOREGROUND { } else if ratios == ComplexColorRatios::CURRENT_COLOR {
Color::Foreground Color::CurrentColor
} else { } else {
Color::Complex(color, ratios) Color::Complex { color, ratios }
} }
} }
@ -55,7 +64,7 @@ impl<RGBA> Color<RGBA> {
/// Returns a complex color value representing currentcolor. /// Returns a complex color value representing currentcolor.
pub fn currentcolor() -> Self { pub fn currentcolor() -> Self {
Color::Foreground Color::CurrentColor
} }
/// Whether it is a numeric color (no currentcolor component). /// Whether it is a numeric color (no currentcolor component).
@ -65,7 +74,7 @@ impl<RGBA> Color<RGBA> {
/// Whether it is a currentcolor value (no numeric color component). /// Whether it is a currentcolor value (no numeric color component).
pub fn is_currentcolor(&self) -> bool { pub fn is_currentcolor(&self) -> bool {
matches!(*self, Color::Foreground) matches!(*self, Color::CurrentColor)
} }
} }
@ -92,9 +101,12 @@ impl<RGBA> From<RGBA> for Color<RGBA> {
ToCss, ToCss,
ToShmem, ToShmem,
)] )]
pub enum ColorOrAuto<C> { #[repr(C, u8)]
/// A `<color> pub enum GenericColorOrAuto<C> {
/// A `<color>`.
Color(C), Color(C),
/// `auto` /// `auto`
Auto, Auto,
} }
pub use self::GenericColorOrAuto as ColorOrAuto;

View file

@ -387,8 +387,8 @@ impl ToComputedValue for Color {
fn from_computed_value(computed: &ComputedColor) -> Self { fn from_computed_value(computed: &ComputedColor) -> Self {
match *computed { match *computed {
GenericColor::Numeric(color) => Color::rgba(color), GenericColor::Numeric(color) => Color::rgba(color),
GenericColor::Foreground => Color::currentcolor(), GenericColor::CurrentColor => Color::currentcolor(),
GenericColor::Complex(..) => Color::Complex(*computed), GenericColor::Complex { .. } => Color::Complex(*computed),
} }
} }
} }