MCPcopy Create free account
hub / github.com/FastLED/FastLED / parseHexColor

Function parseHexColor

src/fl/control/wled/json_helpers.cpp.hpp:10–44  ·  view source on GitHub ↗

Helper function to parse hex color string to RGB

Source from the content-addressed store, hash-verified

8
9// Helper function to parse hex color string to RGB
10bool parseHexColor(const fl::string& hexStr, u8& r, u8& g, u8& b) {
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}
45
46// Helper function to convert RGB to hex string
47fl::string rgbToHex(u8 r, u8 g, u8 b) {

Callers 1

parseSegmentFieldsFunction · 0.85

Calls 2

lengthMethod · 0.45
substrMethod · 0.45

Tested by

no test coverage detected