From 754ed1d75b9f0263db6202a0f322a609b15712cb Mon Sep 17 00:00:00 2001 From: Hans Scharler Date: Wed, 4 Dec 2024 10:08:13 -0500 Subject: [PATCH] Added support for HSV color space --- README.md | 2 + .../CheerLights_Basic/CheerLights_Basic.ino | 8 ++++ keywords.txt | 3 ++ src/CheerLights.cpp | 46 +++++++++++++++++++ src/CheerLights.h | 6 +++ 5 files changed, 65 insertions(+) diff --git a/README.md b/README.md index 8bf0447..2127e57 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/examples/CheerLights_Basic/CheerLights_Basic.ino b/examples/CheerLights_Basic/CheerLights_Basic.ino index 3519c7e..ee650f3 100644 --- a/examples/CheerLights_Basic/CheerLights_Basic.ino +++ b/examples/CheerLights_Basic/CheerLights_Basic.ino @@ -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!"); diff --git a/keywords.txt b/keywords.txt index 28ccc7b..ef6de20 100644 --- a/keywords.txt +++ b/keywords.txt @@ -5,6 +5,9 @@ currentColorHex KEYWORD2 currentRed KEYWORD2 currentGreen KEYWORD2 currentBlue KEYWORD2 +currentHue KEYWORD2 +currentSaturation KEYWORD2 +currentValue KEYWORD2 begin KEYWORD2 isConnected KEYWORD2 hasColorChanged KEYWORD2 diff --git a/src/CheerLights.cpp b/src/CheerLights.cpp index 974ec98..63a5348 100644 --- a/src/CheerLights.cpp +++ b/src/CheerLights.cpp @@ -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) { @@ -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; } @@ -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; } diff --git a/src/CheerLights.h b/src/CheerLights.h index 79ddad8..896d729 100644 --- a/src/CheerLights.h +++ b/src/CheerLights.h @@ -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: @@ -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; };