Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / color_utils.h
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 #ifndef UI_GFX_COLOR_UTILS_H_
6 #define UI_GFX_COLOR_UTILS_H_
7
8 #include "base/basictypes.h"
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/gfx/gfx_export.h"
11
12 class SkBitmap;
13
14 namespace color_utils {
15
16 // Represents an HSL color.
17 struct HSL {
18   double h;
19   double s;
20   double l;
21 };
22
23 GFX_EXPORT unsigned char GetLuminanceForColor(SkColor color);
24
25 // Calculated according to http://www.w3.org/TR/WCAG20/#relativeluminancedef
26 GFX_EXPORT double RelativeLuminance(SkColor color);
27
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);
31
32 // Determines whether the given |hsl| falls within the given range for each
33 // component. All components of |hsl| are expected to be in the range [0, 1].
34 //
35 // If a component is negative in either |lower_bound| or |upper_bound|, that
36 // component will be ignored.
37 //
38 // For hue, the lower bound should be in the range [0, 1] and the upper bound
39 // should be in the range [(lower bound), (lower bound + 1)].
40 // For saturation and value, bounds should be specified in the range [0, 1],
41 // with the lower bound less than the upper bound.
42 GFX_EXPORT bool IsWithinHSLRange(const HSL& hsl,
43                                  const HSL& lower_bound,
44                                  const HSL& upper_bound);
45
46 // Makes |hsl| valid input for HSLShift(). Sets values of hue, saturation
47 // and luminosity which are outside of the valid range [0, 1] to -1.
48 // -1 is a special value which indicates 'no change'.
49 GFX_EXPORT void MakeHSLShiftValid(HSL* hsl);
50
51 // HSL-Shift an SkColor. The shift values are in the range of 0-1, with the
52 // option to specify -1 for 'no change'. The shift values are defined as:
53 // hsl_shift[0] (hue): The absolute hue value - 0 and 1 map
54 //    to 0 and 360 on the hue color wheel (red).
55 // hsl_shift[1] (saturation): A saturation shift, with the
56 //    following key values:
57 //    0 = remove all color.
58 //    0.5 = leave unchanged.
59 //    1 = fully saturate the image.
60 // hsl_shift[2] (lightness): A lightness shift, with the
61 //    following key values:
62 //    0 = remove all lightness (make all pixels black).
63 //    0.5 = leave unchanged.
64 //    1 = full lightness (make all pixels white).
65 GFX_EXPORT SkColor HSLShift(SkColor color, const HSL& shift);
66
67 // Builds a histogram based on the Y' of the Y'UV representation of
68 // this image.
69 GFX_EXPORT void BuildLumaHistogram(const SkBitmap& bitmap, int histogram[256]);
70
71 // Returns a blend of the supplied colors, ranging from |background| (for
72 // |alpha| == 0) to |foreground| (for |alpha| == 255). The alpha channels of
73 // the supplied colors are also taken into account, so the returned color may
74 // be partially transparent.
75 GFX_EXPORT SkColor AlphaBlend(SkColor foreground, SkColor background,
76                               SkAlpha alpha);
77
78 // Makes a dark color lighter or a light color darker by blending |color| with
79 // white or black depending on its current luminance.  |alpha| controls the
80 // amount of white or black that will be alpha-blended into |color|.
81 GFX_EXPORT SkColor BlendTowardOppositeLuminance(SkColor color, SkAlpha alpha);
82
83 // Given an opaque foreground and background color, try to return a foreground
84 // color that is "readable" over the background color by luma-inverting the
85 // foreground color and then picking whichever foreground color has higher
86 // contrast against the background color.  You should not pass colors with
87 // non-255 alpha to this routine, since determining the correct behavior in such
88 // cases can be impossible.
89 //
90 // NOTE: This won't do anything but waste time if the supplied foreground color
91 // has a luma value close to the midpoint (0.5 in the HSL representation).
92 GFX_EXPORT SkColor GetReadableColor(SkColor foreground, SkColor background);
93
94 // Invert a color.
95 GFX_EXPORT SkColor InvertColor(SkColor color);
96
97 // Gets a Windows system color as a SkColor
98 GFX_EXPORT SkColor GetSysSkColor(int which);
99
100 }  // namespace color_utils
101
102 #endif  // UI_GFX_COLOR_UTILS_H_