From 0f45e8d4813686b2a7ccb55615990dc0c9a685f6 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 27 Jul 2020 11:38:47 -0700 Subject: [PATCH] Fix 8bit math (#360) --- src/internal/RgbColor.h | 9 ++++++--- src/internal/RgbwColor.h | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/internal/RgbColor.h b/src/internal/RgbColor.h index 8ed524f..b50857b 100644 --- a/src/internal/RgbColor.h +++ b/src/internal/RgbColor.h @@ -178,18 +178,21 @@ struct RgbColor private: inline static uint8_t _elementDim(uint8_t value, uint8_t ratio) { - return (value * (ratio + 1)) >> 8; + return (static_cast(value) * (static_cast(ratio) + 1)) >> 8; } inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio) { - uint16_t element = (value << 8) / (ratio + 1); + uint16_t element = ((static_cast(value) + 1) << 8) / (static_cast(ratio) + 1); if (element > 255) { element = 255; } - + else + { + element -= 1; + } return element; } }; diff --git a/src/internal/RgbwColor.h b/src/internal/RgbwColor.h index ee08906..73732b5 100644 --- a/src/internal/RgbwColor.h +++ b/src/internal/RgbwColor.h @@ -208,18 +208,21 @@ struct RgbwColor private: inline static uint8_t _elementDim(uint8_t value, uint8_t ratio) { - return (value * (ratio + 1)) >> 8; + return (static_cast(value) * (static_cast(ratio) + 1)) >> 8; } inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio) { - uint16_t element = (value << 8) / (ratio + 1); + uint16_t element = ((static_cast(value) + 1) << 8) / (static_cast(ratio) + 1); if (element > 255) { element = 255; } - + else + { + element -= 1; + } return element; } };