Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
5#ifndef MODES_COLORS_UTILS_HPP
6#define MODES_COLORS_UTILS_HPP
7
9
10#include "src/system/ext/scale8.h"
11#include "src/system/ext/math8.h"
12
13#include <cstdint>
14
15//
16// Color utilities likely already defined elsewhere
17// (but this time in pure constexpr-able INLINE c++)
18//
19
21namespace lampda::modes::colors {
22
24static constexpr LMBD_INLINE uint32_t fromRGB(uint8_t r, uint8_t g, uint8_t b)
25{
26 uint32_t rx = r;
27 uint32_t gx = g;
28 return (rx << 16) | (gx << 8) | b;
29}
30
32struct ToRGB
33{
35 constexpr LMBD_INLINE ToRGB(uint32_t color) :
36 r {(uint8_t)((color >> 16) & 0xff)},
37 g {(uint8_t)((color >> 8) & 0xff)},
38 b {(uint8_t)(color & 0xff)}
39 {
40 }
41
42 uint8_t r;
43 uint8_t g;
44 uint8_t b;
45};
46
48static constexpr LMBD_INLINE uint32_t fromGrey(uint32_t w) { return fromRGB(w, w, w); }
49
51static constexpr uint8_t colorRotation[360] = {
52 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 15, 17, 18,
53 20, 22, 24, 26, 28, 30, 32, 35, 37, 39, 42, 44, 47, 49, 52, 55, 58, 60, 63, 66, 69, 72,
54 75, 78, 81, 85, 88, 91, 94, 97, 101, 104, 107, 111, 114, 117, 121, 124, 127, 131, 134, 137, 141, 144,
55 147, 150, 154, 157, 160, 163, 167, 170, 173, 176, 179, 182, 185, 188, 191, 194, 197, 200, 202, 205, 208, 210,
56 213, 215, 217, 220, 222, 224, 226, 229, 231, 232, 234, 236, 238, 239, 241, 242, 244, 245, 246, 248, 249, 250,
57 251, 251, 252, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 251, 251, 250,
58 249, 248, 246, 245, 244, 242, 241, 239, 238, 236, 234, 232, 231, 229, 226, 224, 222, 220, 217, 215, 213, 210,
59 208, 205, 202, 200, 197, 194, 191, 188, 185, 182, 179, 176, 173, 170, 167, 163, 160, 157, 154, 150, 147, 144,
60 141, 137, 134, 131, 127, 124, 121, 117, 114, 111, 107, 104, 101, 97, 94, 91, 88, 85, 81, 78, 75, 72,
61 69, 66, 63, 60, 58, 55, 52, 49, 47, 44, 42, 39, 37, 35, 32, 30, 28, 26, 24, 22, 20, 18,
62 17, 15, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0};
69
71static constexpr LMBD_INLINE uint32_t fromAngleHue(uint16_t angleDegrees)
72{
73 uint8_t r = colorRotation[(angleDegrees + 120) % 360];
74 uint8_t g = colorRotation[angleDegrees % 360];
75 uint8_t b = colorRotation[(angleDegrees + 240) % 360];
76 return fromRGB(r, g, b);
77}
78
79} // namespace lampda::modes::colors
80
82
83namespace lampda::modes::colors {
84
95static uint32_t blend(uint32_t leftColor, uint32_t rightColor, uint16_t blend, bool b16 = false)
96{
97 if (blend == 0)
98 return leftColor;
99 uint16_t blendmax = b16 ? 0xFFFF : 0xFF;
100 if (blend >= blendmax)
101 return rightColor;
102 uint8_t shift = b16 ? 16 : 8;
103
104 ToRGB left_rgb(leftColor);
105 ToRGB right_rgb(rightColor);
106
107 return fromRGB(((right_rgb.r * blend) + (left_rgb.r * (blendmax - blend))) >> shift,
108 ((right_rgb.g * blend) + (left_rgb.g * (blendmax - blend))) >> shift,
109 ((right_rgb.b * blend) + (left_rgb.b * (blendmax - blend))) >> shift);
110}
111
123template<bool isVideoMode = false> static uint32_t fade(uint32_t inputColor, uint8_t fadeAmount)
124{
125 uint32_t res;
126 ToRGB input_rgb(inputColor);
127
128 if (isVideoMode)
129 {
130 res = fromRGB(scale8_video(input_rgb.r, fadeAmount),
131 scale8_video(input_rgb.g, fadeAmount),
132 scale8_video(input_rgb.b, fadeAmount));
133 }
134 else
135 {
136 res = fromRGB(scale8(input_rgb.r, fadeAmount), scale8(input_rgb.g, fadeAmount), scale8(input_rgb.b, fadeAmount));
137 }
138 return res;
139}
140
142template<bool isFast = false> uint32_t add(uint32_t c1, uint32_t c2)
143{
144 ToRGB input1_rgb(c1);
145 ToRGB input2_rgb(c2);
146
147 if (isFast)
148 {
149 return fromRGB(
150 qadd8(input1_rgb.r, input2_rgb.r), qadd8(input1_rgb.g, input2_rgb.g), qadd8(input1_rgb.b, input2_rgb.b));
151 }
152 else
153 {
154 uint32_t r = input1_rgb.r + input2_rgb.r;
155 uint32_t g = input1_rgb.g + input2_rgb.g;
156 uint32_t b = input1_rgb.b + input2_rgb.b;
157 uint16_t max = r;
158 if (g > max)
159 max = g;
160 if (b > max)
161 max = b;
162 if (max < 256)
163 {
164 return fromRGB(r, g, b);
165 }
166 else
167 {
168 return fromRGB(r * 255 / max, g * 255 / max, b * 255 / max);
169 }
170 }
171}
172
173} // namespace lampda::modes::colors
174
175#endif
Contains shorthand macro definitions.
Tools to manipulate colors and their representation.
Definition: gamma.hpp:10
static uint32_t fade(uint32_t inputColor, uint8_t fadeAmount)
fade the color toward black
Definition: utils.hpp:123
static constexpr uint8_t colorRotation[360]
precompiled table for Hue
Definition: utils.hpp:51
static constexpr LMBD_INLINE uint32_t fromGrey(uint32_t w)
Return color (w, w, w) as a single uint32_t integer.
Definition: utils.hpp:48
static uint32_t blend(uint32_t leftColor, uint32_t rightColor, uint16_t blend, bool b16=false)
blend to two colors
Definition: utils.hpp:95
static constexpr LMBD_INLINE uint32_t fromAngleHue(uint16_t angleDegrees)
Given a 360 degrees angle, return a corresponding color as an integer.
Definition: utils.hpp:71
static constexpr LMBD_INLINE uint32_t fromRGB(uint8_t r, uint8_t g, uint8_t b)
Return color (r, g, b) as a single uint32_t integer.
Definition: utils.hpp:24
uint32_t add(uint32_t c1, uint32_t c2)
Add two colors together.
Definition: utils.hpp:142
static constexpr N max(const N a, const N b)
Maximum of two numbers.
Definition: utils.h:37
Define some useful color palettes, and tools to use them.
Exposes (r, g, b) as uint8_t in struct from a single uint32_t color.
Definition: utils.hpp:33
uint8_t b
blue
Definition: utils.hpp:44
constexpr LMBD_INLINE ToRGB(uint32_t color)
convert color bytes to rgb struct
Definition: utils.hpp:35
uint8_t r
red
Definition: utils.hpp:42
uint8_t g
green
Definition: utils.hpp:43