Skip to content

Commit 754ed1d

Browse files
committed
Added support for HSV color space
1 parent d714f2d commit 754ed1d

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ An Arduino library to synchronize with the global CheerLights color by fetching
66

77
- Fetch the latest CheerLights color name.
88
- Retrieve RGB values for the current color.
9+
- Retrieve HSV values for the current color.
910
- Easy integration with LED strips like NeoPixels.
1011

1112
## IDE Installation
@@ -28,6 +29,7 @@ Include the library in your sketch and initialize it with your WiFi credentials.
2829
- `currentColorName()`: The current CheerLights color name (e.g. "red"). Returns a pointer to a constant char array.
2930
- `currentColorHex()`: The current CheerLights color as a hex value (e.g. 0xFF0000). Returns a uint32_t.
3031
- `currentRed()`, `currentGreen()`, `currentBlue()`: The RGB values for the current CheerLights color (e.g. 255, 0, 0). Returns a uint8_t.
32+
- `currentHue()`, `currentSaturation()`, `currentValue()`: The HSV values for the current CheerLights color (e.g. 0, 255, 255). Returns a uint16_t for hue and uint8_t for saturation and value.
3133
- `hasColorChanged()`: Returns a boolean indicating whether the current CheerLights color has changed since the last call to this method.
3234

3335
## Compatibility

examples/CheerLights_Basic/CheerLights_Basic.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ void loop() {
7272
Serial.print(", ");
7373
Serial.println(CheerLights.currentBlue());
7474

75+
// Print the current CheerLights color as HSV values
76+
Serial.print("Current CheerLights Color HSV: ");
77+
Serial.print(CheerLights.currentHue());
78+
Serial.print(", ");
79+
Serial.print(CheerLights.currentSaturation());
80+
Serial.print(", ");
81+
Serial.println(CheerLights.currentValue());
82+
7583
// Check if the color has changed
7684
if (CheerLights.hasColorChanged()) {
7785
Serial.println("Color has changed!");

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ currentColorHex KEYWORD2
55
currentRed KEYWORD2
66
currentGreen KEYWORD2
77
currentBlue KEYWORD2
8+
currentHue KEYWORD2
9+
currentSaturation KEYWORD2
10+
currentValue KEYWORD2
811
begin KEYWORD2
912
isConnected KEYWORD2
1013
hasColorChanged KEYWORD2

src/CheerLights.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CheerLights::CheerLights() {
55
_colorName[sizeof(_colorName) - 1] = '\0';
66
_colorHex = 0x000000;
77
_previousColorHex = 0x000000;
8+
_colorHue = 0;
9+
_colorSaturation = 0;
10+
_colorValue = 0;
811
}
912

1013
void CheerLights::begin(const char* ssid, const char* password) {
@@ -145,6 +148,37 @@ void CheerLights::_fetchColor() {
145148
}
146149
}
147150

151+
// Map the color name to hue, saturation, and value
152+
static const struct {
153+
const char* name;
154+
uint16_t hue;
155+
uint8_t saturation;
156+
uint8_t value;
157+
} colorMapHSV[] = {
158+
{"red", 0, 255, 255},
159+
{"green", 21845, 255, 255},
160+
{"blue", 43690, 255, 255},
161+
{"cyan", 32767, 255, 255},
162+
{"white", 0, 0, 255},
163+
{"warmwhite", 7123, 23, 253},
164+
{"oldlace", 7123, 23, 253},
165+
{"magenta", 54612, 255, 255},
166+
{"yellow", 10922, 255, 255},
167+
{"orange", 6954, 255, 255},
168+
{"purple", 54612, 255, 128},
169+
{"pink", 63627, 63, 255},
170+
{"black", 0, 0, 0}
171+
};
172+
173+
for (const auto& colorHSV : colorMapHSV) {
174+
if (strcasecmp(_colorName, colorHSV.name) == 0) {
175+
_colorHue = colorHSV.hue;
176+
_colorSaturation = colorHSV.saturation;
177+
_colorValue = colorHSV.value;
178+
break;
179+
}
180+
}
181+
148182
_colorChanged = _colorHex != _previousColorHex;
149183
_previousColorHex = _colorHex;
150184
}
@@ -174,6 +208,18 @@ uint8_t CheerLights::currentBlue() {
174208
return _colorHex & 0xFF;
175209
}
176210

211+
uint16_t CheerLights::currentHue() {
212+
return _colorHue;
213+
}
214+
215+
uint8_t CheerLights::currentSaturation() {
216+
return _colorSaturation;
217+
}
218+
219+
uint8_t CheerLights::currentValue() {
220+
return _colorValue;
221+
}
222+
177223
bool CheerLights::isConnected() {
178224
return WiFi.status() == WL_CONNECTED;
179225
}

src/CheerLights.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class CheerLights {
4141
uint8_t currentRed();
4242
uint8_t currentGreen();
4343
uint8_t currentBlue();
44+
uint16_t currentHue();
45+
uint8_t currentSaturation();
46+
uint8_t currentValue();
4447
bool isConnected();
4548
bool hasColorChanged();
4649
private:
@@ -51,6 +54,9 @@ class CheerLights {
5154
char _colorName[32];
5255
uint32_t _colorHex;
5356
uint32_t _previousColorHex;
57+
uint16_t _colorHue;
58+
uint8_t _colorSaturation;
59+
uint8_t _colorValue;
5460
bool _colorChanged;
5561
};
5662

0 commit comments

Comments
 (0)