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

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

Issue 1761183002: color_utils cleanup: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
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 #ifndef UI_GFX_COLOR_UTILS_H_ 5 #ifndef UI_GFX_COLOR_UTILS_H_
6 #define UI_GFX_COLOR_UTILS_H_ 6 #define UI_GFX_COLOR_UTILS_H_
7 7
8 #include "third_party/skia/include/core/SkColor.h" 8 #include "third_party/skia/include/core/SkColor.h"
9 #include "ui/gfx/gfx_export.h" 9 #include "ui/gfx/gfx_export.h"
10 10
11 class SkBitmap; 11 class SkBitmap;
12 12
13 namespace color_utils { 13 namespace color_utils {
14 14
15 // Represents an HSL color. 15 // Represents an HSL color.
16 struct HSL { 16 struct HSL {
17 double h; 17 double h;
18 double s; 18 double s;
19 double l; 19 double l;
20 }; 20 };
21 21
22 // The minimum contrast between text and background that is still readable. 22 // The minimum contrast between text and background that is still readable.
23 // This value is taken from w3c accessibility guidelines. 23 // This value is taken from w3c accessibility guidelines.
24 const double kMinimumReadableContrastRatio = 4.5f; 24 const double kMinimumReadableContrastRatio = 4.5f;
25 25
26 // Determines the contrast ratio of two colors. 26 // Determines the contrast ratio of two colors or two relative luminance values
27 GFX_EXPORT double GetContrastRatio(SkColor color_a, SkColor color_b); 27 // (as computed by RelativeLuminance()), calculated according to
28 // http://www.w3.org/TR/WCAG20/#contrast-ratiodef .
29 GFX_EXPORT double ContrastRatio(SkColor color_a, SkColor color_b);
danakj 2016/03/04 01:54:49 GetContrastRatio is preferred (verbs not nouns for
Peter Kasting 2016/03/04 02:10:52 That may be true in Blink, but in Chromium Get...(
danakj 2016/03/04 19:05:55 It's the other way. The WebKit style guide says to
Peter Kasting 2016/03/04 20:28:57 I can't find any such text in the Google style gui
danakj 2016/03/04 21:10:00 I'm probably being over sensitive to this cuz all
30 GFX_EXPORT double ContrastRatio(double luminance_a, double luminance_b);
28 31
29 GFX_EXPORT unsigned char GetLuminanceForColor(SkColor color); 32 // The relative luminance of |color|, that is, the weighted sum of the
33 // linearized RGB components, normalized to 0..1, per BT.709. See
34 // http://www.w3.org/TR/WCAG20/#relativeluminancedef .
35 GFX_EXPORT double RelativeLuminance(SkColor color);
30 36
31 // Calculated according to http://www.w3.org/TR/WCAG20/#relativeluminancedef 37 // The luma of |color|, that is, the weighted sum of the gamma-compressed R'G'B'
32 GFX_EXPORT double RelativeLuminance(SkColor color); 38 // components, per BT.601, a.k.a. the Y' in Y'UV. See
39 // https://en.wikipedia.org/wiki/Luma_(video).
40 GFX_EXPORT uint8_t Luma(SkColor color);
33 41
34 // Note: these transformations assume sRGB as the source color space 42 // Note: these transformations assume sRGB as the source color space
35 GFX_EXPORT void SkColorToHSL(SkColor c, HSL* hsl); 43 GFX_EXPORT void SkColorToHSL(SkColor c, HSL* hsl);
36 GFX_EXPORT SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha); 44 GFX_EXPORT SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha);
37 45
38 // Determines whether the given |hsl| falls within the given range for each 46 // Determines whether the given |hsl| falls within the given range for each
39 // component. All components of |hsl| are expected to be in the range [0, 1]. 47 // component. All components of |hsl| are expected to be in the range [0, 1].
40 // 48 //
41 // If a component is negative in either |lower_bound| or |upper_bound|, that 49 // If a component is negative in either |lower_bound| or |upper_bound|, that
42 // component will be ignored. 50 // component will be ignored.
43 // 51 //
44 // For hue, the lower bound should be in the range [0, 1] and the upper bound 52 // For hue, the lower bound should be in the range [0, 1] and the upper bound
45 // should be in the range [(lower bound), (lower bound + 1)]. 53 // should be in the range [(lower bound), (lower bound + 1)].
46 // For saturation and value, bounds should be specified in the range [0, 1], 54 // For saturation and value, bounds should be specified in the range [0, 1],
47 // with the lower bound less than the upper bound. 55 // with the lower bound less than the upper bound.
48 GFX_EXPORT bool IsWithinHSLRange(const HSL& hsl, 56 GFX_EXPORT bool IsWithinHSLRange(const HSL& hsl,
49 const HSL& lower_bound, 57 const HSL& lower_bound,
50 const HSL& upper_bound); 58 const HSL& upper_bound);
51 59
52 // Makes |hsl| valid input for HSLShift(). Sets values of hue, saturation 60 // Makes |hsl| valid input for HSLShift(). Sets values of hue, saturation
53 // and luminosity which are outside of the valid range [0, 1] to -1. 61 // and lightness which are outside of the valid range [0, 1] to -1. -1 is a
54 // -1 is a special value which indicates 'no change'. 62 // special value which indicates 'no change'.
55 GFX_EXPORT void MakeHSLShiftValid(HSL* hsl); 63 GFX_EXPORT void MakeHSLShiftValid(HSL* hsl);
56 64
57 // HSL-Shift an SkColor. The shift values are in the range of 0-1, with the 65 // HSL-Shift an SkColor. The shift values are in the range of 0-1, with the
58 // option to specify -1 for 'no change'. The shift values are defined as: 66 // option to specify -1 for 'no change'. The shift values are defined as:
59 // hsl_shift[0] (hue): The absolute hue value - 0 and 1 map 67 // hsl_shift[0] (hue): The absolute hue value - 0 and 1 map
60 // to 0 and 360 on the hue color wheel (red). 68 // to 0 and 360 on the hue color wheel (red).
61 // hsl_shift[1] (saturation): A saturation shift, with the 69 // hsl_shift[1] (saturation): A saturation shift, with the
62 // following key values: 70 // following key values:
63 // 0 = remove all color. 71 // 0 = remove all color.
64 // 0.5 = leave unchanged. 72 // 0.5 = leave unchanged.
65 // 1 = fully saturate the image. 73 // 1 = fully saturate the image.
66 // hsl_shift[2] (lightness): A lightness shift, with the 74 // hsl_shift[2] (lightness): A lightness shift, with the
67 // following key values: 75 // following key values:
68 // 0 = remove all lightness (make all pixels black). 76 // 0 = remove all lightness (make all pixels black).
69 // 0.5 = leave unchanged. 77 // 0.5 = leave unchanged.
70 // 1 = full lightness (make all pixels white). 78 // 1 = full lightness (make all pixels white).
71 GFX_EXPORT SkColor HSLShift(SkColor color, const HSL& shift); 79 GFX_EXPORT SkColor HSLShift(SkColor color, const HSL& shift);
72 80
73 // Builds a histogram based on the Y' of the Y'UV representation of 81 // Builds a histogram based on the Y' of the Y'UV representation of this image.
74 // this image.
75 GFX_EXPORT void BuildLumaHistogram(const SkBitmap& bitmap, int histogram[256]); 82 GFX_EXPORT void BuildLumaHistogram(const SkBitmap& bitmap, int histogram[256]);
76 83
77 // Calculates how "boring" an image is. The boring score is the 84 // Calculates how "boring" an image is. The boring score is the
78 // 0,1 ranged percentage of pixels that are the most common 85 // 0,1 ranged percentage of pixels that are the most common
79 // luma. Higher boring scores indicate that a higher percentage of a 86 // luma. Higher boring scores indicate that a higher percentage of a
80 // bitmap are all the same brightness. 87 // bitmap are all the same brightness.
81 GFX_EXPORT double CalculateBoringScore(const SkBitmap& bitmap); 88 GFX_EXPORT double CalculateBoringScore(const SkBitmap& bitmap);
82 89
83 // Returns a blend of the supplied colors, ranging from |background| (for 90 // Returns a blend of the supplied colors, ranging from |background| (for
84 // |alpha| == 0) to |foreground| (for |alpha| == 255). The alpha channels of 91 // |alpha| == 0) to |foreground| (for |alpha| == 255). The alpha channels of
85 // the supplied colors are also taken into account, so the returned color may 92 // the supplied colors are also taken into account, so the returned color may
86 // be partially transparent. 93 // be partially transparent.
87 GFX_EXPORT SkColor AlphaBlend(SkColor foreground, SkColor background, 94 GFX_EXPORT SkColor AlphaBlend(SkColor foreground, SkColor background,
88 SkAlpha alpha); 95 SkAlpha alpha);
89 96
90 // Returns true if the luminance of |color| is closer to black than white. 97 // Returns true if the luma of |color| is closer to black than white.
91 GFX_EXPORT bool IsDark(SkColor color); 98 GFX_EXPORT bool IsDark(SkColor color);
92 99
93 // Makes a dark color lighter or a light color darker by blending |color| with 100 // Makes a dark color lighter or a light color darker by blending |color| with
94 // white or black depending on its current luminance. |alpha| controls the 101 // white or black depending on its current luma. |alpha| controls the amount of
95 // amount of white or black that will be alpha-blended into |color|. 102 // white or black that will be alpha-blended into |color|.
96 GFX_EXPORT SkColor BlendTowardOppositeLuminance(SkColor color, SkAlpha alpha); 103 GFX_EXPORT SkColor BlendTowardOppositeLuma(SkColor color, SkAlpha alpha);
97 104
98 // Given an opaque foreground and background color, try to return a foreground 105 // Given a foreground and background color, try to return a foreground color
99 // color that is "readable" over the background color by luma-inverting the 106 // that is "readable" over the background color by luma-inverting the foreground
100 // foreground color and then picking whichever foreground color has higher 107 // color and then using PickContrastingColor() to pick the one with greater
101 // contrast against the background color. You should not pass colors with 108 // contrast. During this process, alpha values will be ignored; the returned
102 // non-255 alpha to this routine, since determining the correct behavior in such 109 // color will have the same alpha as |foreground|.
103 // cases can be impossible.
104 // 110 //
105 // NOTE: This won't do anything but waste time if the supplied foreground color 111 // NOTE: This won't do anything but waste time if the supplied foreground color
106 // has a luma value close to the midpoint (0.5 in the HSL representation). 112 // has a luma value close to the midpoint (0.5 in the HSL representation).
107 GFX_EXPORT SkColor GetReadableColor(SkColor foreground, SkColor background); 113 GFX_EXPORT SkColor GetReadableColor(SkColor foreground, SkColor background);
108 114
115 // Returns whichever of |foreground1| or |foreground2| has higher contrast with
116 // |background|.
117 GFX_EXPORT SkColor PickContrastingColor(SkColor foreground1,
118 SkColor foreground2,
119 SkColor background);
120
109 // Invert a color. 121 // Invert a color.
110 GFX_EXPORT SkColor InvertColor(SkColor color); 122 GFX_EXPORT SkColor InvertColor(SkColor color);
111 123
112 // Gets a Windows system color as a SkColor 124 // Gets a Windows system color as a SkColor
113 GFX_EXPORT SkColor GetSysSkColor(int which); 125 GFX_EXPORT SkColor GetSysSkColor(int which);
114 126
115 // Returns true only if Chrome should use an inverted color scheme - which is 127 // Returns true only if Chrome should use an inverted color scheme - which is
116 // only true if the system has high-contrast mode enabled and and is using a 128 // only true if the system has high-contrast mode enabled and and is using a
117 // light-on-dark color scheme. 129 // light-on-dark color scheme.
118 GFX_EXPORT bool IsInvertedColorScheme(); 130 GFX_EXPORT bool IsInvertedColorScheme();
119 131
120 // Derives a color for icons on a UI surface based on the text color on the same 132 // Derives a color for icons on a UI surface based on the text color on the same
121 // surface. 133 // surface.
122 GFX_EXPORT SkColor DeriveDefaultIconColor(SkColor text_color); 134 GFX_EXPORT SkColor DeriveDefaultIconColor(SkColor text_color);
123 135
124 } // namespace color_utils 136 } // namespace color_utils
125 137
126 #endif // UI_GFX_COLOR_UTILS_H_ 138 #endif // UI_GFX_COLOR_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698