diff --git a/gfx/src/nsFont.h b/gfx/src/nsFont.h index 4c914a07fa76..3e8bffa5c1a1 100644 --- a/gfx/src/nsFont.h +++ b/gfx/src/nsFont.h @@ -45,7 +45,7 @@ struct nsFont final { // rendering mode when the alpha component > 0. Only used for text in the // chrome. mozilla::StyleAbsoluteColor fontSmoothingBackgroundColor = - mozilla::StyleAbsoluteColor::TRANSPARENT; + mozilla::StyleAbsoluteColor::Transparent(); // Language system tag, to override document language; // this is an OpenType "language system" tag represented as a 32-bit integer diff --git a/layout/generic/nsImageFrame.cpp b/layout/generic/nsImageFrame.cpp index f98964457c1d..e2f72b3a76ae 100644 --- a/layout/generic/nsImageFrame.cpp +++ b/layout/generic/nsImageFrame.cpp @@ -1809,7 +1809,7 @@ void nsImageFrame::DisplayAltText(nsPresContext* aPresContext, struct nsRecessedBorder : public nsStyleBorder { explicit nsRecessedBorder(nscoord aBorderWidth) { for (const auto side : AllPhysicalSides()) { - BorderColorFor(side) = StyleColor::BLACK; + BorderColorFor(side) = StyleColor::Black(); mBorder.Side(side) = aBorderWidth; // Note: use SetBorderStyle here because we want to affect // mComputedBorder diff --git a/layout/painting/nsCSSRenderingGradients.cpp b/layout/painting/nsCSSRenderingGradients.cpp index e7ca461653b4..f4a859cf34e5 100644 --- a/layout/painting/nsCSSRenderingGradients.cpp +++ b/layout/painting/nsCSSRenderingGradients.cpp @@ -552,7 +552,7 @@ static StyleAbsoluteColor GetSpecifiedColor( const StyleGenericGradientItem& aItem, const ComputedStyle& aStyle) { if (aItem.IsInterpolationHint()) { - return StyleAbsoluteColor::TRANSPARENT; + return StyleAbsoluteColor::Transparent(); } const StyleColor& c = aItem.IsSimpleColorStop() ? aItem.AsSimpleColorStop() @@ -1084,7 +1084,7 @@ void nsCSSGradientRenderer::Paint(gfxContext& aContext, const nsRect& aDest, gfxRect dirtyFillRect = fillRect.Intersect(dirtyAreaToFill); gfxRect fillRectRelativeToTile = dirtyFillRect - tileRect.TopLeft(); - auto edgeColor = StyleAbsoluteColor::TRANSPARENT; + auto edgeColor = StyleAbsoluteColor::Transparent(); if (mGradient->IsLinear() && !isRepeat && RectIsBeyondLinearGradientEdge(fillRectRelativeToTile, matrix, mStops, gradientStart, gradientEnd, diff --git a/layout/style/StyleColorInlines.h b/layout/style/StyleColorInlines.h index 6057ac9abe09..72b4dbae8adb 100644 --- a/layout/style/StyleColorInlines.h +++ b/layout/style/StyleColorInlines.h @@ -28,25 +28,37 @@ inline StyleAbsoluteColor StyleAbsoluteColor::Srgb(float red, float green, StyleColorSpace::Srgb, StyleColorFlags{0}}; } +inline StyleAbsoluteColor StyleAbsoluteColor::Transparent() { + return StyleAbsoluteColor::Srgb(0.0f, 0.0f, 0.0f, 0.0f); +} + +inline StyleAbsoluteColor StyleAbsoluteColor::Black() { + return StyleAbsoluteColor::Srgb(0.0f, 0.0f, 0.0f, 1.0f); +} + +inline StyleAbsoluteColor StyleAbsoluteColor::White() { + return StyleAbsoluteColor::Srgb(1.0f, 1.0f, 1.0f, 1.0f); +} + template <> inline StyleColor StyleColor::FromColor(nscolor aColor) { return StyleColor::Absolute(StyleAbsoluteColor::FromColor(aColor)); } -// static template <> -inline const StyleColor StyleColor::TRANSPARENT = - StyleColor::Absolute(StyleAbsoluteColor::TRANSPARENT); +inline StyleColor StyleColor::Black() { + return FromColor(NS_RGB(0, 0, 0)); +} -// static template <> -inline const StyleColor StyleColor::BLACK = - StyleColor::Absolute(StyleAbsoluteColor::BLACK); +inline StyleColor StyleColor::White() { + return FromColor(NS_RGB(255, 255, 255)); +} -// static template <> -inline const StyleColor StyleColor::WHITE = - StyleColor::Absolute(StyleAbsoluteColor::WHITE); +inline StyleColor StyleColor::Transparent() { + return FromColor(NS_RGBA(0, 0, 0, 0)); +} template <> StyleAbsoluteColor StyleColor::ResolveColor(const StyleAbsoluteColor&) const; diff --git a/layout/style/nsStyleStruct.cpp b/layout/style/nsStyleStruct.cpp index 2536de5c91b5..019347ae88a5 100644 --- a/layout/style/nsStyleStruct.cpp +++ b/layout/style/nsStyleStruct.cpp @@ -752,7 +752,7 @@ using SVGPaintFallback = StyleGenericSVGPaintFallback; // nsStyleSVG // nsStyleSVG::nsStyleSVG() - : mFill{StyleSVGPaintKind::Color(StyleColor::BLACK), + : mFill{StyleSVGPaintKind::Color(StyleColor::Black()), SVGPaintFallback::Unset()}, mStroke{StyleSVGPaintKind::None(), SVGPaintFallback::Unset()}, mMarkerEnd(StyleUrlOrNone::None()), @@ -899,9 +899,9 @@ nsStyleSVGReset::nsStyleSVGReset() mR(NonNegativeLengthPercentage::Zero()), mMask(nsStyleImageLayers::LayerType::Mask), mClipPath(StyleClipPath::None()), - mStopColor(StyleColor::BLACK), - mFloodColor(StyleColor::BLACK), - mLightingColor(StyleColor::WHITE), + mStopColor(StyleColor::Black()), + mFloodColor(StyleColor::Black()), + mLightingColor(StyleColor::White()), mStopOpacity(1.0f), mFloodOpacity(1.0f), mVectorEffect(StyleVectorEffect::None), @@ -2037,7 +2037,7 @@ nsChangeHint nsStyleImageLayers::Layer::CalcDifference( nsStyleBackground::nsStyleBackground() : mImage(nsStyleImageLayers::LayerType::Background), - mBackgroundColor(StyleColor::TRANSPARENT) { + mBackgroundColor(StyleColor::Transparent()) { MOZ_COUNT_CTOR(nsStyleBackground); } diff --git a/servo/components/style/color/mod.rs b/servo/components/style/color/mod.rs index 39355684f0e2..8731a4b14b1c 100644 --- a/servo/components/style/color/mod.rs +++ b/servo/components/style/color/mod.rs @@ -185,30 +185,6 @@ macro_rules! color_components_as { } impl AbsoluteColor { - /// A fully transparent color in the legacy syntax. - pub const TRANSPARENT: Self = Self { - components: ColorComponents(0.0, 0.0, 0.0), - alpha: 0.0, - color_space: ColorSpace::Srgb, - flags: ColorFlags { bits: 0 }, // cbindgen does not like ColorFlags::empty(). - }; - - /// An opaque black color in the legacy syntax. - pub const BLACK: Self = Self { - components: ColorComponents(0.0, 0.0, 0.0), - alpha: 1.0, - color_space: ColorSpace::Srgb, - flags: ColorFlags { bits: 0 }, // cbindgen does not like ColorFlags::empty(). - }; - - /// An opaque white color in the legacy syntax. - pub const WHITE: Self = Self { - components: ColorComponents(1.0, 1.0, 1.0), - alpha: 1.0, - color_space: ColorSpace::Srgb, - flags: ColorFlags { bits: 0 }, // cbindgen does not like ColorFlags::empty(). - }; - /// Create a new [`AbsoluteColor`] with the given [`ColorSpace`] and /// components. pub fn new(color_space: ColorSpace, components: ColorComponents, alpha: f32) -> Self { @@ -247,6 +223,21 @@ impl AbsoluteColor { Self::new(ColorSpace::Srgb, ColorComponents(red, green, blue), alpha) } + /// Create a new transparent color. + pub fn transparent() -> Self { + Self::srgb(0.0, 0.0, 0.0, 0.0) + } + + /// Create a new opaque black color. + pub fn black() -> Self { + Self::srgb(0.0, 0.0, 0.0, 1.0) + } + + /// Create a new opaque white color. + pub fn white() -> Self { + Self::srgb(1.0, 1.0, 1.0, 1.0) + } + /// Return all the components of the color in an array. (Includes alpha) #[inline] pub fn raw_components(&self) -> &[f32; 4] { diff --git a/servo/components/style/properties/cascade.rs b/servo/components/style/properties/cascade.rs index 6a8daad993d0..0b03622c2447 100644 --- a/servo/components/style/properties/cascade.rs +++ b/servo/components/style/properties/cascade.rs @@ -445,7 +445,7 @@ fn tweak_when_ignoring_colors( // We assume here currentColor is opaque. color .to_computed_value(context) - .resolve_to_absolute(&AbsoluteColor::BLACK) + .resolve_to_absolute(&AbsoluteColor::black()) .alpha } diff --git a/servo/components/style/properties/longhands/background.mako.rs b/servo/components/style/properties/longhands/background.mako.rs index e01c2a4f8570..76b71b12cd01 100644 --- a/servo/components/style/properties/longhands/background.mako.rs +++ b/servo/components/style/properties/longhands/background.mako.rs @@ -9,7 +9,7 @@ ${helpers.predefined_type( "background-color", "Color", - "computed::Color::TRANSPARENT", + "computed::Color::transparent()", engines="gecko servo-2013 servo-2020", initial_specified_value="SpecifiedValue::transparent()", spec="https://drafts.csswg.org/css-backgrounds/#background-color", diff --git a/servo/components/style/properties/longhands/font.mako.rs b/servo/components/style/properties/longhands/font.mako.rs index cd63d4562fa5..7650fc2886ab 100644 --- a/servo/components/style/properties/longhands/font.mako.rs +++ b/servo/components/style/properties/longhands/font.mako.rs @@ -445,7 +445,7 @@ ${helpers.single_keyword( ${helpers.predefined_type( "-moz-font-smoothing-background-color", "color::MozFontSmoothingBackgroundColor", - "computed::color::MozFontSmoothingBackgroundColor::TRANSPARENT", + "computed::color::MozFontSmoothingBackgroundColor::transparent()", engines="gecko", animation_value_type="none", gecko_ffi_name="mFont.fontSmoothingBackgroundColor", diff --git a/servo/components/style/properties/longhands/inherited_svg.mako.rs b/servo/components/style/properties/longhands/inherited_svg.mako.rs index 84b484660e52..b9d55c01b20e 100644 --- a/servo/components/style/properties/longhands/inherited_svg.mako.rs +++ b/servo/components/style/properties/longhands/inherited_svg.mako.rs @@ -51,7 +51,7 @@ ${helpers.single_keyword( ${helpers.predefined_type( "fill", "SVGPaint", - "crate::values::computed::SVGPaint::BLACK", + "crate::values::computed::SVGPaint::black()", engines="gecko", animation_value_type="IntermediateSVGPaint", boxed=True, diff --git a/servo/components/style/properties/longhands/inherited_text.mako.rs b/servo/components/style/properties/longhands/inherited_text.mako.rs index 7b2108e2307a..c1b110c08656 100644 --- a/servo/components/style/properties/longhands/inherited_text.mako.rs +++ b/servo/components/style/properties/longhands/inherited_text.mako.rs @@ -9,7 +9,7 @@ ${helpers.predefined_type( "color", "ColorPropertyValue", - "crate::color::AbsoluteColor::BLACK", + "crate::color::AbsoluteColor::black()", engines="gecko servo-2013 servo-2020", animation_value_type="AbsoluteColor", ignored_when_colors_disabled="True", diff --git a/servo/components/style/properties/longhands/svg.mako.rs b/servo/components/style/properties/longhands/svg.mako.rs index 165687992288..d579473a0230 100644 --- a/servo/components/style/properties/longhands/svg.mako.rs +++ b/servo/components/style/properties/longhands/svg.mako.rs @@ -20,7 +20,7 @@ ${helpers.single_keyword( ${helpers.predefined_type( "stop-color", "Color", - "computed::Color::BLACK", + "computed::Color::black()", engines="gecko", animation_value_type="AnimatedRGBA", spec="https://www.w3.org/TR/SVGTiny12/painting.html#StopColorProperty", @@ -40,7 +40,7 @@ ${helpers.predefined_type( ${helpers.predefined_type( "flood-color", "Color", - "computed::Color::BLACK", + "computed::Color::black()", engines="gecko", animation_value_type="AnimatedColor", spec="https://www.w3.org/TR/SVG/filters.html#FloodColorProperty", @@ -58,7 +58,7 @@ ${helpers.predefined_type( ${helpers.predefined_type( "lighting-color", "Color", - "computed::Color::WHITE", + "computed::Color::white()", engines="gecko", animation_value_type="AnimatedColor", spec="https://www.w3.org/TR/SVG/filters.html#LightingColorProperty", diff --git a/servo/components/style/values/animated/color.rs b/servo/components/style/values/animated/color.rs index 12d865b463fe..d03eb9b3a855 100644 --- a/servo/components/style/values/animated/color.rs +++ b/servo/components/style/values/animated/color.rs @@ -74,7 +74,7 @@ impl Animate for Color { impl ComputeSquaredDistance for Color { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - let current_color = AbsoluteColor::TRANSPARENT; + let current_color = AbsoluteColor::transparent(); self.resolve_to_absolute(¤t_color) .compute_squared_distance(&other.resolve_to_absolute(¤t_color)) } @@ -83,6 +83,6 @@ impl ComputeSquaredDistance for Color { impl ToAnimatedZero for Color { #[inline] fn to_animated_zero(&self) -> Result { - Ok(Color::Absolute(AbsoluteColor::TRANSPARENT)) + Ok(Color::Absolute(AbsoluteColor::transparent())) } } diff --git a/servo/components/style/values/computed/color.rs b/servo/components/style/values/computed/color.rs index 035ad43dada6..820bcfe5b1b3 100644 --- a/servo/components/style/values/computed/color.rs +++ b/servo/components/style/values/computed/color.rs @@ -42,15 +42,6 @@ impl ToCss for Color { } impl Color { - /// A fully transparent color. - pub const TRANSPARENT: Self = Self::Absolute(AbsoluteColor::TRANSPARENT); - - /// An opaque black color. - pub const BLACK: Self = Self::Absolute(AbsoluteColor::BLACK); - - /// An opaque white color. - pub const WHITE: Self = Self::Absolute(AbsoluteColor::WHITE); - /// Create a new computed [`Color`] from a given color-mix, simplifying it to an absolute color /// if possible. pub fn from_color_mix(color_mix: ColorMix) -> Self { @@ -61,6 +52,21 @@ impl Color { } } + /// Returns a complex color value representing transparent. + pub fn transparent() -> Color { + Color::Absolute(AbsoluteColor::transparent()) + } + + /// Returns opaque black. + pub fn black() -> Color { + Color::Absolute(AbsoluteColor::black()) + } + + /// Returns opaque white. + pub fn white() -> Color { + Color::Absolute(AbsoluteColor::white()) + } + /// Combine this complex color with the given foreground color into an /// absolute color. pub fn resolve_to_absolute(&self, current_color: &AbsoluteColor) -> AbsoluteColor { @@ -87,7 +93,7 @@ impl Color { impl ToAnimatedZero for AbsoluteColor { fn to_animated_zero(&self) -> Result { - Ok(Self::TRANSPARENT) + Ok(Self::transparent()) } } diff --git a/servo/components/style/values/computed/svg.rs b/servo/components/style/values/computed/svg.rs index 6efdfca36b7b..640c3bfda708 100644 --- a/servo/components/style/values/computed/svg.rs +++ b/servo/components/style/values/computed/svg.rs @@ -20,10 +20,12 @@ pub type SVGPaintKind = generic::GenericSVGPaintKind; impl SVGPaint { /// Opaque black color - pub const BLACK: Self = Self { - kind: generic::SVGPaintKind::Color(Color::BLACK), - fallback: generic::SVGPaintFallback::Unset, - }; + pub fn black() -> Self { + SVGPaint { + kind: generic::SVGPaintKind::Color(Color::black()), + fallback: generic::SVGPaintFallback::Unset, + } + } } /// | | | context-value diff --git a/servo/components/style/values/specified/color.rs b/servo/components/style/values/specified/color.rs index 18c747dc0c96..3a745d488bd2 100644 --- a/servo/components/style/values/specified/color.rs +++ b/servo/components/style/values/specified/color.rs @@ -801,7 +801,7 @@ impl Color { #[inline] pub fn transparent() -> Self { // We should probably set authored to "transparent", but maybe it doesn't matter. - Self::from_absolute_color(AbsoluteColor::TRANSPARENT) + Self::from_absolute_color(AbsoluteColor::transparent()) } /// Create a color from an [`AbsoluteColor`]. @@ -974,7 +974,7 @@ impl ToComputedValue for MozFontSmoothingBackgroundColor { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { self.0 .to_computed_value(context) - .resolve_to_absolute(&AbsoluteColor::TRANSPARENT) + .resolve_to_absolute(&AbsoluteColor::transparent()) } fn from_computed_value(computed: &Self::ComputedValue) -> Self { diff --git a/servo/ports/geckolib/cbindgen.toml b/servo/ports/geckolib/cbindgen.toml index d78ea534d6c7..93f825882cb1 100644 --- a/servo/ports/geckolib/cbindgen.toml +++ b/servo/ports/geckolib/cbindgen.toml @@ -545,12 +545,10 @@ renaming_overrides_prefixing = true """ "GenericColor" = """ - static const StyleGenericColor TRANSPARENT; - static const StyleGenericColor BLACK; - static const StyleGenericColor WHITE; - static inline StyleGenericColor FromColor(nscolor); - + static inline StyleGenericColor Black(); + static inline StyleGenericColor White(); + static inline StyleGenericColor Transparent(); bool MaybeTransparent() const; /** @@ -581,6 +579,10 @@ renaming_overrides_prefixing = true */ static inline StyleAbsoluteColor Srgb(float red, float green, float blue, float alpha); + static inline StyleAbsoluteColor Transparent(); + static inline StyleAbsoluteColor Black(); + static inline StyleAbsoluteColor White(); + static inline StyleAbsoluteColor FromColor(nscolor); /**