FastLED 3.9.15
Loading...
Searching...
No Matches

◆ parseHexColor()

bool fl::wled::parseHexColor ( const fl::string & hexStr,
u8 & r,
u8 & g,
u8 & b )

Parse hex color string to RGB components.

Parameters
hexStrHex color string (e.g., "FF0000" or "#FF0000")
rOutput: red component (0-255)
gOutput: green component (0-255)
bOutput: blue component (0-255)
Returns
true if parsing succeeded, false otherwise

Supports formats:

  • "RRGGBB" (6 hex digits)
  • "#RRGGBB" (# prefix optional)

Definition at line 10 of file json_helpers.cpp.hpp.

10 {
11 fl::string hex = hexStr;
12
13 // Strip leading '#' if present
14 if (hex.length() > 0 && hex[0] == '#') {
15 hex = hex.substr(1);
16 }
17
18 // Validate length (must be exactly 6 characters)
19 if (hex.length() != 6) {
20 return false;
21 }
22
23 // Validate hex characters
24 for (size_t i = 0; i < hex.length(); i++) {
25 char c = hex[i];
26 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
27 return false;
28 }
29 }
30
31 // Parse hex digits
32 auto hexDigitToInt = [](char c) -> u8 {
33 if (c >= '0' && c <= '9') return c - '0';
34 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
35 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
36 return 0;
37 };
38
39 r = (hexDigitToInt(hex[0]) << 4) | hexDigitToInt(hex[1]);
40 g = (hexDigitToInt(hex[2]) << 4) | hexDigitToInt(hex[3]);
41 b = (hexDigitToInt(hex[4]) << 4) | hexDigitToInt(hex[5]);
42
43 return true;
44}
unsigned char u8
Definition stdint.h:131
const hex_t hex
Definition ios.cpp.hpp:6

References fl::hex.

Referenced by parseSegmentFields().

+ Here is the caller graph for this function: