Home | History | Annotate | Download | only in gfx
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ui/gfx/color_utils.h"
      6 
      7 #include <math.h>
      8 #if defined(OS_WIN)
      9 #include <windows.h>
     10 #endif
     11 
     12 #include <algorithm>
     13 
     14 #include "base/basictypes.h"
     15 #include "base/logging.h"
     16 #include "build/build_config.h"
     17 #if defined(OS_WIN)
     18 #include "skia/ext/skia_utils_win.h"
     19 #endif
     20 #include "third_party/skia/include/core/SkBitmap.h"
     21 
     22 namespace color_utils {
     23 
     24 
     25 // Helper functions -----------------------------------------------------------
     26 
     27 namespace {
     28 
     29 int calcHue(double temp1, double temp2, double hue) {
     30   if (hue < 0.0)
     31     ++hue;
     32   else if (hue > 1.0)
     33     --hue;
     34 
     35   double result = temp1;
     36   if (hue * 6.0 < 1.0)
     37     result = temp1 + (temp2 - temp1) * hue * 6.0;
     38   else if (hue * 2.0 < 1.0)
     39     result = temp2;
     40   else if (hue * 3.0 < 2.0)
     41     result = temp1 + (temp2 - temp1) * (2.0 / 3.0 - hue) * 6.0;
     42 
     43   // Scale the result from 0 - 255 and round off the value.
     44   return static_cast<int>(result * 255 + .5);
     45 }
     46 
     47 // Next two functions' formulas from:
     48 // http://www.w3.org/TR/WCAG20/#relativeluminancedef
     49 // http://www.w3.org/TR/WCAG20/#contrast-ratiodef
     50 
     51 double ConvertSRGB(double eight_bit_component) {
     52   const double component = eight_bit_component / 255.0;
     53   return (component <= 0.03928) ?
     54       (component / 12.92) : pow((component + 0.055) / 1.055, 2.4);
     55 }
     56 
     57 SkColor LumaInvertColor(SkColor color) {
     58   HSL hsl;
     59   SkColorToHSL(color, &hsl);
     60   hsl.l = 1.0 - hsl.l;
     61   return HSLToSkColor(hsl, 255);
     62 }
     63 
     64 double ContrastRatio(double foreground_luminance, double background_luminance) {
     65   DCHECK_GE(foreground_luminance, 0.0);
     66   DCHECK_GE(background_luminance, 0.0);
     67   foreground_luminance += 0.05;
     68   background_luminance += 0.05;
     69   return (foreground_luminance > background_luminance) ?
     70       (foreground_luminance / background_luminance) :
     71       (background_luminance / foreground_luminance);
     72 }
     73 
     74 }  // namespace
     75 
     76 
     77 // ----------------------------------------------------------------------------
     78 
     79 unsigned char GetLuminanceForColor(SkColor color) {
     80   int luma = static_cast<int>((0.3 * SkColorGetR(color)) +
     81                               (0.59 * SkColorGetG(color)) +
     82                               (0.11 * SkColorGetB(color)));
     83   return std::max(std::min(luma, 255), 0);
     84 }
     85 
     86 double RelativeLuminance(SkColor color) {
     87   return (0.2126 * ConvertSRGB(SkColorGetR(color))) +
     88          (0.7152 * ConvertSRGB(SkColorGetG(color))) +
     89          (0.0722 * ConvertSRGB(SkColorGetB(color)));
     90 }
     91 
     92 void SkColorToHSL(SkColor c, HSL* hsl) {
     93   double r = static_cast<double>(SkColorGetR(c)) / 255.0;
     94   double g = static_cast<double>(SkColorGetG(c)) / 255.0;
     95   double b = static_cast<double>(SkColorGetB(c)) / 255.0;
     96   double vmax = std::max(std::max(r, g), b);
     97   double vmin = std::min(std::min(r, g), b);
     98   double delta = vmax - vmin;
     99   hsl->l = (vmax + vmin) / 2;
    100   if (SkColorGetR(c) == SkColorGetG(c) && SkColorGetR(c) == SkColorGetB(c)) {
    101     hsl->h = hsl->s = 0;
    102   } else {
    103     double dr = (((vmax - r) / 6.0) + (delta / 2.0)) / delta;
    104     double dg = (((vmax - g) / 6.0) + (delta / 2.0)) / delta;
    105     double db = (((vmax - b) / 6.0) + (delta / 2.0)) / delta;
    106     // We need to compare for the max value because comparing vmax to r, g, or b
    107     // can sometimes result in values overflowing registers.
    108     if (r >= g && r >= b)
    109       hsl->h = db - dg;
    110     else if (g >= r && g >= b)
    111       hsl->h = (1.0 / 3.0) + dr - db;
    112     else  // (b >= r && b >= g)
    113       hsl->h = (2.0 / 3.0) + dg - dr;
    114 
    115     if (hsl->h < 0.0)
    116       ++hsl->h;
    117     else if (hsl->h > 1.0)
    118       --hsl->h;
    119 
    120     hsl->s = delta / ((hsl->l < 0.5) ? (vmax + vmin) : (2 - vmax - vmin));
    121   }
    122 }
    123 
    124 SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha) {
    125   double hue = hsl.h;
    126   double saturation = hsl.s;
    127   double lightness = hsl.l;
    128 
    129   // If there's no color, we don't care about hue and can do everything based on
    130   // brightness.
    131   if (!saturation) {
    132     uint8 light;
    133 
    134     if (lightness < 0)
    135       light = 0;
    136     else if (lightness >= 1.0)
    137       light = 255;
    138     else
    139       light = SkDoubleToFixed(lightness) >> 8;
    140 
    141     return SkColorSetARGB(alpha, light, light, light);
    142   }
    143 
    144   double temp2 = (lightness < 0.5) ?
    145       (lightness * (1.0 + saturation)) :
    146       (lightness + saturation - (lightness * saturation));
    147   double temp1 = 2.0 * lightness - temp2;
    148   return SkColorSetARGB(alpha,
    149       calcHue(temp1, temp2, hue + 1.0 / 3.0),
    150       calcHue(temp1, temp2, hue),
    151       calcHue(temp1, temp2, hue - 1.0 / 3.0));
    152 }
    153 
    154 SkColor HSLShift(SkColor color, const HSL& shift) {
    155   HSL hsl;
    156   int alpha = SkColorGetA(color);
    157   SkColorToHSL(color, &hsl);
    158 
    159   // Replace the hue with the tint's hue.
    160   if (shift.h >= 0)
    161     hsl.h = shift.h;
    162 
    163   // Change the saturation.
    164   if (shift.s >= 0) {
    165     if (shift.s <= 0.5)
    166       hsl.s *= shift.s * 2.0;
    167     else
    168       hsl.s += (1.0 - hsl.s) * ((shift.s - 0.5) * 2.0);
    169   }
    170 
    171   SkColor result = HSLToSkColor(hsl, alpha);
    172 
    173   if (shift.l < 0)
    174     return result;
    175 
    176   // Lightness shifts in the style of popular image editors aren't actually
    177   // represented in HSL - the L value does have some effect on saturation.
    178   double r = static_cast<double>(SkColorGetR(result));
    179   double g = static_cast<double>(SkColorGetG(result));
    180   double b = static_cast<double>(SkColorGetB(result));
    181   if (shift.l <= 0.5) {
    182     r *= (shift.l * 2.0);
    183     g *= (shift.l * 2.0);
    184     b *= (shift.l * 2.0);
    185   } else {
    186     r += (255.0 - r) * ((shift.l - 0.5) * 2.0);
    187     g += (255.0 - g) * ((shift.l - 0.5) * 2.0);
    188     b += (255.0 - b) * ((shift.l - 0.5) * 2.0);
    189   }
    190   return SkColorSetARGB(alpha,
    191                         static_cast<int>(r),
    192                         static_cast<int>(g),
    193                         static_cast<int>(b));
    194 }
    195 
    196 void BuildLumaHistogram(const SkBitmap& bitmap, int histogram[256]) {
    197   DCHECK_EQ(SkBitmap::kARGB_8888_Config, bitmap.config());
    198 
    199   SkAutoLockPixels bitmap_lock(bitmap);
    200 
    201   int pixel_width = bitmap.width();
    202   int pixel_height = bitmap.height();
    203   for (int y = 0; y < pixel_height; ++y) {
    204     for (int x = 0; x < pixel_width; ++x)
    205       ++histogram[GetLuminanceForColor(bitmap.getColor(x, y))];
    206   }
    207 }
    208 
    209 SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha) {
    210   if (alpha == 0)
    211     return background;
    212   if (alpha == 255)
    213     return foreground;
    214 
    215   int f_alpha = SkColorGetA(foreground);
    216   int b_alpha = SkColorGetA(background);
    217 
    218   double normalizer = (f_alpha * alpha + b_alpha * (255 - alpha)) / 255.0;
    219   if (normalizer == 0.0)
    220     return SK_ColorTRANSPARENT;
    221 
    222   double f_weight = f_alpha * alpha / normalizer;
    223   double b_weight = b_alpha * (255 - alpha) / normalizer;
    224 
    225   double r = (SkColorGetR(foreground) * f_weight +
    226               SkColorGetR(background) * b_weight) / 255.0;
    227   double g = (SkColorGetG(foreground) * f_weight +
    228               SkColorGetG(background) * b_weight) / 255.0;
    229   double b = (SkColorGetB(foreground) * f_weight +
    230               SkColorGetB(background) * b_weight) / 255.0;
    231 
    232   return SkColorSetARGB(static_cast<int>(normalizer),
    233                         static_cast<int>(r),
    234                         static_cast<int>(g),
    235                         static_cast<int>(b));
    236 }
    237 
    238 SkColor BlendTowardOppositeLuminance(SkColor color, SkAlpha alpha) {
    239   unsigned char background_luminance =
    240       color_utils::GetLuminanceForColor(color);
    241   const SkColor blend_color =
    242       (background_luminance < 128) ? SK_ColorWHITE : SK_ColorBLACK;
    243   return color_utils::AlphaBlend(blend_color, color, alpha);
    244 }
    245 
    246 SkColor GetReadableColor(SkColor foreground, SkColor background) {
    247   const SkColor foreground2 = LumaInvertColor(foreground);
    248   const double background_luminance = RelativeLuminance(background);
    249   return (ContrastRatio(RelativeLuminance(foreground), background_luminance) >=
    250           ContrastRatio(RelativeLuminance(foreground2), background_luminance)) ?
    251       foreground : foreground2;
    252 }
    253 
    254 SkColor InvertColor(SkColor color) {
    255   return SkColorSetARGB(
    256       SkColorGetA(color),
    257       255 - SkColorGetR(color),
    258       255 - SkColorGetG(color),
    259       255 - SkColorGetB(color));
    260 }
    261 
    262 SkColor GetSysSkColor(int which) {
    263 #if defined(OS_WIN)
    264   return skia::COLORREFToSkColor(GetSysColor(which));
    265 #else
    266   NOTIMPLEMENTED();
    267   return SK_ColorLTGRAY;
    268 #endif
    269 }
    270 
    271 }  // namespace color_utils
    272