Parse hex color string to RGB components.
- Parameters
-
| hexStr | Hex color string (e.g., "FF0000" or "#FF0000") |
| r | Output: red component (0-255) |
| g | Output: green component (0-255) |
| b | Output: blue component (0-255) |
- Returns
- true if parsing succeeded, false otherwise
Supports formats:
- "RRGGBB" (6 hex digits)
- "#RRGGBB" (# prefix optional)
Definition at line 11 of file json_helpers.cpp.hpp.
11 {
13
14
15 if (
hex.length() > 0 &&
hex[0] ==
'#') {
17 }
18
19
20 if (
hex.length() != 6) {
21 return false;
22 }
23
24
25 for (
size_t i = 0; i <
hex.length(); i++) {
27 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
28 return false;
29 }
30 }
31
32
33 auto hexDigitToInt = [](
char c) ->
u8 {
34 if (c >= '0' && c <= '9') return c - '0';
35 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
36 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
37 return 0;
38 };
39
40 r = (hexDigitToInt(
hex[0]) << 4) | hexDigitToInt(
hex[1]);
41 g = (hexDigitToInt(
hex[2]) << 4) | hexDigitToInt(
hex[3]);
42 b = (hexDigitToInt(
hex[4]) << 4) | hexDigitToInt(
hex[5]);
43
44 return true;
45}
References fl::FL_NO_EXCEPT, fl::g, and fl::hex.
Referenced by parseSegmentFields().