FastLED 3.10.8
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 11 of file json_helpers.cpp.hpp.

11 {
12 fl::string hex = hexStr;
13
14 // Strip leading '#' if present
15 if (hex.length() > 0 && hex[0] == '#') {
16 hex = hex.substr(1);
17 }
18
19 // Validate length (must be exactly 6 characters)
20 if (hex.length() != 6) {
21 return false;
22 }
23
24 // Validate hex characters
25 for (size_t i = 0; i < hex.length(); i++) {
26 char c = hex[i];
27 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
28 return false;
29 }
30 }
31
32 // Parse hex digits
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}
unsigned char u8
Definition stdint.h:131
const hex_t hex
Definition ios.cpp.hpp:11
InputGamut g
Definition rgbw.h:125

References fl::FL_NO_EXCEPT, fl::g, and fl::hex.

Referenced by parseSegmentFields().

+ Here is the caller graph for this function: