[Common] Split RGB into a new Common library

This commit is contained in:
TennesseeTrash 2025-06-14 19:52:18 +02:00
parent 3b5eb6f1a4
commit 133c3d1a01
6 changed files with 22 additions and 6 deletions

View file

@ -0,0 +1,11 @@
add_library(KVGToolsCommon INTERFACE)
target_compile_features(KVGToolsCommon
INTERFACE
cxx_std_23
)
target_include_directories(KVGToolsCommon
INTERFACE
"Include"
)

View file

@ -0,0 +1,111 @@
#ifndef KVG_TOOLS_COMMON_RGB_HPP
#define KVG_TOOLS_COMMON_RGB_HPP
#include <cstdint>
#include <string>
#include <string_view>
namespace Common
{
namespace Implementation
{
constexpr std::uint8_t HexToDec(char ch)
{
if (ch >= '0' && ch <= '9') {
return std::uint8_t(ch - '0');
}
if (ch >= 'A' && ch <= 'F') {
return std::uint8_t(ch - 'A' + 10);
}
if (ch >= 'a' && ch <= 'f') {
return std::uint8_t(ch - 'a' + 10);
}
return 0;
}
constexpr char DecToHex(std::uint8_t val)
{
if (val < 10) {
return char(val + '0');
}
if (val < 16) {
return char(val - 10 + 'A');
}
return '0';
}
constexpr std::uint8_t ToDec(char upper, char lower)
{
return HexToDec(upper) << 4 | HexToDec(lower);
}
constexpr std::uint8_t ToDec(char ch)
{
return ToDec(ch, ch);
}
constexpr void ChannelToHex(std::uint8_t val, char& ch1, char& ch2)
{
ch1 = DecToHex(15 & (val >> 4));
ch2 = DecToHex(15 & (val));
}
}
struct RGB
{
std::uint8_t Red;
std::uint8_t Green;
std::uint8_t Blue;
static constexpr RGB FromHex(std::string_view hex)
{
if (hex.empty()) {
return RGB { .Red = 0, .Green = 0, .Blue = 0 };
}
if (hex[0] == '#') {
hex = std::string_view(hex.data() + 1, hex.length() - 1);
}
for (char ch : hex) {
if ((ch < '0' || ch > '9') && (ch < 'A' || ch > 'F') && (ch < 'a' && ch > 'f')) {
return RGB { .Red = 0, .Green = 0, .Blue = 0 };
}
}
if (hex.length() == 3) {
return RGB {
.Red = Implementation::ToDec(hex[0]),
.Green = Implementation::ToDec(hex[1]),
.Blue = Implementation::ToDec(hex[2]),
};
}
if (hex.length() == 6) {
return RGB {
.Red = Implementation::ToDec(hex[0], hex[1]),
.Green = Implementation::ToDec(hex[2], hex[3]),
.Blue = Implementation::ToDec(hex[4], hex[5]),
};
}
return RGB { .Red = 0, .Green = 0, .Blue = 0 };
}
constexpr std::string ToHex() const
{
std::string result(7, '#');
Implementation::ChannelToHex(Red, result[1], result[2]);
Implementation::ChannelToHex(Green, result[3], result[4]);
Implementation::ChannelToHex(Blue, result[5], result[6]);
return result;
}
};
}
#endif // KVG_TOOLS_COMMON_RGB_HPP