Bug 1541546 - Expose colors via cbindgen. r=heycam
☠☠ backed out by 95484a7dc30b ☠ ☠
authorEmilio Cobos Álvarez <emilio@crisal.io>
Thu, 04 Apr 2019 11:34:56 +0000
changeset 467973 ba71816514eb515f0a7c29de61ddeca9d2939551
parent 467972 f98617ff7cf585b77a60e90f9ba3f191d09ec653
child 467974 4da6fb98e8f9d7db99181e763919a765f0d06f1b
push id112667
push useraiakab@mozilla.com
push dateThu, 04 Apr 2019 16:12:45 +0000
treeherdermozilla-inbound@230bb363f2f3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1541546
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1541546 - Expose colors via cbindgen. r=heycam Also s/Foreground/CurrentColor. Differential Revision: https://phabricator.services.mozilla.com/D25975
servo/components/style/gecko_bindings/sugar/style_complex_color.rs
servo/components/style/values/animated/color.rs
servo/components/style/values/computed/color.rs
servo/components/style/values/generics/color.rs
servo/components/style/values/specified/color.rs
servo/ports/geckolib/cbindgen.toml
--- a/servo/components/style/gecko_bindings/sugar/style_complex_color.rs
+++ b/servo/components/style/gecko_bindings/sugar/style_complex_color.rs
@@ -44,18 +44,18 @@ impl From<ComputedRGBA> for StyleComplex
         }
     }
 }
 
 impl From<ComputedColor> for StyleComplexColor {
     fn from(other: ComputedColor) -> Self {
         match other {
             GenericColor::Numeric(color) => color.into(),
-            GenericColor::Foreground => Self::current_color(),
-            GenericColor::Complex(color, ratios) => {
+            GenericColor::CurrentColor => Self::current_color(),
+            GenericColor::Complex { color, ratios } => {
                 debug_assert!(ratios != ComplexColorRatios::NUMERIC);
                 debug_assert!(ratios != ComplexColorRatios::FOREGROUND);
                 StyleComplexColor {
                     mColor: convert_rgba_to_nscolor(&color).into(),
                     mBgRatio: ratios.bg,
                     mFgRatio: ratios.fg,
                     mTag: Tag::eComplex,
                 }
@@ -68,28 +68,28 @@ impl From<StyleComplexColor> for Compute
     fn from(other: StyleComplexColor) -> Self {
         match other.mTag {
             Tag::eNumeric => {
                 debug_assert!(other.mBgRatio == 1. && other.mFgRatio == 0.);
                 GenericColor::Numeric(convert_nscolor_to_rgba(other.mColor))
             },
             Tag::eForeground => {
                 debug_assert!(other.mBgRatio == 0. && other.mFgRatio == 1.);
-                GenericColor::Foreground
+                GenericColor::CurrentColor
             },
             Tag::eComplex => {
                 debug_assert!(other.mBgRatio != 1. || other.mFgRatio != 0.);
                 debug_assert!(other.mBgRatio != 0. || other.mFgRatio != 1.);
-                GenericColor::Complex(
-                    convert_nscolor_to_rgba(other.mColor),
-                    ComplexColorRatios {
+                GenericColor::Complex {
+                    color: convert_nscolor_to_rgba(other.mColor),
+                    ratios: ComplexColorRatios {
                         bg: other.mBgRatio,
                         fg: other.mFgRatio,
                     },
-                )
+                }
             },
             Tag::eAuto => unreachable!("Unsupport StyleComplexColor with tag eAuto"),
         }
     }
 }
 
 impl From<ColorOrAuto> for StyleComplexColor {
     fn from(other: ColorOrAuto) -> Self {
--- a/servo/components/style/values/animated/color.rs
+++ b/servo/components/style/values/animated/color.rs
@@ -95,80 +95,80 @@ impl ComputeSquaredDistance for RGBA {
 
 /// An animated value for `<color>`.
 pub type Color = GenericColor<RGBA>;
 
 impl Color {
     fn effective_intermediate_rgba(&self) -> RGBA {
         match *self {
             GenericColor::Numeric(color) => color,
-            GenericColor::Foreground => RGBA::transparent(),
-            GenericColor::Complex(color, ratios) => RGBA {
+            GenericColor::CurrentColor => RGBA::transparent(),
+            GenericColor::Complex { color, ratios } => RGBA {
                 alpha: color.alpha * ratios.bg,
                 ..color.clone()
             },
         }
     }
 
     fn effective_ratios(&self) -> ComplexColorRatios {
         match *self {
             GenericColor::Numeric(..) => ComplexColorRatios::NUMERIC,
-            GenericColor::Foreground => ComplexColorRatios::FOREGROUND,
-            GenericColor::Complex(.., ratios) => ratios,
+            GenericColor::CurrentColor => ComplexColorRatios::CURRENT_COLOR,
+            GenericColor::Complex { ratios, .. } => ratios,
         }
     }
 }
 
 impl Animate for Color {
     #[inline]
     fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
         use self::GenericColor::*;
 
         // Common cases are interpolating between two numeric colors,
         // two currentcolors, and a numeric color and a currentcolor.
         let (this_weight, other_weight) = procedure.weights();
 
         Ok(match (*self, *other, procedure) {
             // Any interpolation of currentcolor with currentcolor returns currentcolor.
-            (Foreground, Foreground, Procedure::Interpolate { .. }) => Foreground,
+            (CurrentColor, CurrentColor, Procedure::Interpolate { .. }) => CurrentColor,
             // Animating two numeric colors.
             (Numeric(c1), Numeric(c2), _) => Numeric(c1.animate(&c2, procedure)?),
             // Combinations of numeric color and currentcolor
-            (Foreground, Numeric(color), _) => Self::with_ratios(
+            (CurrentColor, Numeric(color), _) => Self::with_ratios(
                 color,
                 ComplexColorRatios {
                     bg: other_weight as f32,
                     fg: this_weight as f32,
                 },
             ),
-            (Numeric(color), Foreground, _) => Self::with_ratios(
+            (Numeric(color), CurrentColor, _) => Self::with_ratios(
                 color,
                 ComplexColorRatios {
                     bg: this_weight as f32,
                     fg: other_weight as f32,
                 },
             ),
 
             // Any other animation of currentcolor with currentcolor.
-            (Foreground, Foreground, _) => Self::with_ratios(
+            (CurrentColor, CurrentColor, _) => Self::with_ratios(
                 RGBA::transparent(),
                 ComplexColorRatios {
                     bg: 0.,
                     fg: (this_weight + other_weight) as f32,
                 },
             ),
 
             // Defer to complex calculations
             _ => {
                 // Compute the "scaled" contribution for `color`.
                 fn scaled_rgba(color: &Color) -> RGBA {
                     match *color {
                         GenericColor::Numeric(color) => color,
-                        GenericColor::Foreground => RGBA::transparent(),
-                        GenericColor::Complex(color, ratios) => RGBA {
+                        GenericColor::CurrentColor => RGBA::transparent(),
+                        GenericColor::Complex { color, ratios } => RGBA {
                             red: color.red * ratios.bg,
                             green: color.green * ratios.bg,
                             blue: color.blue * ratios.bg,
                             alpha: color.alpha * ratios.bg,
                         },
                     }
                 }
 
@@ -231,19 +231,19 @@ impl Animate for Color {
 
 impl ComputeSquaredDistance for Color {
     #[inline]
     fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
         use self::GenericColor::*;
 
         // All comments from the Animate impl also applies here.
         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)?,
-            (Foreground, Numeric(color)) | (Numeric(color), Foreground) => {
+            (CurrentColor, Numeric(color)) | (Numeric(color), CurrentColor) => {
                 // `computed_squared_distance` is symmetric.
                 color.compute_squared_distance(&RGBA::transparent())? +
                     SquaredDistance::from_sqrt(1.)
             },
             (_, _) => {
                 let self_color = self.effective_intermediate_rgba();
                 let other_color = other.effective_intermediate_rgba();
                 let self_ratios = self.effective_ratios();
--- a/servo/components/style/values/computed/color.rs
+++ b/servo/components/style/values/computed/color.rs
@@ -28,18 +28,18 @@ impl Color {
 
     /// Combine this complex color with the given foreground color into
     /// a numeric RGBA color. It currently uses linear blending.
     pub fn to_rgba(&self, fg_color: RGBA) -> RGBA {
         let (color, ratios) = match *self {
             // Common cases that the complex color is either pure numeric
             // color or pure currentcolor.
             GenericColor::Numeric(color) => return color,
-            GenericColor::Foreground => return fg_color,
-            GenericColor::Complex(color, ratios) => (color, ratios),
+            GenericColor::CurrentColor => return fg_color,
+            GenericColor::Complex { color, ratios } => (color, ratios),
         };
 
         // For the more complicated case that the alpha value differs,
         // we use the following formula to compute the components:
         // alpha = self_alpha * bg_ratio + fg_alpha * fg_ratio
         // color = (self_color * self_alpha * bg_ratio +
         //          fg_color * fg_alpha * fg_ratio) / alpha
 
@@ -71,17 +71,17 @@ impl Color {
 
 impl ToCss for Color {
     fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
     where
         W: fmt::Write,
     {
         match *self {
             GenericColor::Numeric(color) => color.to_css(dest),
-            GenericColor::Foreground => CSSParserColor::CurrentColor.to_css(dest),
+            GenericColor::CurrentColor => CSSParserColor::CurrentColor.to_css(dest),
             _ => Ok(()),
         }
     }
 }
 
 impl ToAnimatedValue for RGBA {
     type AnimatedValue = AnimatedRGBA;
 
--- a/servo/components/style/values/generics/color.rs
+++ b/servo/components/style/values/generics/color.rs
@@ -2,75 +2,84 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
 //! Generic types for color properties.
 
 /// Ratios representing the contribution of color and currentcolor to
 /// the final color value.
 #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
+#[repr(C)]
 pub struct ComplexColorRatios {
     /// Numeric color contribution.
     pub bg: f32,
-    /// Foreground color, aka currentcolor, contribution.
+    /// currentcolor contribution.
     pub fg: f32,
 }
 
 impl ComplexColorRatios {
     /// Ratios representing a `Numeric` color.
     pub const NUMERIC: ComplexColorRatios = ComplexColorRatios { bg: 1., fg: 0. };
-    /// Ratios representing the `Foreground` color.
-    pub const FOREGROUND: ComplexColorRatios = ComplexColorRatios { bg: 0., fg: 1. };
+    /// Ratios representing the `CurrentColor` color.
+    pub const CURRENT_COLOR: ComplexColorRatios = ComplexColorRatios { bg: 0., fg: 1. };
 }
 
 /// This enum represents a combined color from a numeric color and
 /// the current foreground color (currentcolor keyword).
 #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
-pub enum Color<RGBA> {
+#[repr(C, u8)]
+pub enum GenericColor<RGBA> {
     ///  Numeric RGBA color.
     Numeric(RGBA),
 
     /// The current foreground color.
-    Foreground,
+    CurrentColor,
 
     /// A linear combination of numeric color and currentcolor.
     /// 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> {
     /// Create a color based upon the specified ratios.
     pub fn with_ratios(color: RGBA, ratios: ComplexColorRatios) -> Self {
         if ratios == ComplexColorRatios::NUMERIC {
             Color::Numeric(color)
-        } else if ratios == ComplexColorRatios::FOREGROUND {
-            Color::Foreground
+        } else if ratios == ComplexColorRatios::CURRENT_COLOR {
+            Color::CurrentColor
         } else {
-            Color::Complex(color, ratios)
+            Color::Complex { color, ratios }
         }
     }
 
     /// Returns a numeric color representing the given RGBA value.
     pub fn rgba(color: RGBA) -> Self {
         Color::Numeric(color)
     }
 
     /// Returns a complex color value representing currentcolor.
     pub fn currentcolor() -> Self {
-        Color::Foreground
+        Color::CurrentColor
     }
 
     /// Whether it is a numeric color (no currentcolor component).
     pub fn is_numeric(&self) -> bool {
         matches!(*self, Color::Numeric(..))
     }
 
     /// Whether it is a currentcolor value (no numeric color component).
     pub fn is_currentcolor(&self) -> bool {
-        matches!(*self, Color::Foreground)
+        matches!(*self, Color::CurrentColor)
     }
 }
 
 impl<RGBA> From<RGBA> for Color<RGBA> {
     fn from(color: RGBA) -> Self {
         Self::rgba(color)
     }
 }
@@ -87,14 +96,17 @@ impl<RGBA> From<RGBA> for Color<RGBA> {
     Parse,
     SpecifiedValueInfo,
     ToAnimatedValue,
     ToAnimatedZero,
     ToComputedValue,
     ToCss,
     ToShmem,
 )]
-pub enum ColorOrAuto<C> {
-    /// A `<color>
+#[repr(C, u8)]
+pub enum GenericColorOrAuto<C> {
+    /// A `<color>`.
     Color(C),
     /// `auto`
     Auto,
 }
+
+pub use self::GenericColorOrAuto as ColorOrAuto;
--- a/servo/components/style/values/specified/color.rs
+++ b/servo/components/style/values/specified/color.rs
@@ -382,18 +382,18 @@ impl ToComputedValue for Color {
             }
         }
         result
     }
 
     fn from_computed_value(computed: &ComputedColor) -> Self {
         match *computed {
             GenericColor::Numeric(color) => Color::rgba(color),
-            GenericColor::Foreground => Color::currentcolor(),
-            GenericColor::Complex(..) => Color::Complex(*computed),
+            GenericColor::CurrentColor => Color::currentcolor(),
+            GenericColor::Complex { .. } => Color::Complex(*computed),
         }
     }
 }
 
 /// Specified color value, but resolved to just RGBA for computed value
 /// with value from color property at the same context.
 #[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
 pub struct RGBAColor(pub Color);
--- a/servo/ports/geckolib/cbindgen.toml
+++ b/servo/ports/geckolib/cbindgen.toml
@@ -101,16 +101,18 @@ include = [
   "RestyleHint",
   "TouchAction",
   "WillChangeBits",
   "TextDecorationLine",
   "MozListReversed",
   "Owned",
   "OwnedOrNull",
   "Strong",
+  "Color",
+  "ColorOrAuto",
 ]
 item_types = ["enums", "structs", "typedefs", "functions"]
 renaming_overrides_prefixing = true
 
 # Prevent some renaming for Gecko types that cbindgen doesn't otherwise understand.
 [export.rename]
 "nscolor" = "nscolor"
 "nsAtom" = "nsAtom"