OLD | NEW |
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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 r += (255.0 - r) * ((shift.l - 0.5) * 2.0); | 183 r += (255.0 - r) * ((shift.l - 0.5) * 2.0); |
184 g += (255.0 - g) * ((shift.l - 0.5) * 2.0); | 184 g += (255.0 - g) * ((shift.l - 0.5) * 2.0); |
185 b += (255.0 - b) * ((shift.l - 0.5) * 2.0); | 185 b += (255.0 - b) * ((shift.l - 0.5) * 2.0); |
186 } | 186 } |
187 return SkColorSetARGB(alpha, | 187 return SkColorSetARGB(alpha, |
188 static_cast<int>(r), | 188 static_cast<int>(r), |
189 static_cast<int>(g), | 189 static_cast<int>(g), |
190 static_cast<int>(b)); | 190 static_cast<int>(b)); |
191 } | 191 } |
192 | 192 |
193 bool IsColorCloseToTransparent(SkAlpha alpha) { | |
194 const int kCloseToBoundary = 64; | |
195 return alpha < kCloseToBoundary; | |
196 } | |
197 | |
198 bool IsColorCloseToGrey(int r, int g, int b) { | |
199 const int kAverageBoundary = 15; | |
200 int average = (r + g + b) / 3; | |
201 return (abs(r - average) < kAverageBoundary) && | |
202 (abs(g - average) < kAverageBoundary) && | |
203 (abs(b - average) < kAverageBoundary); | |
204 } | |
205 | |
206 SkColor GetAverageColorOfFavicon(SkBitmap* favicon, SkAlpha alpha) { | |
207 int r = 0, g = 0, b = 0; | |
208 | |
209 SkAutoLockPixels favicon_lock(*favicon); | |
210 SkColor* pixels = static_cast<SkColor*>(favicon->getPixels()); | |
211 if (!pixels) | |
212 return SkColorSetARGB(alpha, 0, 0, 0); | |
213 | |
214 // Assume ARGB_8888 format. | |
215 DCHECK(favicon->config() == SkBitmap::kARGB_8888_Config); | |
216 SkColor* current_color = pixels; | |
217 | |
218 DCHECK(favicon->width() <= 16 && favicon->height() <= 16); | |
219 | |
220 int pixel_count = favicon->width() * favicon->height(); | |
221 int color_count = 0; | |
222 for (int i = 0; i < pixel_count; ++i, ++current_color) { | |
223 // Disregard this color if it is close to black, close to white, or close | |
224 // to transparent since any of those pixels do not contribute much to the | |
225 // color makeup of this icon. | |
226 int cr = SkColorGetR(*current_color); | |
227 int cg = SkColorGetG(*current_color); | |
228 int cb = SkColorGetB(*current_color); | |
229 | |
230 if (IsColorCloseToTransparent(SkColorGetA(*current_color)) || | |
231 IsColorCloseToGrey(cr, cg, cb)) | |
232 continue; | |
233 | |
234 r += cr; | |
235 g += cg; | |
236 b += cb; | |
237 ++color_count; | |
238 } | |
239 | |
240 return color_count ? | |
241 SkColorSetARGB(alpha, r / color_count, g / color_count, b / color_count) : | |
242 SkColorSetARGB(alpha, 0, 0, 0); | |
243 } | |
244 | |
245 void BuildLumaHistogram(const SkBitmap& bitmap, int histogram[256]) { | 193 void BuildLumaHistogram(const SkBitmap& bitmap, int histogram[256]) { |
246 SkAutoLockPixels bitmap_lock(bitmap); | 194 SkAutoLockPixels bitmap_lock(bitmap); |
247 if (!bitmap.getPixels()) | 195 if (!bitmap.getPixels()) |
248 return; | 196 return; |
249 | 197 |
250 // Assume ARGB_8888 format. | 198 // Assume ARGB_8888 format. |
251 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); | 199 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); |
252 | 200 |
253 int pixel_width = bitmap.width(); | 201 int pixel_width = bitmap.width(); |
254 int pixel_height = bitmap.height(); | 202 int pixel_height = bitmap.height(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 SkColor GetSysSkColor(int which) { | 255 SkColor GetSysSkColor(int which) { |
308 #if defined(OS_WIN) | 256 #if defined(OS_WIN) |
309 return skia::COLORREFToSkColor(GetSysColor(which)); | 257 return skia::COLORREFToSkColor(GetSysColor(which)); |
310 #else | 258 #else |
311 NOTIMPLEMENTED(); | 259 NOTIMPLEMENTED(); |
312 return SK_ColorLTGRAY; | 260 return SK_ColorLTGRAY; |
313 #endif | 261 #endif |
314 } | 262 } |
315 | 263 |
316 } // namespace color_utils | 264 } // namespace color_utils |
OLD | NEW |