decodeHexColor

decode color string supported formats: #RGB, #ARGB, #RRGGBB, #AARRGGBB, rgb(r,g,b), rgba(r,g,b,a), rgba(r,g,b,a%)

@safe
uint
decodeHexColor
(
string s
,
uint defValue = 0
)

Examples

assert(decodeHexColor("") == 0);
assert(decodeHexColor("@null") == COLOR_TRANSPARENT);
assert(decodeHexColor("trAnsParent") == COLOR_TRANSPARENT);
assert(decodeHexColor("grAy") == 0x808080);
assert(decodeHexColor("#8B008B") == 0x8B008B);
assert(decodeHexColor("#fFf") == 0xfff);
assert(decodeHexColor("#f0F0") == 0xf0f0);
assert(decodeHexColor("#80ff0000") == 0x80ff0000);
assert(decodeHexColor("rgba(255, 0, 0,.5 )") == 0x80ff0000);
assert(decodeHexColor("rgba(255,0, 0, 50%)") == 0x80ff0000);
assert(decodeHexColor("rgba(255,0, 0, 100%)") == 0xff0000);
assert(decodeHexColor("rgba(255,0, 0, 0%)") == 0x00000000);
assert(decodeHexColor("rgb(255,255, 255)") == 0xffffff);
assert(decodeHexColor("rgba(255,0, 0, 150%)") == 0xff0000); // invalid input
assert(decodeHexColor("rgba(255,0, 0, -34%)") == 0x00000000); // invalid input
assert(decodeHexColor("rgb(321,321,321)") == 0xffffff); // invalid input
assert(decodeHexColor("not_valid_color_name") == 0x00000000); // invalid input, return def value
assert(decodeHexColor("#80ff00000") == 0x000000000); // invalid input, return def value
assert(decodeHexColor("#f0") == 0x00000000); // invalid input, return def value
assert(decodeHexColor("rgba(255,255, 255, 10)") == 0xffffff); // invalid input
assert(decodeHexColor("rgba(444,0, 0, -5)") == 0x00000000); // invalid input

Meta