11#include <unordered_map>
15using namespace std::string_literals;
93 auto len =
fColor.length();
94 return (len > 2) && (
fColor[0] ==
'[') && (
fColor[len-1] ==
']');
107 }
else if (val >= 1.) {
111 auto len =
snprintf(sbuf,
sizeof(sbuf),
"[%7.5f]", val);
112 while ((len > 5) && (sbuf[len-2] ==
'0')) {
145 }
else if (
IsName() && (alpha != 0xFF)) {
148 SetRGBA(rgb[0], rgb[1], rgb[2], alpha);
161 static std::unordered_map<std::string,RGB_t> known_colors = {
180 auto known = known_colors.find(
name);
181 if (known != known_colors.end()) {
182 std::vector<uint8_t> res;
184 res[0] = known->second[0];
185 res[1] = known->second[1];
186 res[2] = known->second[2];
202 std::vector<uint8_t> rgba;
209 if (rgba.size() > 0) {
211 rgba[0] = std::stoi(
fColor.substr(1,2),
nullptr, 16);
212 rgba[1] = std::stoi(
fColor.substr(3,2),
nullptr, 16);
213 rgba[2] = std::stoi(
fColor.substr(5,2),
nullptr, 16);
214 if (rgba.size() == 4)
215 rgba[3] = std::stoi(
fColor.substr(7,2),
nullptr, 16);
235 static const char *digits =
"0123456789ABCDEF";
236 std::string res(2,
'0');
237 res[0] = digits[
v >> 4];
238 res[1] = digits[
v & 0xf];
248 if (hex.length() != 6)
return false;
251 SetRGB( std::stoi(hex.substr(0,2),
nullptr, 16),
252 std::stoi(hex.substr(2,2),
nullptr, 16),
253 std::stoi(hex.substr(4,2),
nullptr, 16));
265 if (hex.length() != 6)
return false;
267 SetAlpha(std::stoi(hex,
nullptr, 16));
282 res +=
toHex((rgba.size() == 4) ? rgba[3] : 0xff);
310 float red = arr[0]/255., green = arr[1]/255., blue = arr[2]/255.;
312 hue = light = satur = 0.;
314 float rnorm, gnorm, bnorm, minval, maxval, msum, mdiff;
315 minval = maxval = 0 ;
318 if (green < minval) minval = green;
319 if (blue < minval) minval = blue;
321 if (green > maxval) maxval = green;
322 if (blue > maxval) maxval = blue;
324 rnorm = gnorm = bnorm = 0;
325 mdiff = maxval - minval;
326 msum = maxval + minval;
328 if (maxval != minval) {
329 rnorm = (maxval - red)/mdiff;
330 gnorm = (maxval - green)/mdiff;
331 bnorm = (maxval - blue)/mdiff;
337 if (light < 0.5) satur = mdiff/msum;
338 else satur = mdiff/(2.0 - msum);
340 if (red == maxval) hue = 60.0 * (6.0 + bnorm - gnorm);
341 else if (green == maxval) hue = 60.0 * (2.0 + rnorm - bnorm);
342 else hue = 60.0 * (4.0 + gnorm - rnorm);
344 if (hue > 360) hue = hue - 360;
353 float rh, rl, rs, rm1, rm2;
355 if (hue > 0) { rh = hue;
if (rh > 360) rh = 360; }
356 if (light > 0) { rl = light;
if (rl > 1) rl = 1; }
357 if (satur > 0) { rs = satur;
if (rs > 1) rs = 1; }
359 if (rl <= 0.5) rm2 = rl*(1.0 + rs);
360 else rm2 = rl + rs - rl*rs;
368 auto toRGB = [rm1, rm2] (
float h) {
369 if (
h > 360)
h =
h - 360;
370 if (
h < 0)
h =
h + 360;
371 if (
h < 60 )
return rm1 + (rm2-rm1)*
h/60;
372 if (
h < 180)
return rm2;
373 if (
h < 240)
return rm1 + (rm2-rm1)*(240-
h)/60;
377 SetRGB(toRGB(rh+120), toRGB(rh), toRGB(rh-120));
385 static RColor autoColor(
"auto");
static std::vector< uint8_t > ConvertNameToRGB(const std::string &name)
Converts string name of color in RGB value - when possible.
static R__DLLEXPORT constexpr RGB_t kRed
static R__DLLEXPORT constexpr RGB_t kLime
bool IsOrdinal() const
Returns if color codes ordinal value from palette.
void SetRGB(const RGB_t &rgb)
Set r/g/b components of color.
std::string fColor
string representation of color
static R__DLLEXPORT constexpr RGB_t kYellow
void SetOrdinal(float val)
Set color as ordinal value from RPalette When object will be painted on the client side,...
void SetHLS(float hue, float light, float satur)
Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS).
static R__DLLEXPORT constexpr RGB_t kPurple
static R__DLLEXPORT constexpr float kOpaque
static R__DLLEXPORT constexpr RGB_t kGreen
bool GetHLS(float &hue, float &light, float &satur) const
Return the Hue, Light, Saturation (HLS) definition of this RColor.
bool SetRGBHex(const std::string &hex)
Set RGB values as hex.
static R__DLLEXPORT constexpr float kSemiTransparent
std::vector< uint8_t > AsRGBA() const
Returns color as RGBA array, trying also convert color name into RGBA value.
static const RColor & AutoColor()
Set the color value from the Hue, Light, Saturation (HLS).
static R__DLLEXPORT constexpr RGB_t kFuchsia
static R__DLLEXPORT constexpr RGB_t kWhite
bool IsRGBA() const
returns true if color stored as RGBA
bool SetAlphaHex(const std::string &hex)
Set Alpha value as hex.
void SetAlpha(uint8_t alpha)
Set alpha as value from range 0..255.
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 R__DLLEXPORT constexpr RGB_t kGrey
static R__DLLEXPORT constexpr RGB_t kBlack
bool IsName() const
Returns true if color specified as name.
static R__DLLEXPORT constexpr RGB_t kTeal
static R__DLLEXPORT constexpr RGB_t kAqua
static R__DLLEXPORT constexpr float kTransparent
static R__DLLEXPORT constexpr RGB_t kBlue
static R__DLLEXPORT 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 R__DLLEXPORT constexpr RGB_t kOlive
float GetOrdinal() const
Return ordinal value, which was set before with SetOrdinal() call.
static R__DLLEXPORT constexpr RGB_t kMaroon
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 R__DLLEXPORT constexpr RGB_t kSilver
bool IsRGB() const
returns true if color stored as RGB
void SetRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha)
Set r/g/b/a components of color, a is integer between 0..255.
bool IsAuto() const
Returns true if color specified as auto color.