1#ifndef KOUTIL_TERM_COLOR_H
2#define KOUTIL_TERM_COLOR_H
27 enum class Tag : std::uint8_t {
66 constexpr Color(std::uint8_t
id)
98 static constexpr Color from_hsv(std::uint16_t h,
float s,
float v);
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 ColorBG(Color c)
Definition color.h:199
const Color color
Definition color.h:202
Structure representing a foreground color.
Definition color.h:208
const Color color
Definition color.h:212
constexpr ColorFG(Color c)
Definition color.h:209
Structure representing a color.
Definition color.h:23
constexpr channel_t id() const
Gets the ID value of the color.
Definition color.h:143
static constexpr Color from_id(channel_t id)
Creates a Color object from an ID value.
Definition color.h:88
static constexpr std::uint8_t convert_val(float val)
Converts a floating-point value to a channel value.
Definition color.h:186
constexpr Color(std::uint8_t id)
Constructor with an ID value.
Definition color.h:66
constexpr bool operator==(Color other) const
Definition color.h:159
constexpr Color()
Definition color.h:38
constexpr ColorBG as_bg() const
Converts the color to a ColorBG object.
Definition color.h:215
const channel_t red
Definition color.h:32
constexpr ColorFG as_fg() const
Converts the color to a ColorFG object.
Definition color.h:217
static constexpr Color from_rgb(channel_t red, channel_t green, channel_t blue)
Creates a Color object from RGB values.
Definition color.h:80
const Tag tag
Definition color.h:35
static consteval std::uint8_t extract_value(char val)
Extracts a value from a hexadecimal character.
Definition color.h:170
const channel_t blue
Definition color.h:34
const channel_t green
Definition color.h:33
static consteval Color from_hex(std::string_view hex)
Creates a Color object from a hexadecimal string.
Definition color.h:106
constexpr std::array< channel_t, 3 > get_channels() const
Gets the RGB channel values of the color.
Definition color.h:133
std::uint8_t channel_t
Definition color.h:25
static constexpr Color from_hsv(std::uint16_t h, float s, float v)
Creates a Color object from HSV values.
Definition color.h:219
constexpr Color(channel_t r, channel_t g, channel_t b)
Constructor with RGB values.
Definition color.h:53