diff --git a/src/internal/colors/Rgb16Color.h b/src/internal/colors/Rgb16Color.h index 96da4f3..aa08164 100644 --- a/src/internal/colors/Rgb16Color.h +++ b/src/internal/colors/Rgb16Color.h @@ -66,6 +66,16 @@ struct Rgb16Color : RgbColorBase { }; + // ------------------------------------------------------------------------ + // Construct a Rgb16Color using RgbCOlor + // ------------------------------------------------------------------------ + Rgb16Color(const RgbColor& color) + { + setR(color.R); + setG(color.G); + setB(color.B); + }; + // ------------------------------------------------------------------------ // Construct a Rgb16Color using HtmlColor // ------------------------------------------------------------------------ @@ -143,6 +153,23 @@ struct Rgb16Color : RgbColorBase return (Color565 & 0x001f) << 3; }; + // ------------------------------------------------------------------------ + // operator [] - readonly + // access elements in order by index rather than R,G,B + // see static Count for the number of elements + // ------------------------------------------------------------------------ + uint8_t operator[](size_t idx) const + { + switch (idx) + { + case 0: + return getR(); + case 1: + return getG(); + default: + return getB(); + } + } // ------------------------------------------------------------------------ // Comparison operators @@ -281,5 +308,8 @@ struct Rgb16Color : RgbColorBase }; uint16_t Color565; + + const static uint8_t Max = 255; + const static size_t Count = 3; // three elements in [] }; diff --git a/src/internal/colors/SegmentDigit.cpp b/src/internal/colors/SegmentDigit.cpp index a04af91..2984cf8 100644 --- a/src/internal/colors/SegmentDigit.cpp +++ b/src/internal/colors/SegmentDigit.cpp @@ -120,6 +120,28 @@ uint8_t SevenSegDigit::CalculateBrightness() const return static_cast(sum / Count); } +SevenSegDigit SevenSegDigit::Dim(uint8_t ratio) const +{ + SevenSegDigit result; + + for (uint8_t iSegment = 0; iSegment < Count; iSegment++) + { + result.Segment[iSegment] = _elementDim(Segment[iSegment], ratio); + } + return result; +} + +SevenSegDigit SevenSegDigit::Brighten(uint8_t ratio) const +{ + SevenSegDigit result; + + for (uint8_t iSegment = 0; iSegment < Count; iSegment++) + { + result.Segment[iSegment] = _elementBrighten(Segment[iSegment], ratio); + } + return result; +} + void SevenSegDigit::Darken(uint8_t delta) { for (uint8_t iSegment = 0; iSegment < Count; iSegment++) diff --git a/src/internal/colors/SegmentDigit.h b/src/internal/colors/SegmentDigit.h index 19d3930..abf8649 100644 --- a/src/internal/colors/SegmentDigit.h +++ b/src/internal/colors/SegmentDigit.h @@ -146,6 +146,22 @@ struct SevenSegDigit // ------------------------------------------------------------------------ uint8_t CalculateBrightness() const; + // ------------------------------------------------------------------------ + // Dim will return a new SevenSegDigit that is blended to off with the given ratio + // ratio - (0-255) where 255 will return the original brightness and 0 will return off + // + // NOTE: This is a simple linear blend + // ------------------------------------------------------------------------ + SevenSegDigit Dim(uint8_t ratio) const; + + // ------------------------------------------------------------------------ + // Brighten will return a new SevenSegDigit that is blended to full bright with the given ratio + // ratio - (0-255) where 255 will return the original brightness and 0 will return full brightness + // + // NOTE: This is a simple linear blend + // ------------------------------------------------------------------------ + SevenSegDigit Brighten(uint8_t ratio) const; + // ------------------------------------------------------------------------ // Darken will adjust the color by the given delta toward black // NOTE: This is a simple linear change @@ -285,5 +301,25 @@ struct SevenSegDigit protected: void init(uint8_t bitmask, uint8_t brightness, uint8_t defaultBrightness); + + inline static uint8_t _elementDim(uint8_t value, uint8_t ratio) + { + return (static_cast(value) * (static_cast(ratio) + 1)) >> 8; + } + + inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio) + { + uint16_t element = ((static_cast(value) + 1) << 8) / (static_cast(ratio) + 1); + + if (element > Max) + { + element = Max; + } + else + { + element -= 1; + } + return element; + } };