| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/gfx/color_utils.h" | 5 #include "app/gfx/color_utils.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #if defined(OS_WIN) | 8 #if defined(OS_WIN) |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #endif | 10 #endif |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 int pixel_width = bitmap->width(); | 243 int pixel_width = bitmap->width(); |
| 244 int pixel_height = bitmap->height(); | 244 int pixel_height = bitmap->height(); |
| 245 for (int y = 0; y < pixel_height; ++y) { | 245 for (int y = 0; y < pixel_height; ++y) { |
| 246 SkColor* current_color = static_cast<uint32_t*>(bitmap->getAddr32(0, y)); | 246 SkColor* current_color = static_cast<uint32_t*>(bitmap->getAddr32(0, y)); |
| 247 for (int x = 0; x < pixel_width; ++x, ++current_color) { | 247 for (int x = 0; x < pixel_width; ++x, ++current_color) { |
| 248 histogram[GetLumaForColor(current_color)]++; | 248 histogram[GetLumaForColor(current_color)]++; |
| 249 } | 249 } |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 SkColor SetColorAlpha(SkColor c, SkAlpha alpha) { | 253 SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha) { |
| 254 return SkColorSetARGB(alpha, SkColorGetR(c), SkColorGetG(c), SkColorGetB(c)); | 254 if (alpha == 0) |
| 255 return background; |
| 256 else if (alpha == 0xFF) |
| 257 return foreground; |
| 258 |
| 259 return SkColorSetRGB( |
| 260 ((SkColorGetR(foreground) * alpha) + |
| 261 (SkColorGetR(background) * (0xFF - alpha))) / 0xFF, |
| 262 ((SkColorGetG(foreground) * alpha) + |
| 263 (SkColorGetG(background) * (0xFF - alpha))) / 0xFF, |
| 264 ((SkColorGetB(foreground) * alpha) + |
| 265 (SkColorGetB(background) * (0xFF - alpha))) / 0xFF); |
| 266 } |
| 267 |
| 268 // Next three functions' formulas from: |
| 269 // http://www.w3.org/TR/WCAG20/#relativeluminancedef |
| 270 // http://www.w3.org/TR/WCAG20/#contrast-ratiodef |
| 271 static double ConvertSRGB(double eight_bit_component) { |
| 272 const double component = eight_bit_component / 255.0; |
| 273 return (component <= 0.03928) ? |
| 274 (component / 12.92) : pow((component + 0.055) / 1.055, 2.4); |
| 275 } |
| 276 |
| 277 static double RelativeLuminance(SkColor color) { |
| 278 return (0.2126 * ConvertSRGB(SkColorGetR(color))) + |
| 279 (0.7152 * ConvertSRGB(SkColorGetG(color))) + |
| 280 (0.0722 * ConvertSRGB(SkColorGetB(color))); |
| 281 } |
| 282 |
| 283 static double ContrastRatio(SkColor color1, SkColor color2) { |
| 284 const double l1 = RelativeLuminance(color1) + 0.05; |
| 285 const double l2 = RelativeLuminance(color2) + 0.05; |
| 286 return (l1 > l2) ? (l1 / l2) : (l2 / l1); |
| 287 } |
| 288 |
| 289 SkColor PickMoreReadableColor(SkColor foreground1, |
| 290 SkColor foreground2, |
| 291 SkColor background) { |
| 292 return (ContrastRatio(foreground1, background) >= |
| 293 ContrastRatio(foreground2, background)) ? foreground1 : foreground2; |
| 255 } | 294 } |
| 256 | 295 |
| 257 SkColor GetSysSkColor(int which) { | 296 SkColor GetSysSkColor(int which) { |
| 258 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
| 259 return skia::COLORREFToSkColor(::GetSysColor(which)); | 298 return skia::COLORREFToSkColor(::GetSysColor(which)); |
| 260 #else | 299 #else |
| 261 NOTIMPLEMENTED(); | 300 NOTIMPLEMENTED(); |
| 262 return SK_ColorLTGRAY; | 301 return SK_ColorLTGRAY; |
| 263 #endif | 302 #endif |
| 264 } | 303 } |
| 265 | 304 |
| 266 } // namespace color_utils | 305 } // namespace color_utils |
| OLD | NEW |