diff --git a/keywords.txt b/keywords.txt index fce24f3..295945b 100644 --- a/keywords.txt +++ b/keywords.txt @@ -349,6 +349,7 @@ SetPixelColor KEYWORD2 GetPixelColor KEYWORD2 SwapPixelColor KEYWORD2 CalculateBrightness KEYWORD2 +Dim KEYWORD2 Darken KEYWORD2 Lighten KEYWORD2 LinearBlend KEYWORD2 diff --git a/src/internal/RgbColor.cpp b/src/internal/RgbColor.cpp index 29ae434..436dfed 100644 --- a/src/internal/RgbColor.cpp +++ b/src/internal/RgbColor.cpp @@ -163,6 +163,12 @@ uint8_t RgbColor::CalculateBrightness() const return (uint8_t)(((uint16_t)R + (uint16_t)G + (uint16_t)B) / 3); } +RgbColor RgbColor::Dim(uint8_t ratio) const +{ + // specifically avoids float math + return RgbColor(R * ratio / 255, G * ratio / 255, B * ratio / 255); +} + void RgbColor::Darken(uint8_t delta) { if (R > delta) diff --git a/src/internal/RgbColor.h b/src/internal/RgbColor.h index 5161123..994189a 100644 --- a/src/internal/RgbColor.h +++ b/src/internal/RgbColor.h @@ -98,6 +98,14 @@ struct RgbColor // ------------------------------------------------------------------------ uint8_t CalculateBrightness() const; + // ------------------------------------------------------------------------ + // Dim will return a new color that is blended to black with the given ratio + // ratio - (0-255) where 255 will return the original color and 0 will return black + // + // NOTE: This is a simple linear blend + // ------------------------------------------------------------------------ + RgbColor Dim(uint8_t ratio) const; + // ------------------------------------------------------------------------ // Darken will adjust the color by the given delta toward black // NOTE: This is a simple linear change diff --git a/src/internal/RgbwColor.cpp b/src/internal/RgbwColor.cpp index 062834d..6bc56d0 100644 --- a/src/internal/RgbwColor.cpp +++ b/src/internal/RgbwColor.cpp @@ -67,6 +67,12 @@ uint8_t RgbwColor::CalculateBrightness() const } } +RgbwColor RgbwColor::Dim(uint8_t ratio) const +{ + // specifically avoids float math + return RgbwColor(R * ratio / 255, G * ratio / 255, B * ratio / 255, W * ratio / 255); +} + void RgbwColor::Darken(uint8_t delta) { if (R > delta) diff --git a/src/internal/RgbwColor.h b/src/internal/RgbwColor.h index 38f2530..66e3bd1 100644 --- a/src/internal/RgbwColor.h +++ b/src/internal/RgbwColor.h @@ -126,6 +126,14 @@ struct RgbwColor // ------------------------------------------------------------------------ uint8_t CalculateBrightness() const; + // ------------------------------------------------------------------------ + // Dim will return a new color that is blended to black with the given ratio + // ratio - (0-255) where 255 will return the original color and 0 will return black + // + // NOTE: This is a simple linear blend + // ------------------------------------------------------------------------ + RgbwColor Dim(uint8_t ratio) const; + // ------------------------------------------------------------------------ // Darken will adjust the color by the given delta toward black // NOTE: This is a simple linear change