Skip to content

Commit

Permalink
Bug 1846249 - Change color functions to constants r=dholbert
Browse files Browse the repository at this point in the history
  • Loading branch information
tiaanl committed Aug 1, 2023
1 parent 99602c8 commit cfd3c0c
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 86 deletions.
2 changes: 1 addition & 1 deletion gfx/src/nsFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion layout/generic/nsImageFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions layout/painting/nsCSSRenderingGradients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ static StyleAbsoluteColor GetSpecifiedColor(
const StyleGenericGradientItem<StyleColor, T>& aItem,
const ComputedStyle& aStyle) {
if (aItem.IsInterpolationHint()) {
return StyleAbsoluteColor::Transparent();
return StyleAbsoluteColor::TRANSPARENT;
}
const StyleColor& c = aItem.IsSimpleColorStop()
? aItem.AsSimpleColorStop()
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 9 additions & 21 deletions layout/style/StyleColorInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,25 @@ 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 StyleColor StyleColor::Black() {
return FromColor(NS_RGB(0, 0, 0));
}
inline const StyleColor StyleColor::TRANSPARENT =
StyleColor::Absolute(StyleAbsoluteColor::TRANSPARENT);

// static
template <>
inline StyleColor StyleColor::White() {
return FromColor(NS_RGB(255, 255, 255));
}
inline const StyleColor StyleColor::BLACK =
StyleColor::Absolute(StyleAbsoluteColor::BLACK);

// static
template <>
inline StyleColor StyleColor::Transparent() {
return FromColor(NS_RGBA(0, 0, 0, 0));
}
inline const StyleColor StyleColor::WHITE =
StyleColor::Absolute(StyleAbsoluteColor::WHITE);

template <>
StyleAbsoluteColor StyleColor::ResolveColor(const StyleAbsoluteColor&) const;
Expand Down
10 changes: 5 additions & 5 deletions layout/style/nsStyleStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ using SVGPaintFallback = StyleGenericSVGPaintFallback<StyleColor>;
// nsStyleSVG
//
nsStyleSVG::nsStyleSVG()
: mFill{StyleSVGPaintKind::Color(StyleColor::Black()),
: mFill{StyleSVGPaintKind::Color(StyleColor::BLACK),
SVGPaintFallback::Unset()},
mStroke{StyleSVGPaintKind::None(), SVGPaintFallback::Unset()},
mMarkerEnd(StyleUrlOrNone::None()),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -2037,7 +2037,7 @@ nsChangeHint nsStyleImageLayers::Layer::CalcDifference(

nsStyleBackground::nsStyleBackground()
: mImage(nsStyleImageLayers::LayerType::Background),
mBackgroundColor(StyleColor::Transparent()) {
mBackgroundColor(StyleColor::TRANSPARENT) {
MOZ_COUNT_CTOR(nsStyleBackground);
}

Expand Down
39 changes: 24 additions & 15 deletions servo/components/style/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ 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 {
Expand Down Expand Up @@ -223,21 +247,6 @@ 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] {
Expand Down
2 changes: 1 addition & 1 deletion servo/components/style/properties/cascade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion servo/components/style/properties/longhands/font.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions servo/components/style/properties/longhands/svg.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions servo/components/style/values/animated/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Animate for Color {
impl ComputeSquaredDistance for Color {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
let current_color = AbsoluteColor::transparent();
let current_color = AbsoluteColor::TRANSPARENT;
self.resolve_to_absolute(&current_color)
.compute_squared_distance(&other.resolve_to_absolute(&current_color))
}
Expand All @@ -83,6 +83,6 @@ impl ComputeSquaredDistance for Color {
impl ToAnimatedZero for Color {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
Ok(Color::Absolute(AbsoluteColor::transparent()))
Ok(Color::Absolute(AbsoluteColor::TRANSPARENT))
}
}
26 changes: 10 additions & 16 deletions servo/components/style/values/computed/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ 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 {
Expand All @@ -52,21 +61,6 @@ 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 {
Expand All @@ -93,7 +87,7 @@ impl Color {

impl ToAnimatedZero for AbsoluteColor {
fn to_animated_zero(&self) -> Result<Self, ()> {
Ok(Self::transparent())
Ok(Self::TRANSPARENT)
}
}

Expand Down
10 changes: 4 additions & 6 deletions servo/components/style/values/computed/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ pub type SVGPaintKind = generic::GenericSVGPaintKind<Color, ComputedUrl>;

impl SVGPaint {
/// Opaque black color
pub fn black() -> Self {
SVGPaint {
kind: generic::SVGPaintKind::Color(Color::black()),
fallback: generic::SVGPaintFallback::Unset,
}
}
pub const BLACK: Self = Self {
kind: generic::SVGPaintKind::Color(Color::BLACK),
fallback: generic::SVGPaintFallback::Unset,
};
}

/// <length> | <percentage> | <number> | context-value
Expand Down
4 changes: 2 additions & 2 deletions servo/components/style/values/specified/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions servo/ports/geckolib/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ header = """/* This Source Code Form is subject to the terms of the Mozilla Publ
// Work-around silly windows.h define.
#pragma push_macro("STRICT")
#undef STRICT
#pragma push_macro("TRANSPARENT")
#undef TRANSPARENT
"""
trailer = """
#pragma pop_macro("STRICT")
#pragma pop_macro("TRANSPARENT")
#pragma GCC diagnostic pop
#include "mozilla/ServoStyleConstsInlines.h"
"""
Expand Down Expand Up @@ -546,10 +549,12 @@ 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;
/**
Expand Down Expand Up @@ -580,10 +585,6 @@ 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);
/**
Expand Down

0 comments on commit cfd3c0c

Please sign in to comment.