forked from Makuna/NeoPixelBus
Enhance ColorObject (#340)
Enahnce Dim Add Brighten NeoPixelBrightnessBus use enhancement
This commit is contained in:
@@ -34,32 +34,12 @@ template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBrightnessBu
|
||||
private:
|
||||
void ConvertColor(typename T_COLOR_FEATURE::ColorObject* color)
|
||||
{
|
||||
if (_brightness)
|
||||
{
|
||||
uint8_t* ptr = (uint8_t*) color;
|
||||
uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize;
|
||||
|
||||
while (ptr != ptrEnd)
|
||||
{
|
||||
uint16_t value = *ptr;
|
||||
*ptr++ = (value * _brightness) >> 8;
|
||||
}
|
||||
}
|
||||
*color = color->Dim(_brightness);
|
||||
}
|
||||
|
||||
void RecoverColor(typename T_COLOR_FEATURE::ColorObject* color) const
|
||||
{
|
||||
if (_brightness)
|
||||
{
|
||||
uint8_t* ptr = (uint8_t*) color;
|
||||
uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize;
|
||||
|
||||
while (ptr != ptrEnd)
|
||||
{
|
||||
uint16_t value = *ptr;
|
||||
*ptr++ = (value << 8) / _brightness;
|
||||
}
|
||||
}
|
||||
*color = color->Brighten(_brightness);
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -110,14 +90,13 @@ public:
|
||||
|
||||
// re-scale existing pixels
|
||||
//
|
||||
uint8_t* ptr = this->Pixels();
|
||||
uint8_t* ptrEnd = ptr + this->PixelsSize();
|
||||
while (ptr != ptrEnd)
|
||||
for (uint16_t indexPixel = 0; indexPixel < NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::PixelCount(); indexPixel++)
|
||||
{
|
||||
uint16_t value = *ptr;
|
||||
*ptr++ = (value * scale) >> 8;
|
||||
typename T_COLOR_FEATURE::ColorObject color = NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::GetPixelColor(indexPixel);
|
||||
color = color.Dim(scale);
|
||||
NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::SetPixelColor(indexPixel, color);
|
||||
}
|
||||
|
||||
|
||||
_brightness = newBrightness;
|
||||
this->Dirty();
|
||||
}
|
||||
|
@@ -166,7 +166,13 @@ uint8_t RgbColor::CalculateBrightness() const
|
||||
RgbColor RgbColor::Dim(uint8_t ratio) const
|
||||
{
|
||||
// specifically avoids float math
|
||||
return RgbColor(R * ratio / 255, G * ratio / 255, B * ratio / 255);
|
||||
return RgbColor(_elementDim(R, ratio), _elementDim(G, ratio), _elementDim(B, ratio));
|
||||
}
|
||||
|
||||
RgbColor RgbColor::Brighten(uint8_t ratio) const
|
||||
{
|
||||
// specifically avoids float math
|
||||
return RgbColor(_elementBrighten(R, ratio), _elementBrighten(G, ratio), _elementBrighten(B, ratio));
|
||||
}
|
||||
|
||||
void RgbColor::Darken(uint8_t delta)
|
||||
|
@@ -109,6 +109,14 @@ struct RgbColor
|
||||
// ------------------------------------------------------------------------
|
||||
RgbColor Dim(uint8_t ratio) const;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Brighten will return a new color that is blended to white with the given ratio
|
||||
// ratio - (0-255) where 255 will return the original color and 0 will return white
|
||||
//
|
||||
// NOTE: This is a simple linear blend
|
||||
// ------------------------------------------------------------------------
|
||||
RgbColor Brighten(uint8_t ratio) const;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Darken will adjust the color by the given delta toward black
|
||||
// NOTE: This is a simple linear change
|
||||
@@ -166,5 +174,23 @@ struct RgbColor
|
||||
uint8_t R;
|
||||
uint8_t G;
|
||||
uint8_t B;
|
||||
|
||||
private:
|
||||
inline static uint8_t _elementDim(uint8_t value, uint8_t ratio)
|
||||
{
|
||||
return (value * (ratio + 1)) >> 8;
|
||||
}
|
||||
|
||||
inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio)
|
||||
{
|
||||
uint16_t element = (value << 8) / (ratio + 1);
|
||||
|
||||
if (element > 255)
|
||||
{
|
||||
element = 255;
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -70,7 +70,13 @@ 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);
|
||||
return RgbwColor(_elementDim(R, ratio), _elementDim(G, ratio), _elementDim(B, ratio), _elementDim(W, ratio));
|
||||
}
|
||||
|
||||
RgbwColor RgbwColor::Brighten(uint8_t ratio) const
|
||||
{
|
||||
// specifically avoids float math
|
||||
return RgbwColor(_elementBrighten(R, ratio), _elementBrighten(G, ratio), _elementBrighten(B, ratio), _elementBrighten(W, ratio));
|
||||
}
|
||||
|
||||
void RgbwColor::Darken(uint8_t delta)
|
||||
|
@@ -136,6 +136,14 @@ struct RgbwColor
|
||||
// ------------------------------------------------------------------------
|
||||
RgbwColor Dim(uint8_t ratio) const;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Brighten will return a new color that is blended to white with the given ratio
|
||||
// ratio - (0-255) where 255 will return the original color and 0 will return white
|
||||
//
|
||||
// NOTE: This is a simple linear blend
|
||||
// ------------------------------------------------------------------------
|
||||
RgbwColor Brighten(uint8_t ratio) const;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Darken will adjust the color by the given delta toward black
|
||||
// NOTE: This is a simple linear change
|
||||
@@ -197,6 +205,22 @@ struct RgbwColor
|
||||
uint8_t B;
|
||||
uint8_t W;
|
||||
|
||||
private:
|
||||
inline static uint8_t _elementDim(uint8_t value, uint8_t ratio)
|
||||
{
|
||||
return (value * (ratio + 1)) >> 8;
|
||||
}
|
||||
|
||||
inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio)
|
||||
{
|
||||
uint16_t element = (value << 8) / (ratio + 1);
|
||||
|
||||
if (element > 255)
|
||||
{
|
||||
element = 255;
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user