From 1f109e9a249624df66316532a5f72b1c60e14dec Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 29 Dec 2021 10:21:01 -0800 Subject: [PATCH] Color gamma (#542) * Fix math overflow * support --- src/internal/NeoGamma.h | 26 ++++++++++++++++++++++++++ src/internal/Rgbw64Color.h | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/internal/NeoGamma.h b/src/internal/NeoGamma.h index 4aaf68e..44d835d 100644 --- a/src/internal/NeoGamma.h +++ b/src/internal/NeoGamma.h @@ -34,6 +34,10 @@ public: { return static_cast(255.0f * NeoEase::Gamma(value / 255.0f) + 0.5f); } + static uint16_t Correct(uint16_t value) + { + return static_cast(65535.0f * NeoEase::Gamma(value / 65535.0f) + 0.5f); + } }; // NeoGammaTableMethod uses 256 bytes of memory, but is significantly faster @@ -44,6 +48,13 @@ public: { return _table[value]; } + /* + static uint16_t Correct(uint16_t value) + { +// NOTE: 16 bit color elements not supported with NeoGammaTableMethod, use NeoGammaEquationMethod + return 0; + } + */ private: static const uint8_t _table[256]; @@ -68,6 +79,21 @@ public: T_METHOD::Correct(original.B), T_METHOD::Correct(original.W) ); } + + Rgb48Color Correct(const Rgb48Color& original) + { + return RgbColor(T_METHOD::Correct(original.R), + T_METHOD::Correct(original.G), + T_METHOD::Correct(original.B)); + } + + Rgbw64Color Correct(const Rgbw64Color& original) + { + return RgbwColor(T_METHOD::Correct(original.R), + T_METHOD::Correct(original.G), + T_METHOD::Correct(original.B), + T_METHOD::Correct(original.W)); + } }; diff --git a/src/internal/Rgbw64Color.h b/src/internal/Rgbw64Color.h index 28a2193..100c520 100644 --- a/src/internal/Rgbw64Color.h +++ b/src/internal/Rgbw64Color.h @@ -227,12 +227,12 @@ struct Rgbw64Color private: inline static uint16_t _elementDim(uint16_t value, uint16_t ratio) { - return (static_cast(value) * (static_cast(ratio) + 1)) >> 16; + return (static_cast(value) * (static_cast(ratio) + 1)) >> 16; } inline static uint16_t _elementBrighten(uint16_t value, uint16_t ratio) { - uint16_t element = ((static_cast(value) + 1) << 16) / (static_cast(ratio) + 1); + uint32_t element = ((static_cast(value) + 1) << 16) / (static_cast(ratio) + 1); if (element > Max) {