1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/image_util.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include "base/check.h"
#include "base/cxx17_backports.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "third_party/re2/src/re2/re2.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/utils/SkParse.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_utils.h"
namespace extensions {
namespace image_util {
bool ParseCssColorString(const std::string& color_string, SkColor* result) {
if (color_string.empty())
return false;
if (color_string[0] == '#')
return ParseHexColorString(color_string, result);
if (base::StartsWith(color_string, "hsl", base::CompareCase::SENSITIVE))
return ParseHslColorString(color_string, result);
if (base::StartsWith(color_string, "rgb", base::CompareCase::SENSITIVE)) {
return ParseRgbColorString(color_string, result);
}
if (SkParse::FindNamedColor(color_string.c_str(), color_string.size(),
result) != nullptr) {
return true;
}
return false;
}
bool ParseHexColorString(const std::string& color_string, SkColor* result) {
std::string formatted_color;
// Save a memory allocation -- we never need more than 8 chars.
formatted_color.reserve(8);
// Check the string for incorrect formatting.
if (color_string.empty() || color_string[0] != '#')
return false;
// Convert the string from #FFF format to #FFFFFF format.
if (color_string.length() == 4 || color_string.length() == 5) {
for (size_t i = 1; i < color_string.length(); ++i) {
formatted_color += color_string[i];
formatted_color += color_string[i];
}
} else if (color_string.length() == 7) {
formatted_color.assign(color_string, 1, 6);
} else if (color_string.length() == 9) {
formatted_color.assign(color_string, 1, 8);
} else {
return false;
}
// Add an alpha if one was not set.
if (formatted_color.length() == 6) {
formatted_color += "FF";
}
// Convert the hex string to an integer.
std::array<uint8_t, 4> color_bytes;
if (!base::HexStringToSpan(formatted_color, color_bytes))
return false;
*result = SkColorSetARGB(color_bytes[3], color_bytes[0], color_bytes[1],
color_bytes[2]);
return true;
}
std::string GenerateHexColorString(SkColor color) {
return base::StringPrintf("#%02X%02X%02X", SkColorGetR(color),
SkColorGetG(color), SkColorGetB(color));
}
bool ParseRgbColorString(const std::string& color_string, SkColor* result) {
// https://www.w3.org/wiki/CSS/Properties/color/RGB#The_format_of_the_RGB_Value
// The CSS3 specification defines the format of a RGB color as
// rgb(<number>, <number>, <number>) or
// rgb(<percent>, <percent>, <percent>) or
// and with alpha, the format is
// rgb(<number>, <number>, <number>, <alphavalue>) or
// rgba(<percent>, <percent>, <percent>, <alphavalue>)
// Whitespace is arbitrary.
// e.g.: rgb(120, 100, 50), rgba(120, 100, 50, 0.5);
int r = 0;
int g = 0;
int b = 0;
// 'rgb()' has '1' alpha value implicitly.
double alpha = 1.0;
// Percentage rgb values are not supported.
if (color_string.find('%') != std::string::npos) {
NOTIMPLEMENTED();
return false;
}
if (!re2::RE2::FullMatch(color_string,
"rgb\\(([\\d]+),\\s*([\\d]+),\\s*([\\d]+)\\)", &r,
&g, &b) &&
!re2::RE2::FullMatch(
color_string,
"rgba\\(([\\d]+),\\s*([\\d]+),\\s*([\\d]+),\\s*([\\d.]+)\\)", &r, &g,
&b, &alpha)) {
return false;
}
if (alpha < 0 || alpha > 1.0 || r < 0 || r > 255 || g < 0 || g > 255 ||
b < 0 || b > 255) {
return false;
}
SkAlpha sk_alpha = alpha * 255;
*result = SkColorSetARGB(sk_alpha, r, g, b);
return true;
}
bool ParseHslColorString(const std::string& color_string, SkColor* result) {
// http://www.w3.org/wiki/CSS/Properties/color/HSL#The_format_of_the_HSL_Value
// The CSS3 specification defines the format of a HSL color as
// hsl(<number>, <percent>, <percent>)
// and with alpha, the format is
// hsla(<number>, <percent>, <percent>, <number>)
// e.g.: hsl(120, 100%, 50%), hsla(120, 100%, 50%, 0.5);
double hue = 0.0;
double saturation = 0.0;
double lightness = 0.0;
// 'hsl()' has '1' alpha value implicitly.
double alpha = 1.0;
if (!re2::RE2::FullMatch(color_string,
"hsl\\((-?[\\d.]+),\\s*([\\d.]+)%,\\s*([\\d.]+)%\\)",
&hue, &saturation, &lightness) &&
!re2::RE2::FullMatch(
color_string,
"hsla\\((-?[\\d.]+),\\s*([\\d.]+)%,\\s*([\\d.]+)%,\\s*([\\d.]+)\\)",
&hue, &saturation, &lightness, &alpha)) {
return false;
}
color_utils::HSL hsl;
// Normalize the value between 0.0 and 1.0.
hsl.h = (((static_cast<int>(hue) % 360) + 360) % 360) / 360.0;
hsl.s = base::clamp(saturation, 0.0, 100.0) / 100.0;
hsl.l = base::clamp(lightness, 0.0, 100.0) / 100.0;
SkAlpha sk_alpha = base::clamp(alpha, 0.0, 1.0) * 255;
*result = color_utils::HSLToSkColor(hsl, sk_alpha);
return true;
}
bool IsIconSufficientlyVisible(const SkBitmap& bitmap) {
// TODO(crbug.com/805600): Currently, we only consider if there are enough
// visible pixels that it won't be difficult for the user to see. Future
// revisions will consider the background color of the display context.
// If the alpha value of any pixel is greater than kAlphaThreshold, the
// pixmap is not transparent. These values will likely be adjusted, based
// on stats and research into visibility thresholds.
constexpr unsigned int kAlphaThreshold = 10;
// The minimum "percent" of pixels that must be visible for the icon to be
// considered OK.
constexpr double kMinPercentVisiblePixels = 0.03;
const int total_pixels = bitmap.height() * bitmap.width();
// Pre-calculate the minimum number of visible pixels so we can exit early.
// Since we expect most icons to be visible, this will perform better for
// the common case.
const int minimum_visible_pixels =
std::max(kMinPercentVisiblePixels * total_pixels, 1.0);
int visible_pixels = 0;
for (int y = 0; y < bitmap.height(); ++y) {
for (int x = 0; x < bitmap.width(); ++x) {
if (SkColorGetA(bitmap.getColor(x, y)) >= kAlphaThreshold) {
if (++visible_pixels == minimum_visible_pixels) {
return true;
}
}
}
}
return false;
}
bool IsIconAtPathSufficientlyVisible(const base::FilePath& path) {
SkBitmap icon;
if (!LoadPngFromFile(path, &icon)) {
return false;
}
return IsIconSufficientlyVisible(icon);
}
const SkColor kDefaultToolbarColor = SK_ColorWHITE;
struct ScopedUmaMicrosecondHistogramTimer {
ScopedUmaMicrosecondHistogramTimer() : timer() {}
~ScopedUmaMicrosecondHistogramTimer() {
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"Extensions.IsRenderedIconSufficientlyVisibleTime", timer.Elapsed(),
base::TimeDelta::FromMicroseconds(1), base::TimeDelta::FromSeconds(5),
50);
}
const base::ElapsedTimer timer;
};
bool IsRenderedIconSufficientlyVisible(const SkBitmap& icon,
SkColor background_color) {
const ScopedUmaMicrosecondHistogramTimer timer;
// If any of a pixel's RGB values is greater than this number, the pixel is
// considered visible.
constexpr unsigned int kThreshold = 7;
// The minimum "percent" of pixels that must be visible for the icon to be
// considered OK.
constexpr double kMinPercentVisiblePixels = 0.03;
const int total_pixels = icon.height() * icon.width();
// Pre-calculate the minimum number of visible pixels so we can exit early.
// Since we expect most icons to be visible, this will perform better for
// the common case.
const int minimum_visible_pixels =
std::max(kMinPercentVisiblePixels * total_pixels, 1.0);
// Draw the icon onto a canvas, then draw the background color onto the
// resulting bitmap, using SkBlendMode::kDifference. Then, check the RGB
// values against the threshold. Any pixel with a value greater than the
// threshold is considered visible.
SkBitmap bitmap;
RenderIconForVisibilityAnalysis(icon, background_color, &bitmap);
int visible_pixels = 0;
for (int x = 0; x < icon.width(); ++x) {
for (int y = 0; y < icon.height(); ++y) {
SkColor pixel = bitmap.getColor(x, y);
if (SkColorGetR(pixel) > kThreshold || SkColorGetB(pixel) > kThreshold ||
SkColorGetG(pixel) > kThreshold) {
if (++visible_pixels == minimum_visible_pixels) {
return true;
}
}
}
}
return false;
}
void RenderIconForVisibilityAnalysis(const SkBitmap& icon,
SkColor background_color,
SkBitmap* rendered_icon) {
DCHECK(rendered_icon);
DCHECK(rendered_icon->empty());
rendered_icon->allocN32Pixels(icon.width(), icon.height());
rendered_icon->eraseColor(background_color);
SkCanvas offscreen(*rendered_icon, SkSurfaceProps{});
offscreen.drawImage(SkImage::MakeFromBitmap(icon), 0, 0);
offscreen.drawColor(background_color, SkBlendMode::kDifference);
}
bool IsRenderedIconAtPathSufficientlyVisible(const base::FilePath& path,
SkColor background_color) {
SkBitmap icon;
if (!LoadPngFromFile(path, &icon)) {
return false;
}
return IsRenderedIconSufficientlyVisible(icon, background_color);
}
bool LoadPngFromFile(const base::FilePath& path, SkBitmap* dst) {
std::string png_bytes;
if (!base::ReadFileToString(path, &png_bytes)) {
return false;
}
return gfx::PNGCodec::Decode(
reinterpret_cast<const unsigned char*>(png_bytes.data()),
png_bytes.length(), dst);
}
} // namespace image_util
} // namespace extensions
|