Skip to content

Commit 5ac3757

Browse files
committed
parse: properties: utils: Add support for alpha in hex color
This adds support for 4 and 8 digit hex values, which specify alpha. See css-color-4 section 5.2.
1 parent 0d6398f commit 5ac3757

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/parse/properties/utils.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ css_error css__parse_colour_specifier(css_language *c,
377377
consumeWhitespace(vector, ctx);
378378

379379
/* IDENT(<colour name>) |
380-
* HASH(rgb | rrggbb) |
380+
* HASH(rgb | rgba | rrggbb | rrggbbaa) |
381381
* FUNCTION(rgb) [ [ NUMBER | PERCENTAGE ] ',' ] {3} ')'
382382
* FUNCTION(rgba) [ [ NUMBER | PERCENTAGE ] ',' ] {4} ')'
383383
* FUNCTION(hsl) ANGLE ',' PERCENTAGE ',' PERCENTAGE ')'
@@ -894,7 +894,7 @@ css_error css__parse_named_colour(css_language *c, lwc_string *data,
894894
}
895895

896896
/**
897-
* Parse a hash colour (#rgb or #rrggbb)
897+
* Parse a hash colour (#rgb, #rgba, #rrggbb or #rrggbbaa)
898898
*
899899
* \param data Pointer to colour string
900900
* \param result Pointer to location to receive result (AARRGGBB)
@@ -907,26 +907,49 @@ css_error css__parse_hash_colour(lwc_string *data, uint32_t *result)
907907
size_t len = lwc_string_length(data);
908908
const char *input = lwc_string_data(data);
909909

910-
if (len == 3 && isHex(input[0]) && isHex(input[1]) &&
911-
isHex(input[2])) {
910+
switch (len) {
911+
case 4:
912+
if (!isHex(input[3])) {
913+
return CSS_INVALID;
914+
}
915+
a = charToHex(input[3]);
916+
a |= (a << 4);
917+
/* Fall through */
918+
case 3:
919+
if (!isHex(input[0]) || !isHex(input[1]) || !isHex(input[2])) {
920+
return CSS_INVALID;
921+
}
912922
r = charToHex(input[0]);
913923
g = charToHex(input[1]);
914924
b = charToHex(input[2]);
915925

916926
r |= (r << 4);
917927
g |= (g << 4);
918928
b |= (b << 4);
919-
} else if (len == 6 && isHex(input[0]) && isHex(input[1]) &&
920-
isHex(input[2]) && isHex(input[3]) &&
921-
isHex(input[4]) && isHex(input[5])) {
929+
break;
930+
case 8:
931+
if (!isHex(input[6]) || !isHex(input[7])) {
932+
return CSS_INVALID;
933+
}
934+
a = (charToHex(input[6]) << 4);
935+
a |= charToHex(input[7]);
936+
/* Fall through */
937+
case 6:
938+
if (!isHex(input[0]) || !isHex(input[1]) ||
939+
!isHex(input[2]) || !isHex(input[3]) ||
940+
!isHex(input[4]) || !isHex(input[5])) {
941+
return CSS_INVALID;
942+
}
922943
r = (charToHex(input[0]) << 4);
923944
r |= charToHex(input[1]);
924945
g = (charToHex(input[2]) << 4);
925946
g |= charToHex(input[3]);
926947
b = (charToHex(input[4]) << 4);
927948
b |= charToHex(input[5]);
928-
} else
949+
break;
950+
default:
929951
return CSS_INVALID;
952+
}
930953

931954
*result = ((unsigned)a << 24) | (r << 16) | (g << 8) | b;
932955

0 commit comments

Comments
 (0)