[color] Use integer math in Color::gradient to reduce code size

Replace floating-point arithmetic with integer weighted average.
This eliminates soft-float library calls on platforms without FPU
(e.g., ESP8266), reducing the function from 352 bytes.

The integer version uses uint16_t weighted average which matches
the float version's output in 99.96% of cases (off-by-one in
the remaining 0.04% due to float precision differences).
This commit is contained in:
J. Nick Koston
2026-02-27 08:30:28 -10:00
parent 0f7ac1726d
commit ecf36d60ae
+5 -5
View File
@@ -7,12 +7,12 @@ constinit const Color Color::BLACK(0, 0, 0, 0);
constinit const Color Color::WHITE(255, 255, 255, 255);
Color Color::gradient(const Color &to_color, uint8_t amnt) {
uint8_t inv = 255 - amnt;
Color new_color;
float amnt_f = float(amnt) / 255.0f;
new_color.r = amnt_f * (to_color.r - this->r) + this->r;
new_color.g = amnt_f * (to_color.g - this->g) + this->g;
new_color.b = amnt_f * (to_color.b - this->b) + this->b;
new_color.w = amnt_f * (to_color.w - this->w) + this->w;
new_color.r = (uint16_t(this->r) * inv + uint16_t(to_color.r) * amnt) / 255;
new_color.g = (uint16_t(this->g) * inv + uint16_t(to_color.g) * amnt) / 255;
new_color.b = (uint16_t(this->b) * inv + uint16_t(to_color.b) * amnt) / 255;
new_color.w = (uint16_t(this->w) * inv + uint16_t(to_color.w) * amnt) / 255;
return new_color;
}