11#include <unordered_map>
15using namespace std::string_literals;
33constexpr float RColor::kTransparent;
34constexpr float RColor::kSemiTransparent;
35constexpr float RColor::kOpaque;
41bool RColor::ConvertToRGB(
const std::string &
name, std::vector<uint8_t> &rgba)
49 if ((
name.length() != 7) && (
name.length() != 9))
return false;
51 rgba.resize((
name.length() == 7) ? 3 : 4);
54 rgba[0] = std::stoi(
name.substr(1,2),
nullptr, 16);
55 rgba[1] = std::stoi(
name.substr(3,2),
nullptr, 16);
56 rgba[2] = std::stoi(
name.substr(5,2),
nullptr, 16);
57 if (
name.length() == 9)
58 rgba[3] = std::stoi(
name.substr(7,2),
nullptr, 16);
68 static std::unordered_map<std::string,RGB_t> known_colors = {
88 rgba[0] = rgba[1] = rgba[2] = 0;
90 auto known = known_colors.find(
name);
91 if (known != known_colors.end()) {
92 rgba[0] = known->second[0];
93 rgba[1] = known->second[1];
94 rgba[2] = known->second[2];
107 static const char *digits =
"0123456789ABCDEF";
108 std::string res(2,
'0');
109 res[0] = digits[
v >> 4];
110 res[1] = digits[
v & 0xf];
120 if (hex.length() != 6)
return false;
123 SetRGB( std::stoi(hex.substr(0,2),
nullptr, 16),
124 std::stoi(hex.substr(2,2),
nullptr, 16),
125 std::stoi(hex.substr(4,2),
nullptr, 16));
137 if (hex.length() != 6)
return false;
139 SetAlpha(std::stoi(hex,
nullptr, 16));
148 if (
fRGBA.size() > 0)
151 std::vector<uint8_t> rgba;
168 if ((rgba.size() == 4) && with_alpha)
169 res +=
toHex((rgba.size() == 4) ? rgba[3] : 0xff);
184 if (!hex.empty()) hex =
"#"s + hex;
199 float red = arr[0]/255., green = arr[1]/255., blue = arr[2]/255.;
201 hue = light = satur = 0.;
203 float rnorm, gnorm, bnorm, minval, maxval, msum, mdiff;
204 minval = maxval = 0 ;
207 if (green < minval) minval = green;
208 if (blue < minval) minval = blue;
210 if (green > maxval) maxval = green;
211 if (blue > maxval) maxval = blue;
213 rnorm = gnorm = bnorm = 0;
214 mdiff = maxval - minval;
215 msum = maxval + minval;
217 if (maxval != minval) {
218 rnorm = (maxval - red)/mdiff;
219 gnorm = (maxval - green)/mdiff;
220 bnorm = (maxval - blue)/mdiff;
226 if (light < 0.5) satur = mdiff/msum;
227 else satur = mdiff/(2.0 - msum);
229 if (red == maxval) hue = 60.0 * (6.0 + bnorm - gnorm);
230 else if (green == maxval) hue = 60.0 * (2.0 + rnorm - bnorm);
231 else hue = 60.0 * (4.0 + gnorm - rnorm);
233 if (hue > 360) hue = hue - 360;
242 float rh, rl, rs, rm1, rm2;
244 if (hue > 0) { rh = hue;
if (rh > 360) rh = 360; }
245 if (light > 0) { rl = light;
if (rl > 1) rl = 1; }
246 if (satur > 0) { rs = satur;
if (rs > 1) rs = 1; }
248 if (rl <= 0.5) rm2 = rl*(1.0 + rs);
249 else rm2 = rl + rs - rl*rs;
257 auto toRGB = [rm1, rm2] (
float h) {
258 if (
h > 360)
h =
h - 360;
259 if (
h < 0)
h =
h + 360;
260 if (
h < 60 )
return rm1 + (rm2-rm1)*
h/60;
261 if (
h < 180)
return rm2;
262 if (
h < 240)
return rm1 + (rm2-rm1)*(240-
h)/60;
266 SetRGB(toRGB(rh+120), toRGB(rh), toRGB(rh-120));
static constexpr RGB_t kRed
static constexpr RGB_t kLime
void SetRGB(const RGB_t &rgb)
Set r/g/b components of color.
static bool ConvertToRGB(const std::string &name, std::vector< uint8_t > &rgba)
Converts string name of color in RGB value - when possible.
std::vector< uint8_t > fRGBA
RGB + Alpha.
static constexpr RGB_t kYellow
void SetHLS(float hue, float light, float satur)
Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS).
static constexpr RGB_t kPurple
static constexpr RGB_t kGreen
bool GetHLS(float &hue, float &light, float &satur) const
Return the Hue, Light, Saturation (HLS) definition of this RColor.
std::string fName
name of color - if any
bool SetRGBHex(const std::string &hex)
Set RGB values as hex.
std::vector< uint8_t > AsRGBA() const
Returns color as RGBA array, trying also convert color name into RGBA value.
static constexpr RGB_t kFuchsia
static constexpr RGB_t kWhite
bool SetAlphaHex(const std::string &hex)
Set Alpha value as hex.
std::string AsSVG() const
Returns color value as it will be used in SVG drawing It either include hex format #66FF66 or just pl...
static constexpr RGB_t kGrey
static constexpr RGB_t kTeal
static constexpr RGB_t kAqua
static constexpr RGB_t kBlue
static constexpr RGB_t kNavy
std::string AsHex(bool with_alpha=false) const
Returns color value in hex format like "66FF66" - without any prefix Alpha parameter can be optionall...
static constexpr RGB_t kOlive
static constexpr RGB_t kMaroon
void SetAlpha(uint8_t alpha)
Set alpha as value from range 0..255.
std::array< uint8_t, 3 > RGB_t
static std::string toHex(uint8_t v)
Converts integer from 0 to 255 into hex format with two digits like 00.
static constexpr RGB_t kSilver
static constexpr double s