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.
5 #ifndef UI_GFX_COLOR_UTILS_H_
6 #define UI_GFX_COLOR_UTILS_H_
8 #include "base/basictypes.h"
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/gfx/gfx_export.h"
14 namespace color_utils
{
16 // Represents an HSL color.
23 GFX_EXPORT
unsigned char GetLuminanceForColor(SkColor color
);
25 // Calculated according to http://www.w3.org/TR/WCAG20/#relativeluminancedef
26 GFX_EXPORT
double RelativeLuminance(SkColor color
);
28 // Note: these transformations assume sRGB as the source color space
29 GFX_EXPORT
void SkColorToHSL(SkColor c
, HSL
* hsl
);
30 GFX_EXPORT SkColor
HSLToSkColor(const HSL
& hsl
, SkAlpha alpha
);
32 // Validates an |hsl| value passed in from an untrusted source.
34 // If we were starting from scratch, we'd throw some sort of exception to the
35 // user here, but there's already code out there that acts as if -1 == 0 and
36 // 1 == 2, so just Clamp the values to [0, 1] instead.
37 GFX_EXPORT
void ClampHSL(HSL
* hsl
);
39 // Determines whether the given |hsl| falls within the given range for each
40 // component. All components of |hsl| are expected to be in the range [0, 1].
42 // If a component is negative in either |lower_bound| or |upper_bound|, that
43 // component will be ignored.
45 // For hue, the lower bound should be in the range [0, 1] and the upper bound
46 // should be in the range [(lower bound), (lower bound + 1)].
47 // For saturation and value, bounds should be specified in the range [0, 1],
48 // with the lower bound less than the upper bound.
49 GFX_EXPORT
bool IsWithinHSLRange(const HSL
& hsl
,
50 const HSL
& lower_bound
,
51 const HSL
& upper_bound
);
53 // HSL-Shift an SkColor. The shift values are in the range of 0-1, with the
54 // option to specify -1 for 'no change'. The shift values are defined as:
55 // hsl_shift[0] (hue): The absolute hue value - 0 and 1 map
56 // to 0 and 360 on the hue color wheel (red).
57 // hsl_shift[1] (saturation): A saturation shift, with the
58 // following key values:
59 // 0 = remove all color.
60 // 0.5 = leave unchanged.
61 // 1 = fully saturate the image.
62 // hsl_shift[2] (lightness): A lightness shift, with the
63 // following key values:
64 // 0 = remove all lightness (make all pixels black).
65 // 0.5 = leave unchanged.
66 // 1 = full lightness (make all pixels white).
67 GFX_EXPORT SkColor
HSLShift(SkColor color
, const HSL
& shift
);
69 // Builds a histogram based on the Y' of the Y'UV representation of
71 GFX_EXPORT
void BuildLumaHistogram(const SkBitmap
& bitmap
, int histogram
[256]);
73 // Returns a blend of the supplied colors, ranging from |background| (for
74 // |alpha| == 0) to |foreground| (for |alpha| == 255). The alpha channels of
75 // the supplied colors are also taken into account, so the returned color may
76 // be partially transparent.
77 GFX_EXPORT SkColor
AlphaBlend(SkColor foreground
, SkColor background
,
80 // Makes a dark color lighter or a light color darker by blending |color| with
81 // white or black depending on its current luminance. |alpha| controls the
82 // amount of white or black that will be alpha-blended into |color|.
83 GFX_EXPORT SkColor
BlendTowardOppositeLuminance(SkColor color
, SkAlpha alpha
);
85 // Given an opaque foreground and background color, try to return a foreground
86 // color that is "readable" over the background color by luma-inverting the
87 // foreground color and then picking whichever foreground color has higher
88 // contrast against the background color. You should not pass colors with
89 // non-255 alpha to this routine, since determining the correct behavior in such
90 // cases can be impossible.
92 // NOTE: This won't do anything but waste time if the supplied foreground color
93 // has a luma value close to the midpoint (0.5 in the HSL representation).
94 GFX_EXPORT SkColor
GetReadableColor(SkColor foreground
, SkColor background
);
97 GFX_EXPORT SkColor
InvertColor(SkColor color
);
99 // Gets a Windows system color as a SkColor
100 GFX_EXPORT SkColor
GetSysSkColor(int which
);
102 } // namespace color_utils
104 #endif // UI_GFX_COLOR_UTILS_H_