Skip to content

Commit

Permalink
blink::Color: Ensure that FromRGB and FromRGBA be constexpr
Browse files Browse the repository at this point in the history
This is required to convert several locations in blink/renderer/core
from using the implicit blink::Color constructor from RGBA32.

Also update a missed implicit conversion in
CanvasRenderingContext2DState and GraphicsContext.

Bug: 1351544
Change-Id: I4ae5a98bfabdd6c0d5c1ba0f25bddf4ff0ca89c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3825960
Commit-Queue: ccameron chromium <ccameron@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1033999}
  • Loading branch information
ccameron-chromium authored and Chromium LUCI CQ committed Aug 11, 2022
1 parent b13a076 commit ebe342f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ sk_sp<SkDrawLooper>& CanvasRenderingContext2DState::ShadowOnlyDrawLooper()
if (!shadow_only_draw_looper_) {
DrawLooperBuilder draw_looper_builder;
draw_looper_builder.AddShadow(shadow_offset_, ClampTo<float>(shadow_blur_),
shadow_color_,
Color::FromSkColor(shadow_color_),
DrawLooperBuilder::kShadowIgnoresTransforms,
DrawLooperBuilder::kShadowRespectsAlpha);
shadow_only_draw_looper_ = draw_looper_builder.DetachDrawLooper();
Expand All @@ -584,7 +584,7 @@ CanvasRenderingContext2DState::ShadowAndForegroundDrawLooper() const {
if (!shadow_and_foreground_draw_looper_) {
DrawLooperBuilder draw_looper_builder;
draw_looper_builder.AddShadow(shadow_offset_, ClampTo<float>(shadow_blur_),
shadow_color_,
Color::FromSkColor(shadow_color_),
DrawLooperBuilder::kShadowIgnoresTransforms,
DrawLooperBuilder::kShadowRespectsAlpha);
draw_looper_builder.AddUnmodifiedContent();
Expand Down
10 changes: 5 additions & 5 deletions third_party/blink/renderer/platform/graphics/color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ Color::operator SkColor() const {

Color Color::Light() const {
// Hardcode this common case for speed.
if (color_ == kBlack)
return kLightenedBlack;
if (color_ == kBlack.color_)
return Color(kLightenedBlack);

const float scale_factor = nextafterf(256.0f, 0.0f);

Expand All @@ -327,8 +327,8 @@ Color Color::Light() const {

Color Color::Dark() const {
// Hardcode this common case for speed.
if (color_ == kWhite)
return kDarkenedWhite;
if (color_ == kWhite.color_)
return Color(kDarkenedWhite);

const float scale_factor = nextafterf(256.0f, 0.0f);

Expand All @@ -346,7 +346,7 @@ Color Color::Dark() const {
Color Color::CombineWithAlpha(float other_alpha) const {
RGBA32 rgb_only = Rgb() & 0x00FFFFFF;
float override_alpha = (Alpha() / 255.f) * other_alpha;
return rgb_only | ColorFloatToRGBAByte(override_alpha) << 24;
return Color(rgb_only | ColorFloatToRGBAByte(override_alpha) << 24);
}

Color Color::Blend(const Color& source) const {
Expand Down
11 changes: 8 additions & 3 deletions third_party/blink/renderer/platform/graphics/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ class PLATFORM_EXPORT Color {

// Create a color using rgb() syntax.
static constexpr Color FromRGB(int r, int g, int b) {
return Color(MakeRGB(r, g, b));
return Color(0xFF000000 | ClampInt(r) << 16 | ClampInt(g) << 8 |
ClampInt(b));
}

// Create a color using rgba() syntax.
static constexpr Color FromRGBA(int r, int g, int b, int a) {
return Color(MakeRGBA(r, g, b, a));
return Color(ClampInt(a) << 24 | ClampInt(r) << 16 | ClampInt(g) << 8 |
ClampInt(b));
}

// Create a color using the hsl() syntax.
Expand All @@ -127,7 +129,7 @@ class PLATFORM_EXPORT Color {
// TODO(crbug.com/1308932): These three functions are just helpers for while
// we're converting platform/graphics to float color.
static Color FromSkColor4f(SkColor4f fc) {
return MakeRGBA32FromFloats(fc.fR, fc.fG, fc.fB, fc.fA);
return Color(MakeRGBA32FromFloats(fc.fR, fc.fG, fc.fB, fc.fA));
}
static constexpr Color FromSkColor(SkColor color) { return Color(color); }
static constexpr Color FromRGBA32(RGBA32 color) { return Color(color); }
Expand Down Expand Up @@ -189,6 +191,9 @@ class PLATFORM_EXPORT Color {
static const Color kTransparent;

private:
static constexpr int ClampInt(int x) {
return x < 0 ? 0 : (x > 255 ? 255 : x);
}
void GetHueMaxMin(double&, double&, double&) const;

RGBA32 color_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ Color DarkModeColor(GraphicsContext& context,
if (auto_dark_mode.enabled) {
// TODO(https://crbug.com/1351544): DarkModeFilter should operate on
// SkColor4f, not SkColor.
return context.GetDarkModeFilter()->InvertColorIfNeeded(
SkColor(color), auto_dark_mode.role);
return Color::FromSkColor(context.GetDarkModeFilter()->InvertColorIfNeeded(
SkColor(color), auto_dark_mode.role));
}
return color;
}
Expand Down

0 comments on commit ebe342f

Please sign in to comment.