Skip to content

Commit

Permalink
Added support for HSV color space
Browse files Browse the repository at this point in the history
  • Loading branch information
nothans committed Dec 4, 2024
1 parent d714f2d commit 754ed1d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ An Arduino library to synchronize with the global CheerLights color by fetching

- Fetch the latest CheerLights color name.
- Retrieve RGB values for the current color.
- Retrieve HSV values for the current color.
- Easy integration with LED strips like NeoPixels.

## IDE Installation
Expand All @@ -28,6 +29,7 @@ Include the library in your sketch and initialize it with your WiFi credentials.
- `currentColorName()`: The current CheerLights color name (e.g. "red"). Returns a pointer to a constant char array.
- `currentColorHex()`: The current CheerLights color as a hex value (e.g. 0xFF0000). Returns a uint32_t.
- `currentRed()`, `currentGreen()`, `currentBlue()`: The RGB values for the current CheerLights color (e.g. 255, 0, 0). Returns a uint8_t.
- `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.
- `hasColorChanged()`: Returns a boolean indicating whether the current CheerLights color has changed since the last call to this method.

## Compatibility
Expand Down
8 changes: 8 additions & 0 deletions examples/CheerLights_Basic/CheerLights_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ void loop() {
Serial.print(", ");
Serial.println(CheerLights.currentBlue());

// Print the current CheerLights color as HSV values
Serial.print("Current CheerLights Color HSV: ");
Serial.print(CheerLights.currentHue());
Serial.print(", ");
Serial.print(CheerLights.currentSaturation());
Serial.print(", ");
Serial.println(CheerLights.currentValue());

// Check if the color has changed
if (CheerLights.hasColorChanged()) {
Serial.println("Color has changed!");
Expand Down
3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ currentColorHex KEYWORD2
currentRed KEYWORD2
currentGreen KEYWORD2
currentBlue KEYWORD2
currentHue KEYWORD2
currentSaturation KEYWORD2
currentValue KEYWORD2
begin KEYWORD2
isConnected KEYWORD2
hasColorChanged KEYWORD2
Expand Down
46 changes: 46 additions & 0 deletions src/CheerLights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ CheerLights::CheerLights() {
_colorName[sizeof(_colorName) - 1] = '\0';
_colorHex = 0x000000;
_previousColorHex = 0x000000;
_colorHue = 0;
_colorSaturation = 0;
_colorValue = 0;
}

void CheerLights::begin(const char* ssid, const char* password) {
Expand Down Expand Up @@ -145,6 +148,37 @@ void CheerLights::_fetchColor() {
}
}

// Map the color name to hue, saturation, and value
static const struct {
const char* name;
uint16_t hue;
uint8_t saturation;
uint8_t value;
} colorMapHSV[] = {
{"red", 0, 255, 255},
{"green", 21845, 255, 255},
{"blue", 43690, 255, 255},
{"cyan", 32767, 255, 255},
{"white", 0, 0, 255},
{"warmwhite", 7123, 23, 253},
{"oldlace", 7123, 23, 253},
{"magenta", 54612, 255, 255},
{"yellow", 10922, 255, 255},
{"orange", 6954, 255, 255},
{"purple", 54612, 255, 128},
{"pink", 63627, 63, 255},
{"black", 0, 0, 0}
};

for (const auto& colorHSV : colorMapHSV) {
if (strcasecmp(_colorName, colorHSV.name) == 0) {
_colorHue = colorHSV.hue;
_colorSaturation = colorHSV.saturation;
_colorValue = colorHSV.value;
break;
}
}

_colorChanged = _colorHex != _previousColorHex;
_previousColorHex = _colorHex;
}
Expand Down Expand Up @@ -174,6 +208,18 @@ uint8_t CheerLights::currentBlue() {
return _colorHex & 0xFF;
}

uint16_t CheerLights::currentHue() {
return _colorHue;
}

uint8_t CheerLights::currentSaturation() {
return _colorSaturation;
}

uint8_t CheerLights::currentValue() {
return _colorValue;
}

bool CheerLights::isConnected() {
return WiFi.status() == WL_CONNECTED;
}
Expand Down
6 changes: 6 additions & 0 deletions src/CheerLights.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class CheerLights {
uint8_t currentRed();
uint8_t currentGreen();
uint8_t currentBlue();
uint16_t currentHue();
uint8_t currentSaturation();
uint8_t currentValue();
bool isConnected();
bool hasColorChanged();
private:
Expand All @@ -51,6 +54,9 @@ class CheerLights {
char _colorName[32];
uint32_t _colorHex;
uint32_t _previousColorHex;
uint16_t _colorHue;
uint8_t _colorSaturation;
uint8_t _colorValue;
bool _colorChanged;
};

Expand Down

0 comments on commit 754ed1d

Please sign in to comment.