3#include <concepts/device.hpp>
13template <
typename Value_t>
16 using color_value_t = Value_t;
18 ~base_converter() =
default;
27 return other == original;
31 Value_t
get()
const {
return value; }
35 Value_t
get_state()
const {
return original.get_state(); }
45 base_converter() =
default;
57 static constexpr auto palette{T::palette()};
61template <hex_color_palette T>
64 static consteval auto make_rgb()
68 auto arr{array<pair<std::array<uint8_t, 3>, uint8_t>
69 , T::palette().size()>{}};
71 for (
const auto& [hex, rgb]
72 : std::views::zip(T::palette(), arr))
74 rgb.first[0] = hex.first >> 16;
75 auto remain{hex.first - ((rgb.first[0] & 0xff) << 16)};
76 rgb.first[1] = remain >> 8;
77 rgb.first[2] = remain - ((rgb.first[1] & 0xff) << 8);
78 rgb.second = hex.second;
85 static constexpr auto palette{make_rgb()};
90template <has_color_palette T,
typename Value_t = u
int8_t>
92 : base_converter<Value_t>
95 color_converter() =
default;
99 if (this->original ==
color)
return;
101 this->original =
color;
102 auto c{this->original.get()};
104 float factor{
static_cast<float>(c[3] / 255)};
105 uint8_t r = c[0] * factor;
106 uint8_t g = c[1] * factor;
107 uint8_t b = c[2] * factor;
108 int min_d{std::numeric_limits<int>::max()};
115 for (
const auto& [rgb, val] : this->palette)
120 auto d = dr*dr + dg*dg + db*db;
130 auto sc{this->original.get_secondary()};
132 float factor{
static_cast<float>(sc[3] / 255)};
133 uint8_t sr = sc[0] * factor;
134 uint8_t sg = sc[1] * factor;
135 uint8_t sb = sc[2] * factor;
136 int s_min_d{std::numeric_limits<int>::max()};
138 for (
const auto& [rgb, val] : this->palette)
141 auto sdr{sr - rgb[0]};
143 auto sdg{sg - rgb[1]};
145 auto sdb{sb - rgb[2]};
146 auto d = dr*dr + dg*dg + db*db;
147 auto sd = sdr*sdr + sdg*sdg + sdb*sdb;
156 this->secondary = val;
165 this->original.clear();
170 return this->original.is_clear()
177 requires (!has_color_palette<T>)
178struct color_converter<T, color::rgba>
179 : base_converter<
color::rgba>
184 value = original.
get();
185 secondary = original.get_secondary();
199 return original.is_clear()
210 requires (!has_color_palette<T>)
211struct color_converter<T, uint8_t>
212 : base_converter<uint8_t>
216 if (original ==
color)
return;
219 auto c{original.get()};
221 float factor{
static_cast<float>(c[3] / 255)};
222 uint8_t r = c[0] * factor;
223 uint8_t g = c[1] * factor;
224 uint8_t b = c[2] * factor;
228 value = 0.299 * r + 0.587 * g + 0.114 * b;
233 factor =
static_cast<float>(c[3] / 255);
238 secondary = 0.299 * r + 0.587 * g + 0.114 * b;
249 return original.is_clear()
Provides access to the device's colour palette.
Definition color_converter.hpp:55
Value_t get_state() const
Retrives the current state.
Definition color_converter.hpp:35
Value_t get_secondary() const
Retrives the secondary device specific colour value.
Definition color_converter.hpp:33
virtual bool is_clear()=0
Checks that the original and device specific colour are set to their initial value.
Value_t get() const
Retrives the main device specific colour value.
Definition color_converter.hpp:31
bool operator==(const color &other) const
Determines equality between the original color and another.
Definition color_converter.hpp:25
virtual void clear()=0
Resets the original and the device specific colour to their initial value.
virtual void set(const color &color)=0
Sets the original color.
void clear() override
Resets the original and the device specific colour to their initial value.
Definition color_converter.hpp:188
void set(const color &color) override
Sets the original color.
Definition color_converter.hpp:181
bool is_clear() override
Checks that the original and device specific colour are set to their initial value.
Definition color_converter.hpp:197
void clear() override
Resets the original and the device specific colour to their initial value.
Definition color_converter.hpp:241
bool is_clear() override
Checks that the original and device specific colour are set to their initial value.
Definition color_converter.hpp:247
void set(const color &color) override
Sets the original color.
Definition color_converter.hpp:214
void clear() override
Resets the original and the device specific colour to their initial value.
Definition color_converter.hpp:162
bool is_clear() override
Checks that the original and device specific colour are set to their initial value.
Definition color_converter.hpp:168
void set(const color &color) override
Sets the original color.
Definition color_converter.hpp:97
Generic colour class holding RGBA values of a primary and a secondary colour. The blinking and pulsin...
Definition color.hpp:12
rgba get_secondary() const noexcept
Retrieves the secondary colour as rgba values.
Definition color.hpp:58
@ constant
Primary colour only.
Definition color.hpp:17
rgba get() const noexcept
Retrieves the primary colour as rgba values.
Definition color.hpp:55