18 namespace Experimental {
32 class RColor :
public RAttrBase {
34 R__ATTR_CLASS(RColor,
"color_", AddString(
"rgb",
"").AddString(
"a",
"").AddString(
"name",
"").AddBool(
"auto",
false));
36 using RGB_t = std::array<int, 3>;
40 static std::string toHex(
int v);
43 RColor &SetRGBFloat(
float r,
float g,
float b)
45 return SetRGB(
int(r*255),
int(g*255),
int(b*255));
48 bool GetRGBFloat(
float &r,
float &g,
float &b)
const;
50 int GetColorComponent(
int indx)
const;
55 RColor(
int r,
int g,
int b) : RColor() { SetRGB(r, g, b); }
58 RColor(
int r,
int g,
int b,
float alpha) : RColor()
65 RColor(
const RGB_t &rgb) : RColor() { SetRGB(rgb); }
68 RColor &SetRGB(
const RGB_t &rgb) {
return SetRGB(rgb[0], rgb[1], rgb[2]); }
71 RColor &SetRGB(
int r,
int g,
int b) {
return SetHex(toHex(r) + toHex(g) + toHex(b)); }
74 RColor &SetHex(
const std::string &_hex)
76 SetValue(
"rgb", _hex);
81 std::string GetHex()
const {
return GetValue<std::string>(
"rgb"); }
83 bool GetRGB(
int &r,
int &g,
int &b)
const;
86 int GetRed()
const {
return GetColorComponent(0); }
89 int GetGreen()
const {
return GetColorComponent(1); }
92 int GetBlue()
const {
return GetColorComponent(2); }
101 RColor &SetName(
const std::string &_name)
104 SetValue(
"name", _name);
109 std::string GetName()
const {
return GetValue<std::string>(
"name"); }
112 void ClearName() { ClearValue(
"name"); }
115 float GetAlpha()
const
117 auto hex = GetAlphaHex();
120 return std::strtol(hex.c_str(),
nullptr, 16) / 255.;
124 std::string GetAlphaHex()
const {
return GetValue<std::string>(
"a"); }
127 bool HasAlpha()
const {
return HasValue(
"a"); }
130 RColor &SetAlpha(
float _alpha) {
return SetAlphaHex(toHex((
int)(_alpha * 255))); }
133 RColor &SetAlphaHex(
const std::string &_alfa)
135 SetValue(
"a", _alfa);
140 void ClearAlpha() { ClearValue(
"a"); }
143 bool IsAuto()
const {
return GetValue<bool>(
"auto"); }
146 RColor &SetAuto(
bool on =
true)
148 SetValue(
"auto", on);
153 void ClearAuto() { ClearValue(
"auto"); }
156 bool GetHLS(
float &hue,
float &light,
float &satur)
const;
159 RColor &SetHLS(
float hue,
float light,
float satur);
163 std::string AsSVG()
const
167 return std::string(
"#") + hex + GetAlphaHex();
182 static constexpr RGB_t kRed{{255, 0, 0}};
183 static constexpr RGB_t kGreen{{0, 255, 0}};
184 static constexpr RGB_t kBlue{{0, 0, 255}};
185 static constexpr RGB_t kWhite{{255, 255, 255}};
186 static constexpr RGB_t kBlack{{0, 0, 0}};
187 static constexpr
double kTransparent{0.};
188 static constexpr
double kOpaque{1.};
190 friend bool operator==(
const RColor &lhs,
const RColor &rhs)
193 return (lhs.GetHex() == rhs.GetHex()) && (lhs.GetName() == rhs.GetName()) &&
194 (lhs.GetAlphaHex() == rhs.GetAlphaHex());