renderd7/source/Color.cpp

24 lines
832 B
C++
Raw Normal View History

2022-07-29 13:19:31 +02:00
#include <renderd7/Color.hpp>
2022-11-12 23:19:41 +01:00
#define RGBA8(r, g, b, a) \
((((r)&0xFF) << 0) | (((g)&0xFF) << 8) | (((b)&0xFF) << 16) | \
(((a)&0xFF) << 24))
2022-07-29 13:19:31 +02:00
2022-11-12 23:19:41 +01:00
uint32_t RenderD7::Color::Hex(const std::string color, uint8_t a) {
if (color.length() < 7 ||
std::regex_search(color.substr(1),
std::regex("[^0-9A-Fa-f]"))) { // invalid color.
return RenderD7::Color::Hex("#000000", 0);
}
int r = std::stoi(color.substr(1, 2), nullptr, 16);
int g = std::stoi(color.substr(3, 2), nullptr, 16);
int b = std::stoi(color.substr(5, 2), nullptr, 16);
return RGBA8(r, g, b, a);
2022-07-29 13:19:31 +02:00
}
2022-11-12 23:19:41 +01:00
std::string RenderD7::Color::RGB2Hex(int r, int g, int b) {
std::stringstream ss;
ss << "#";
ss << std::hex << (r << 16 | g << 8 | b);
return ss.str();
2022-07-29 13:19:31 +02:00
}