1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
// Copyright 2006-2008 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdlib.h>
#include "skia/ext/platform_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/rect.h"
namespace color_utils {
TEST(ColorUtils, SkColorToHSLRed) {
HSL hsl = {0, 0, 0};
SkColorToHSL(SK_ColorRED, &hsl);
EXPECT_DOUBLE_EQ(hsl.h, 0);
EXPECT_DOUBLE_EQ(hsl.s, 1);
EXPECT_DOUBLE_EQ(hsl.l, 0.5);
}
TEST(ColorUtils, SkColorToHSLGrey) {
HSL hsl = {0, 0, 0};
SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
EXPECT_DOUBLE_EQ(hsl.h, 0);
EXPECT_DOUBLE_EQ(hsl.s, 0);
EXPECT_EQ(static_cast<int>(hsl.l * 100),
static_cast<int>(0.5 * 100)); // Accurate to two decimal places.
}
TEST(ColorUtils, HSLToSkColorWithAlpha) {
SkColor red = SkColorSetARGB(128, 255, 0, 0);
HSL hsl = {0, 1, 0.5};
SkColor result = HSLToSkColor(hsl, 128);
EXPECT_EQ(SkColorGetA(red), SkColorGetA(result));
EXPECT_EQ(SkColorGetR(red), SkColorGetR(result));
EXPECT_EQ(SkColorGetG(red), SkColorGetG(result));
EXPECT_EQ(SkColorGetB(red), SkColorGetB(result));
}
TEST(ColorUtils, RGBtoHSLRoundTrip) {
// Just spot check values near the edges.
for (int r = 0; r < 10; ++r) {
for (int g = 0; g < 10; ++g) {
for (int b = 0; b < 10; ++b) {
SkColor rgb = SkColorSetARGB(255, r, g, b);
HSL hsl = {0, 0, 0};
SkColorToHSL(rgb, &hsl);
SkColor out = HSLToSkColor(hsl, 255);
EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb));
EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb));
EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb));
}
}
}
for (int r = 240; r < 256; ++r) {
for (int g = 240; g < 256; ++g) {
for (int b = 240; b < 256; ++b) {
SkColor rgb = SkColorSetARGB(255, r, g, b);
HSL hsl = {0, 0, 0};
SkColorToHSL(rgb, &hsl);
SkColor out = HSLToSkColor(hsl, 255);
EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb));
EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb));
EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb));
}
}
}
}
TEST(ColorUtils, IsWithinHSLRange) {
HSL hsl = {0.3, 0.4, 0.5};
HSL lower = {0.2, 0.3, 0.4};
HSL upper = {0.4, 0.5, 0.6};
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
// Bounds are inclusive.
hsl.h = 0.2;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.h = 0.4;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.s = 0.3;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.s = 0.5;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.l = 0.4;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.l = 0.6;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
}
TEST(ColorUtils, IsWithinHSLRangeHueWrapAround) {
HSL hsl = {0.3, 0.4, 0.5};
HSL lower = {0.8, -1, -1};
HSL upper = {1.2, -1, -1};
hsl.h = 0.1;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.h = 0.9;
EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
hsl.h = 0.3;
EXPECT_FALSE(IsWithinHSLRange(hsl, lower, upper));
}
TEST(ColorUtils, IsHSLShiftMeaningful) {
HSL noop_all_neg_one{-1.0, -1.0, -1.0};
HSL noop_s_point_five{-1.0, 0.5, -1.0};
HSL noop_l_point_five{-1.0, -1.0, 0.5};
HSL only_h{0.1, -1.0, -1.0};
HSL only_s{-1.0, 0.1, -1.0};
HSL only_l{-1.0, -1.0, 0.1};
HSL only_hs{0.1, 0.1, -1.0};
HSL only_hl{0.1, -1.0, 0.1};
HSL only_sl{-1.0, 0.1, 0.1};
HSL all_set{0.1, 0.2, 0.3};
EXPECT_FALSE(IsHSLShiftMeaningful(noop_all_neg_one));
EXPECT_FALSE(IsHSLShiftMeaningful(noop_s_point_five));
EXPECT_FALSE(IsHSLShiftMeaningful(noop_l_point_five));
EXPECT_TRUE(IsHSLShiftMeaningful(only_h));
EXPECT_TRUE(IsHSLShiftMeaningful(only_s));
EXPECT_TRUE(IsHSLShiftMeaningful(only_l));
EXPECT_TRUE(IsHSLShiftMeaningful(only_hs));
EXPECT_TRUE(IsHSLShiftMeaningful(only_hl));
EXPECT_TRUE(IsHSLShiftMeaningful(only_sl));
EXPECT_TRUE(IsHSLShiftMeaningful(all_set));
}
TEST(ColorUtils, ColorToHSLRegisterSpill) {
// In a opt build on Linux, this was causing a register spill on my laptop
// (Pentium M) when converting from SkColor to HSL.
SkColor input = SkColorSetARGB(255, 206, 154, 89);
HSL hsl = {-1, -1, -1};
SkColor result = HSLShift(input, hsl);
// |result| should be the same as |input| since we passed in a value meaning
// no color shift.
EXPECT_EQ(SkColorGetA(input), SkColorGetA(result));
EXPECT_EQ(SkColorGetR(input), SkColorGetR(result));
EXPECT_EQ(SkColorGetG(input), SkColorGetG(result));
EXPECT_EQ(SkColorGetB(input), SkColorGetB(result));
}
TEST(ColorUtils, AlphaBlend) {
SkColor fore = SkColorSetARGB(255, 200, 200, 200);
SkColor back = SkColorSetARGB(255, 100, 100, 100);
EXPECT_TRUE(AlphaBlend(fore, back, 1.0f) == fore);
EXPECT_TRUE(AlphaBlend(fore, back, 0.0f) == back);
// One is fully transparent, result is partially transparent.
back = SkColorSetA(back, 0);
EXPECT_EQ(136U, SkColorGetA(AlphaBlend(fore, back, SkAlpha{136})));
// Both are fully transparent, result is fully transparent.
fore = SkColorSetA(fore, 0);
EXPECT_EQ(0U, SkColorGetA(AlphaBlend(fore, back, 1.0f)));
}
TEST(ColorUtils, SkColorToRgbaString) {
SkColor color = SkColorSetARGB(153, 100, 150, 200);
std::string color_string = SkColorToRgbaString(color);
EXPECT_EQ(color_string, "rgba(100,150,200,0.6)");
}
TEST(ColorUtils, SkColorToRgbString) {
SkColor color = SkColorSetARGB(200, 50, 100, 150);
std::string color_string = SkColorToRgbString(color);
EXPECT_EQ(color_string, "50,100,150");
}
TEST(ColorUtils, IsDarkDarkestColorChange) {
ASSERT_FALSE(IsDark(SK_ColorLTGRAY));
const SkColor old_darkest_color = SetDarkestColorForTesting(SK_ColorLTGRAY);
EXPECT_TRUE(IsDark(SK_ColorLTGRAY));
SetDarkestColorForTesting(old_darkest_color);
}
TEST(ColorUtils, MidpointLuminanceMatches) {
const SkColor old_darkest_color = SetDarkestColorForTesting(SK_ColorBLACK);
auto [darkest, midpoint, lightest] = GetLuminancesForTesting();
EXPECT_FLOAT_EQ(GetContrastRatio(darkest, midpoint),
GetContrastRatio(midpoint, lightest));
SetDarkestColorForTesting(old_darkest_color);
std::tie(darkest, midpoint, lightest) = GetLuminancesForTesting();
EXPECT_FLOAT_EQ(GetContrastRatio(darkest, midpoint),
GetContrastRatio(midpoint, lightest));
}
TEST(ColorUtils, GetColorWithMaxContrast) {
const SkColor old_darkest_color = SetDarkestColorForTesting(SK_ColorBLACK);
EXPECT_EQ(SK_ColorWHITE, GetColorWithMaxContrast(SK_ColorBLACK));
EXPECT_EQ(SK_ColorWHITE,
GetColorWithMaxContrast(SkColorSetRGB(0x75, 0x75, 0x75)));
EXPECT_EQ(SK_ColorBLACK, GetColorWithMaxContrast(SK_ColorWHITE));
EXPECT_EQ(SK_ColorBLACK,
GetColorWithMaxContrast(SkColorSetRGB(0x76, 0x76, 0x76)));
SetDarkestColorForTesting(old_darkest_color);
EXPECT_EQ(old_darkest_color, GetColorWithMaxContrast(SK_ColorWHITE));
}
TEST(ColorUtils, GetEndpointColorWithMinContrast) {
const SkColor old_darkest_color = SetDarkestColorForTesting(SK_ColorBLACK);
EXPECT_EQ(SK_ColorBLACK, GetEndpointColorWithMinContrast(SK_ColorBLACK));
EXPECT_EQ(SK_ColorBLACK,
GetEndpointColorWithMinContrast(SkColorSetRGB(0x75, 0x75, 0x75)));
EXPECT_EQ(SK_ColorWHITE, GetEndpointColorWithMinContrast(SK_ColorWHITE));
EXPECT_EQ(SK_ColorWHITE,
GetEndpointColorWithMinContrast(SkColorSetRGB(0x76, 0x76, 0x76)));
SetDarkestColorForTesting(old_darkest_color);
EXPECT_EQ(old_darkest_color,
GetEndpointColorWithMinContrast(old_darkest_color));
}
TEST(ColorUtils, BlendForMinContrast_ForegroundAlreadyMeetsMinimum) {
const auto result = BlendForMinContrast(SK_ColorBLACK, SK_ColorWHITE);
EXPECT_EQ(SK_AlphaTRANSPARENT, result.alpha);
EXPECT_EQ(SK_ColorBLACK, result.color);
}
TEST(ColorUtils, BlendForMinContrast_BlendDarker) {
const SkColor foreground = SkColorSetRGB(0xAA, 0xAA, 0xAA);
const auto result = BlendForMinContrast(foreground, SK_ColorWHITE);
EXPECT_NE(SK_AlphaTRANSPARENT, result.alpha);
EXPECT_NE(foreground, result.color);
EXPECT_GE(GetContrastRatio(result.color, SK_ColorWHITE),
kMinimumReadableContrastRatio);
}
TEST(ColorUtils, BlendForMinContrast_BlendLighter) {
const SkColor foreground = SkColorSetRGB(0x33, 0x33, 0x33);
const auto result = BlendForMinContrast(foreground, SK_ColorBLACK);
EXPECT_NE(SK_AlphaTRANSPARENT, result.alpha);
EXPECT_NE(foreground, result.color);
EXPECT_GE(GetContrastRatio(result.color, SK_ColorBLACK),
kMinimumReadableContrastRatio);
}
TEST(ColorUtils, BlendForMinContrast_StopsAtDarkestColor) {
const SkColor darkest_color = SkColorSetRGB(0x44, 0x44, 0x44);
const SkColor old_darkest_color = SetDarkestColorForTesting(darkest_color);
EXPECT_EQ(darkest_color, BlendForMinContrast(SkColorSetRGB(0x55, 0x55, 0x55),
SkColorSetRGB(0xAA, 0xAA, 0xAA))
.color);
SetDarkestColorForTesting(old_darkest_color);
}
TEST(ColorUtils, BlendForMinContrast_ComputesExpectedOpacities) {
const SkColor source = SkColorSetRGB(0xDE, 0xE1, 0xE6);
const SkColor target = SkColorSetRGB(0xFF, 0xFF, 0xFF);
const SkColor base = source;
SkAlpha alpha = BlendForMinContrast(source, base, target, 1.11f).alpha;
EXPECT_NEAR(alpha / 255.0f, 0.4f, 0.03f);
alpha = BlendForMinContrast(source, base, target, 1.19f).alpha;
EXPECT_NEAR(alpha / 255.0f, 0.65f, 0.03f);
alpha = BlendForMinContrast(source, base, target, 1.13728f).alpha;
EXPECT_NEAR(alpha / 255.0f, 0.45f, 0.03f);
}
TEST(ColorUtils, BlendTowardMaxContrast_PreservesAlpha) {
SkColor test_colors[] = {SK_ColorBLACK, SK_ColorWHITE,
SK_ColorRED, SK_ColorYELLOW,
SK_ColorMAGENTA, gfx::kGoogleGreen500,
gfx::kGoogleRed050, gfx::kGoogleBlue800};
SkAlpha test_alphas[] = {SK_AlphaTRANSPARENT, 0x0F,
gfx::kDisabledControlAlpha, 0xDD};
SkAlpha blend_alpha = 0x7F;
for (const SkColor color : test_colors) {
SkColor opaque_result =
color_utils::BlendTowardMaxContrast(color, blend_alpha);
for (const SkAlpha alpha : test_alphas) {
SkColor input = SkColorSetA(color, alpha);
SkColor result = color_utils::BlendTowardMaxContrast(input, blend_alpha);
// Alpha was preserved.
EXPECT_EQ(SkColorGetA(result), alpha);
// RGB channels unaffected by alpha of input.
EXPECT_EQ(SkColorSetA(result, SK_AlphaOPAQUE), opaque_result);
}
}
}
TEST(ColorUtils, BlendForMinContrast_MatchesNaiveImplementation) {
constexpr SkColor default_foreground = SkColorSetRGB(0xDE, 0xE1, 0xE6);
constexpr SkColor high_contrast_foreground = SK_ColorWHITE;
constexpr SkColor background = default_foreground;
constexpr float kContrastRatio = 1.11f;
const auto result = BlendForMinContrast(
default_foreground, background, high_contrast_foreground, kContrastRatio);
// Naive implementation is direct translation of function description.
SkAlpha alpha = SK_AlphaTRANSPARENT;
SkColor color = default_foreground;
for (int i = SK_AlphaTRANSPARENT; i <= SK_AlphaOPAQUE; ++i) {
alpha = static_cast<SkAlpha>(i);
color = AlphaBlend(high_contrast_foreground, default_foreground, alpha);
if (GetContrastRatio(color, background) >= kContrastRatio)
break;
}
EXPECT_EQ(alpha, result.alpha);
EXPECT_EQ(color, result.color);
}
TEST(ColorUtils, PickGoogleColor) {
// If the input color already has sufficient contrast, it should be accepted.
EXPECT_EQ(gfx::kGoogleBlue800,
PickGoogleColor(gfx::kGoogleBlue800, gfx::kGoogleBlue700, 1.1f));
EXPECT_EQ(gfx::kGoogleBlue600,
PickGoogleColor(gfx::kGoogleBlue600, gfx::kGoogleBlue700, 1.1f));
// If it does not, it should stay on the same side of the background if
// possible.
EXPECT_EQ(gfx::kGoogleBlue900,
PickGoogleColor(gfx::kGoogleBlue800, gfx::kGoogleBlue700, 1.25f));
EXPECT_EQ(gfx::kGoogleBlue500,
PickGoogleColor(gfx::kGoogleBlue600, gfx::kGoogleBlue700, 1.25f));
// If even Blue 900 does not contrast enough, Grey 900 is a slightly darker
// color.
EXPECT_EQ(gfx::kGoogleGrey900,
PickGoogleColor(gfx::kGoogleBlue800, gfx::kGoogleBlue700, 1.5f));
// If no dark colors have enough contrast, the result should be a lighter
// color instead.
EXPECT_EQ(gfx::kGoogleBlue200,
PickGoogleColor(gfx::kGoogleBlue800, gfx::kGoogleBlue700, 3.0f));
// If the requested contrast is too high for any color to be sufficient, the
// result should be the most-contrasting endpoint.
EXPECT_EQ(SK_ColorWHITE,
PickGoogleColor(gfx::kGoogleBlue800, gfx::kGoogleBlue700,
kMaximumPossibleContrast));
// Matching the background exactly is reasonable, if the minimum contrast is
// zero.
EXPECT_EQ(
gfx::kGoogleBlue700,
PickGoogleColor(gfx::kGoogleBlue800, gfx::kGoogleBlue700, 0.0f, 1.2f));
// Blue 600 is the only color that fits in the requested contrast window, but
// it's on the other side of the background from the input, so something
// closer to the input is used instead.
EXPECT_EQ(
gfx::kGoogleBlue800,
PickGoogleColor(gfx::kGoogleBlue900, gfx::kGoogleBlue700, 1.18f, 1.2f));
}
TEST(ColorUtils, PickGoogleColorTwoBackgrounds) {
// If the input color already has sufficient contrast, it should be accepted.
EXPECT_EQ(gfx::kGoogleBlue800, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue800, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.1f));
EXPECT_EQ(gfx::kGoogleBlue600, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue600, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.1f));
EXPECT_EQ(gfx::kGoogleBlue300, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue300, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.1f));
EXPECT_EQ(gfx::kGoogleBlue100, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue100, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.1f));
// If it does not, it should stay on the same side of the background if
// possible.
EXPECT_EQ(gfx::kGoogleBlue900, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue800, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.25f));
EXPECT_EQ(gfx::kGoogleBlue500, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue600, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.25f));
EXPECT_EQ(gfx::kGoogleBlue400, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue300, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.3f));
EXPECT_EQ(gfx::kGoogleBlue050, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue100, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.3f));
// If the blue endpoints do not contrast enough, the grey endpoints are
// available.
EXPECT_EQ(gfx::kGoogleGrey900, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue800, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.5f));
EXPECT_EQ(SK_ColorWHITE, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue100, gfx::kGoogleBlue700,
gfx::kGoogleBlue200, 1.5f));
// If it's not possible to achieve sufficient contrast on the same side of the
// background, then the result color should cross to the other side.
EXPECT_EQ(gfx::kGoogleBlue500, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue100, gfx::kGoogleBlue200,
gfx::kGoogleBlue900, 1.7f));
EXPECT_EQ(gfx::kGoogleBlue100, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue800, gfx::kGoogleBlue700,
gfx::kGoogleBlue600, 3.0f));
// If the requested contrast is too high for any color to be sufficient, the
// result should be the most-contrasting point.
EXPECT_EQ(SK_ColorWHITE, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue800, gfx::kGoogleBlue700,
gfx::kGoogleBlue600, kMaximumPossibleContrast));
EXPECT_EQ(gfx::kGoogleGrey900,
PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue100, gfx::kGoogleBlue200, gfx::kGoogleBlue300,
kMaximumPossibleContrast));
EXPECT_EQ(gfx::kGoogleBlue400,
PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue100, gfx::kGoogleBlue900, gfx::kGoogleBlue050,
kMaximumPossibleContrast));
// Matching the background exactly is reasonable, if the minimum contrast is
// zero.
EXPECT_EQ(gfx::kGoogleBlue700, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue800, gfx::kGoogleBlue700,
gfx::kGoogleBlue600, 0.0f, 1.2f));
// Blue 600 is the only color that fits in the requested contrast window, but
// it's on the other side of the background from the input, so something
// closer to the input is used instead.
EXPECT_EQ(gfx::kGoogleBlue800, PickGoogleColorTwoBackgrounds(
gfx::kGoogleBlue900, gfx::kGoogleBlue700,
gfx ::kGoogleBlue500, 1.18f, 1.2f));
}
} // namespace color_utils
|