Skip to content

Commit f0f6c4b

Browse files
committed
actually parse hex argb not just rgb
1 parent b6f8608 commit f0f6c4b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

csscolorparser.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,18 @@ std::vector<std::string> split(const std::string& s, char delim) {
259259
}
260260

261261
Color CSSColorParser::parse(const std::string& css_str) {
262+
int length = css_str.length();
263+
264+
if (length == 0) {
265+
return {};
266+
}
262267

263268
// #abc and #abc123 syntax.
264-
if (css_str.length() && css_str.front() == '#') {
265-
if (css_str.length() == 4) {
266-
int64_t iv = parseInt(css_str.c_str()+1, 16); // TODO(deanm): Stricter parsing.
269+
if (css_str.front() == '#') {
270+
const char* str = css_str.c_str()+1;
271+
272+
if (length == 4) { // rgb
273+
int64_t iv = parseInt(str, 16); // TODO(deanm): Stricter parsing.
267274
if (!(iv >= 0 && iv <= 0xfff)) {
268275
return {};
269276
} else {
@@ -274,8 +281,8 @@ Color CSSColorParser::parse(const std::string& css_str) {
274281
1
275282
};
276283
}
277-
} else if (css_str.length() == 7) {
278-
int64_t iv = parseInt(css_str.c_str()+1, 16); // TODO(deanm): Stricter parsing.
284+
} else if (length == 7) { // rrggbb
285+
int64_t iv = parseInt(str, 16);
279286
if (!(iv >= 0 && iv <= 0xffffff)) {
280287
return {}; // Covers NaN.
281288
} else {
@@ -286,8 +293,31 @@ Color CSSColorParser::parse(const std::string& css_str) {
286293
1
287294
};
288295
}
296+
} else if (length == 5) { // argb
297+
int64_t iv = parseInt(str, 16);
298+
if (!(iv >= 0 && iv <= 0xffff)) {
299+
return {};
300+
} else {
301+
return {
302+
static_cast<uint8_t>(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)),
303+
static_cast<uint8_t>((iv & 0xf0) | ((iv & 0xf0) >> 4)),
304+
static_cast<uint8_t>((iv & 0xf) | ((iv & 0xf) << 4)),
305+
static_cast<uint8_t>((iv & 0xf000) >> 12) / 255.0f,
306+
};
307+
}
308+
} else if (length == 9) { // aarrggbb
309+
int64_t iv = parseInt(str, 16);
310+
if (!(iv >= 0 && iv <= 0xffffffff)) {
311+
return {}; // Covers NaN.
312+
} else {
313+
return {
314+
static_cast<uint8_t>((iv & 0xff0000) >> 16),
315+
static_cast<uint8_t>((iv & 0xff00) >> 8),
316+
static_cast<uint8_t>(iv & 0xff),
317+
static_cast<uint8_t>((iv & 0xff000000) >> 24) / 255.0f,
318+
};
319+
}
289320
}
290-
291321
return {};
292322
}
293323

0 commit comments

Comments
 (0)