Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: ui/gfx/color_utils.cc

Issue 1683913003: Round color values in AlphaBlend() as we already do in HSLShift(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/color_utils.h" 5 #include "ui/gfx/color_utils.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 else 143 else
144 light = static_cast<uint8_t>(SkDoubleToFixed(lightness) >> 8); 144 light = static_cast<uint8_t>(SkDoubleToFixed(lightness) >> 8);
145 145
146 return SkColorSetARGB(alpha, light, light, light); 146 return SkColorSetARGB(alpha, light, light, light);
147 } 147 }
148 148
149 double temp2 = (lightness < 0.5) ? 149 double temp2 = (lightness < 0.5) ?
150 (lightness * (1.0 + saturation)) : 150 (lightness * (1.0 + saturation)) :
151 (lightness + saturation - (lightness * saturation)); 151 (lightness + saturation - (lightness * saturation));
152 double temp1 = 2.0 * lightness - temp2; 152 double temp1 = 2.0 * lightness - temp2;
153 return SkColorSetARGB(alpha, 153 return SkColorSetARGB(alpha, calcHue(temp1, temp2, hue + 1.0 / 3.0),
154 calcHue(temp1, temp2, hue + 1.0 / 3.0), 154 calcHue(temp1, temp2, hue),
155 calcHue(temp1, temp2, hue), 155 calcHue(temp1, temp2, hue - 1.0 / 3.0));
156 calcHue(temp1, temp2, hue - 1.0 / 3.0));
157 } 156 }
158 157
159 bool IsWithinHSLRange(const HSL& hsl, 158 bool IsWithinHSLRange(const HSL& hsl,
160 const HSL& lower_bound, 159 const HSL& lower_bound,
161 const HSL& upper_bound) { 160 const HSL& upper_bound) {
162 DCHECK(hsl.h >= 0 && hsl.h <= 1) << hsl.h; 161 DCHECK(hsl.h >= 0 && hsl.h <= 1) << hsl.h;
163 DCHECK(hsl.s >= 0 && hsl.s <= 1) << hsl.s; 162 DCHECK(hsl.s >= 0 && hsl.s <= 1) << hsl.s;
164 DCHECK(hsl.l >= 0 && hsl.l <= 1) << hsl.l; 163 DCHECK(hsl.l >= 0 && hsl.l <= 1) << hsl.l;
165 DCHECK(lower_bound.h < 0 || upper_bound.h < 0 || 164 DCHECK(lower_bound.h < 0 || upper_bound.h < 0 ||
166 (lower_bound.h <= 1 && upper_bound.h <= lower_bound.h + 1)) 165 (lower_bound.h <= 1 && upper_bound.h <= lower_bound.h + 1))
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 double f_weight = f_alpha * alpha / normalizer; 279 double f_weight = f_alpha * alpha / normalizer;
281 double b_weight = b_alpha * (255 - alpha) / normalizer; 280 double b_weight = b_alpha * (255 - alpha) / normalizer;
282 281
283 double r = (SkColorGetR(foreground) * f_weight + 282 double r = (SkColorGetR(foreground) * f_weight +
284 SkColorGetR(background) * b_weight) / 255.0; 283 SkColorGetR(background) * b_weight) / 255.0;
285 double g = (SkColorGetG(foreground) * f_weight + 284 double g = (SkColorGetG(foreground) * f_weight +
286 SkColorGetG(background) * b_weight) / 255.0; 285 SkColorGetG(background) * b_weight) / 255.0;
287 double b = (SkColorGetB(foreground) * f_weight + 286 double b = (SkColorGetB(foreground) * f_weight +
288 SkColorGetB(background) * b_weight) / 255.0; 287 SkColorGetB(background) * b_weight) / 255.0;
289 288
290 return SkColorSetARGB(static_cast<int>(normalizer), 289 return SkColorSetARGB(static_cast<int>(std::round(normalizer)),
291 static_cast<int>(r), 290 static_cast<int>(std::round(r)),
292 static_cast<int>(g), 291 static_cast<int>(std::round(g)),
293 static_cast<int>(b)); 292 static_cast<int>(std::round(b)));
294 } 293 }
295 294
296 bool IsDark(SkColor color) { 295 bool IsDark(SkColor color) {
297 return GetLuminanceForColor(color) < 128; 296 return GetLuminanceForColor(color) < 128;
298 } 297 }
299 298
300 SkColor BlendTowardOppositeLuminance(SkColor color, SkAlpha alpha) { 299 SkColor BlendTowardOppositeLuminance(SkColor color, SkAlpha alpha) {
301 return AlphaBlend(IsDark(color) ? SK_ColorWHITE : SK_ColorBLACK, color, 300 return AlphaBlend(IsDark(color) ? SK_ColorWHITE : SK_ColorBLACK, color,
302 alpha); 301 alpha);
303 } 302 }
304 303
305 SkColor GetReadableColor(SkColor foreground, SkColor background) { 304 SkColor GetReadableColor(SkColor foreground, SkColor background) {
306 const SkColor foreground2 = LumaInvertColor(foreground); 305 const SkColor foreground2 = LumaInvertColor(foreground);
307 const double background_luminance = RelativeLuminance(background); 306 const double background_luminance = RelativeLuminance(background);
308 return (ContrastRatio(RelativeLuminance(foreground), background_luminance) >= 307 return (ContrastRatio(RelativeLuminance(foreground), background_luminance) >=
309 ContrastRatio(RelativeLuminance(foreground2), background_luminance)) ? 308 ContrastRatio(RelativeLuminance(foreground2), background_luminance)) ?
310 foreground : foreground2; 309 foreground : foreground2;
311 } 310 }
312 311
313 SkColor InvertColor(SkColor color) { 312 SkColor InvertColor(SkColor color) {
314 return SkColorSetARGB( 313 return SkColorSetARGB(SkColorGetA(color), 255 - SkColorGetR(color),
315 SkColorGetA(color), 314 255 - SkColorGetG(color), 255 - SkColorGetB(color));
316 255 - SkColorGetR(color),
317 255 - SkColorGetG(color),
318 255 - SkColorGetB(color));
319 } 315 }
320 316
321 SkColor GetSysSkColor(int which) { 317 SkColor GetSysSkColor(int which) {
322 #if defined(OS_WIN) 318 #if defined(OS_WIN)
323 return skia::COLORREFToSkColor(GetSysColor(which)); 319 return skia::COLORREFToSkColor(GetSysColor(which));
324 #else 320 #else
325 NOTIMPLEMENTED(); 321 NOTIMPLEMENTED();
326 return SK_ColorLTGRAY; 322 return SK_ColorLTGRAY;
327 #endif 323 #endif
328 } 324 }
(...skipping 11 matching lines...) Expand all
340 if (IsDark(text_color)) { 336 if (IsDark(text_color)) {
341 // For black text, this comes out to kChromeIconGrey. 337 // For black text, this comes out to kChromeIconGrey.
342 return color_utils::AlphaBlend(SK_ColorWHITE, text_color, 338 return color_utils::AlphaBlend(SK_ColorWHITE, text_color,
343 SkColorGetR(gfx::kChromeIconGrey)); 339 SkColorGetR(gfx::kChromeIconGrey));
344 } 340 }
345 // The dimming is less dramatic when darkening a light color. 341 // The dimming is less dramatic when darkening a light color.
346 return color_utils::AlphaBlend(SK_ColorBLACK, text_color, 0x33); 342 return color_utils::AlphaBlend(SK_ColorBLACK, text_color, 0x33);
347 } 343 }
348 344
349 } // namespace color_utils 345 } // namespace color_utils
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698