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

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

Issue 289283004: Add ability to constrain dominant color selection to a HSL range. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase, fix nits Created 6 years, 7 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 | Annotate | Revision Log
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 #include "ui/gfx/color_utils.h" 5 #include "ui/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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 double temp2 = (lightness < 0.5) ? 144 double temp2 = (lightness < 0.5) ?
145 (lightness * (1.0 + saturation)) : 145 (lightness * (1.0 + saturation)) :
146 (lightness + saturation - (lightness * saturation)); 146 (lightness + saturation - (lightness * saturation));
147 double temp1 = 2.0 * lightness - temp2; 147 double temp1 = 2.0 * lightness - temp2;
148 return SkColorSetARGB(alpha, 148 return SkColorSetARGB(alpha,
149 calcHue(temp1, temp2, hue + 1.0 / 3.0), 149 calcHue(temp1, temp2, hue + 1.0 / 3.0),
150 calcHue(temp1, temp2, hue), 150 calcHue(temp1, temp2, hue),
151 calcHue(temp1, temp2, hue - 1.0 / 3.0)); 151 calcHue(temp1, temp2, hue - 1.0 / 3.0));
152 } 152 }
153 153
154 bool IsWithinHSLRange(const HSL& hsl,
155 const HSL& lower_bound,
156 const HSL& upper_bound) {
157 DCHECK(hsl.h >= 0 && hsl.h <= 1);
158 DCHECK(hsl.s >= 0 && hsl.s <= 1);
159 DCHECK(hsl.l >= 0 && hsl.l <= 1);
160 DCHECK(lower_bound.h <= 1);
161 DCHECK(lower_bound.s <= 1);
162 DCHECK(lower_bound.l <= 1);
163 DCHECK(lower_bound.h < 0 ||
164 (upper_bound.h >= 0 && upper_bound.h <= lower_bound.h + 1));
165 DCHECK(lower_bound.s < 0 || (upper_bound.s >= 0 && upper_bound.s <= 1));
166 DCHECK(lower_bound.l < 0 || (upper_bound.l >= 0 && upper_bound.l <= 1));
167
168 // If the upper hue is >1, the given hue bounds wrap around at 1.
169 bool matches_hue = upper_bound.h > 1
170 ? hsl.h >= lower_bound.h || hsl.h <= upper_bound.h - 1
171 : hsl.h >= lower_bound.h && hsl.h <= upper_bound.h;
172 return (upper_bound.h < 0 || lower_bound.h < 0 || matches_hue) &&
173 (upper_bound.s < 0 || lower_bound.s < 0 ||
174 (hsl.s >= lower_bound.s && hsl.s <= upper_bound.s)) &&
175 (upper_bound.l < 0 || lower_bound.l < 0 ||
176 (hsl.l >= lower_bound.l && hsl.l <= upper_bound.l));
177 }
178
154 SkColor HSLShift(SkColor color, const HSL& shift) { 179 SkColor HSLShift(SkColor color, const HSL& shift) {
155 HSL hsl; 180 HSL hsl;
156 int alpha = SkColorGetA(color); 181 int alpha = SkColorGetA(color);
157 SkColorToHSL(color, &hsl); 182 SkColorToHSL(color, &hsl);
158 183
159 // Replace the hue with the tint's hue. 184 // Replace the hue with the tint's hue.
160 if (shift.h >= 0) 185 if (shift.h >= 0)
161 hsl.h = shift.h; 186 hsl.h = shift.h;
162 187
163 // Change the saturation. 188 // Change the saturation.
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 SkColor GetSysSkColor(int which) { 287 SkColor GetSysSkColor(int which) {
263 #if defined(OS_WIN) 288 #if defined(OS_WIN)
264 return skia::COLORREFToSkColor(GetSysColor(which)); 289 return skia::COLORREFToSkColor(GetSysColor(which));
265 #else 290 #else
266 NOTIMPLEMENTED(); 291 NOTIMPLEMENTED();
267 return SK_ColorLTGRAY; 292 return SK_ColorLTGRAY;
268 #endif 293 #endif
269 } 294 }
270 295
271 } // namespace color_utils 296 } // namespace color_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698