1#ifndef KOUTIL_TERM_COLOR_H
2#define KOUTIL_TERM_COLOR_H
27 enum class Tag : std::uint8_t {
107 if (!hex.starts_with(
'#')) {
111 if (hex.size() - 1 == 3) {
117 }
else if (hex.size() - 1 == 6) {
133 [[nodiscard]]
constexpr std::array<channel_t, 3>
get_channels()
const {
171 if (
'a' <= val && val <=
'f') {
172 return static_cast<std::uint8_t
>(10 + val -
'a');
173 }
else if (
'A' <= val && val <=
'F') {
174 return static_cast<std::uint8_t
>(10 + val -
'A');
176 return static_cast<std::uint8_t
>(val -
'0');
187 if (std::is_constant_evaluated()) {
188 return static_cast<std::uint8_t
>(val * 255.0F + 0.5F);
190 return static_cast<std::uint8_t
>(std::round(val * 255.0F));
220 constexpr std::uint16_t HUE_PART = 360 / 6;
221 constexpr float HUE_PART_FLOAT = 360.0F / 6.0F;
225 return { val, val, val };
230 const auto region = h / HUE_PART;
231 const auto rem =
static_cast<float>(h % HUE_PART) / HUE_PART_FLOAT;
233 const float p = v * (1.0F - s);
234 const float q = v * (1.0F - (s * rem));
235 const auto t = v * (1.0F - (s * (1.0F - rem)));
Structure representing a background color.
Definition color.h:198
constexpr color_bg_t(color_t c)
Definition color.h:199
const color_t color
Definition color.h:202
Structure representing a foreground color.
Definition color.h:208
constexpr color_fg_t(color_t c)
Definition color.h:209
const color_t color
Definition color.h:212
Structure representing a color.
Definition color.h:23
static constexpr color_t from_hsv(std::uint16_t h, float s, float v)
Creates a Color object from HSV values.
Definition color.h:219
constexpr color_bg_t as_bg() const
Converts the color to a ColorBG object.
Definition color.h:215
static consteval color_t from_hex(std::string_view hex)
Creates a Color object from a hexadecimal string.
Definition color.h:106
static consteval std::uint8_t extract_value(char val)
Extracts a value from a hexadecimal character.
Definition color.h:170
constexpr channel_t id() const
Gets the ID value of the color.
Definition color.h:143
const channel_t green
Definition color.h:33
std::uint8_t channel_t
Definition color.h:25
constexpr color_t()
Definition color.h:38
const channel_t blue
Definition color.h:34
static constexpr color_t from_id(channel_t id)
Creates a Color object from an ID value.
Definition color.h:88
constexpr color_fg_t as_fg() const
Converts the color to a ColorFG object.
Definition color.h:217
constexpr std::array< channel_t, 3 > get_channels() const
Gets the RGB channel values of the color.
Definition color.h:133
static constexpr color_t from_rgb(channel_t red, channel_t green, channel_t blue)
Creates a Color object from RGB values.
Definition color.h:80
constexpr color_t(channel_t r, channel_t g, channel_t b)
Constructor with RGB values.
Definition color.h:53
static constexpr std::uint8_t convert_val(float val)
Converts a floating-point value to a channel value.
Definition color.h:186
const Tag tag
Definition color.h:35
constexpr bool operator==(color_t other) const
Definition color.h:159
const channel_t red
Definition color.h:32
constexpr color_t(std::uint8_t id)
Constructor with an ID value.
Definition color.h:66