Logo ROOT  
Reference Guide
RColor.hxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
9#ifndef ROOT7_RColor
10#define ROOT7_RColor
11
12#include <cstdint>
13#include <vector>
14#include <string>
15#include <array>
16
17namespace ROOT {
18namespace Experimental {
19
20class RAttrColor;
21
22// TODO: see also imagemagick's C++ interface for RColor operations!
23// https://www.imagemagick.org/api/magick++-classes.php
24
25/** \class RColor
26\ingroup GpadROOT7
27\brief The color class
28\author Axel Naumann <axel@cern.ch>
29\author Sergey Linev <S.Linev@gsi.de>
30\date 2017-09-26
31\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
32*/
33
34class RColor {
35
36 friend class RAttrColor;
37
38 using RGB_t = std::array<uint8_t, 3>;
39
40private:
41
42 std::vector<uint8_t> fRGBA; ///< RGB + Alpha
43 std::string fName; ///< name of color - if any
44
45 static std::string toHex(uint8_t v);
46
47 static bool ConvertToRGB(const std::string &name, std::vector<uint8_t> &rgba);
48
49 bool SetRGBHex(const std::string &hex);
50 bool SetAlphaHex(const std::string &hex);
51
52public:
53
54 RColor() = default;
55
56 /** Construct color with provided r,g,b values */
58
59 /** Construct color with provided r,g,b and alpha values */
60 RColor(uint8_t r, uint8_t g, uint8_t b, float alpha)
61 {
62 SetRGBA(r, g, b, alpha);
63 }
64
65 /** Construct color with provided RGB_t value */
66 RColor(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); };
67
68 /** Construct color with provided name */
69 RColor(const std::string &name) { SetName(name); };
70
71 /** Set r/g/b components of color */
72 void SetRGB(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); }
73
74 /** Set r/g/b components of color */
76 {
77 fName.clear();
78 if (fRGBA.size() < 3)
79 fRGBA.resize(3);
80 fRGBA[0] = r;
81 fRGBA[1] = g;
82 fRGBA[2] = b;
83 }
84
85 /** Set r/g/b/a components of color */
87 {
88 fName.clear();
89 fRGBA.resize(4);
90 SetRGB(r,g,b);
91 SetAlpha(alpha);
92 }
93
94 /** Set alpha as float value from range 0..1 */
95 void SetAlphaFloat(float alpha)
96 {
97 if (alpha <= 0.)
98 SetAlpha(0);
99 else if (alpha >= 1.)
100 SetAlpha(255);
101 else
102 SetAlpha((uint8_t)(alpha * 255));
103 }
104
105 /** Set alpha as value from range 0..255 */
106 void SetAlpha(uint8_t alpha)
107 {
108 if (fRGBA.empty()) {
110 fName.clear();
111 }
112
113 if (fRGBA.size() < 4)
114 fRGBA.resize(4);
115
116 fRGBA[3] = alpha;
117 }
118
119 /** Returns true if color alpha (opacity) was specified */
120 bool HasAlpha() const { return fRGBA.size() == 4; }
121
122 /** Returns true if no color is specified */
123 bool IsEmpty() const { return fName.empty() && (fRGBA.size() == 0); }
124
125 /** Returns color as RGBA array - when exists */
126 const std::vector<uint8_t> &GetRGBA() const { return fRGBA; }
127
128 /** Returns color as RGBA array, trying also convert color name into RGBA value */
129 std::vector<uint8_t> AsRGBA() const;
130
131 /** Returns red color component 0..255 */
133 {
134 if (fRGBA.size() > 2)
135 return fRGBA[0];
136
137 std::vector<uint8_t> rgb;
138 return ConvertToRGB(fName, rgb) ? rgb[0] : 0;
139 }
140
141 /** Returns green color component 0..255 */
143 {
144 if (fRGBA.size() > 2)
145 return fRGBA[1];
146
147 std::vector<uint8_t> rgb;
148 return ConvertToRGB(fName, rgb) ? rgb[1] : 0;
149 }
150
151 /** Returns blue color component 0..255 */
153 {
154 if (fRGBA.size() > 2)
155 return fRGBA[2];
156
157 std::vector<uint8_t> rgb;
158 return ConvertToRGB(fName, rgb) ? rgb[2] : 0;
159 }
160
161 /** Returns color alpha (opacity) as uint8_t 0..255 */
163 {
164 if (fRGBA.size() > 0)
165 return fRGBA.size() == 4 ? fRGBA[3] : 255;
166
167 std::vector<uint8_t> rgba;
168 if (ConvertToRGB(fName, rgba) && (rgba.size() == 3))
169 return rgba[3];
170
171 return 255;
172 }
173
174 /** Returns color alpha (opacity) as float from 0..1 */
175 float GetAlphaFloat() const
176 {
177 return GetAlpha() / 255.;
178 }
179
180 /** Set color as plain SVG name like "white" or "lightblue". Clears RGB component before */
181 RColor &SetName(const std::string &name)
182 {
183 fRGBA.clear();
184 fName = name;
185 return *this;
186 }
187
188 /** Returns color as plain SVG name like "white" or "lightblue" */
189 const std::string &GetName() const { return fName; }
190
191 /** Return the Hue, Light, Saturation (HLS) definition of this RColor */
192 bool GetHLS(float &hue, float &light, float &satur) const;
193
194 /** Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS). */
195 void SetHLS(float hue, float light, float satur);
196
197 std::string AsHex(bool with_alpha = false) const;
198 std::string AsSVG() const;
199
200 void Clear()
201 {
202 fRGBA.clear();
203 fName.clear();
204 }
205
206 static constexpr RGB_t kBlack{{0, 0, 0}};
207 static constexpr RGB_t kGreen{{0, 0x80, 0}};
208 static constexpr RGB_t kLime{{0, 0xFF, 0}};
209 static constexpr RGB_t kAqua{{0, 0xFF, 0xFF}};
210 static constexpr RGB_t kPurple{{0x80, 0, 0x80}};
211 static constexpr RGB_t kGrey{{0x80, 0x80, 0x80}};
212 static constexpr RGB_t kFuchsia{{0xFF, 0, 0xFF}};
213 static constexpr RGB_t kNavy{{0, 0, 0x80}};
214 static constexpr RGB_t kBlue{{0, 0, 0xff}};
215 static constexpr RGB_t kTeal{{0, 0x80, 0x80}};
216 static constexpr RGB_t kOlive{{0x80, 0x80, 0}};
217 static constexpr RGB_t kSilver{{0xc0, 0xc0, 0xc0}};
218 static constexpr RGB_t kMaroon{{0x80, 0, 0}};
219 static constexpr RGB_t kRed{{0xff, 0, 0}};
220 static constexpr RGB_t kYellow{{0xff, 0xff, 0}};
221 static constexpr RGB_t kWhite{{0xff, 0xff, 0xff}};
222 static constexpr float kTransparent{0.};
223 static constexpr float kSemiTransparent{0.5};
224 static constexpr float kOpaque{1.};
225
226 friend bool operator==(const RColor &lhs, const RColor &rhs)
227 {
228 if ((lhs.fName == rhs.fName) && (lhs.fRGBA == rhs.fRGBA)) return true;
229
230 auto l = lhs.AsRGBA();
231 auto r = rhs.AsRGBA();
232
233 return !l.empty() && (l==r);
234 }
235};
236
237} // namespace Experimental
238} // namespace ROOT
239
240#endif
uint8_t
Definition: Converters.cxx:858
ROOT::R::TRInterface & r
Definition: Object.C:4
#define b(i)
Definition: RSha256.hxx:100
#define g(i)
Definition: RSha256.hxx:105
char name[80]
Definition: TGX11.cxx:109
Access RColor from drawable attributes.
Definition: RAttrColor.hxx:33
The color class.
Definition: RColor.hxx:34
void SetRGB(uint8_t r, uint8_t g, uint8_t b)
Set r/g/b components of color.
Definition: RColor.hxx:75
static constexpr RGB_t kRed
Definition: RColor.hxx:219
static constexpr RGB_t kLime
Definition: RColor.hxx:208
friend bool operator==(const RColor &lhs, const RColor &rhs)
Definition: RColor.hxx:226
RColor(const std::string &name)
Construct color with provided name.
Definition: RColor.hxx:69
void SetRGB(const RGB_t &rgb)
Set r/g/b components of color.
Definition: RColor.hxx:72
static bool ConvertToRGB(const std::string &name, std::vector< uint8_t > &rgba)
Converts string name of color in RGB value - when possible.
Definition: RColor.cxx:41
std::vector< uint8_t > fRGBA
RGB + Alpha.
Definition: RColor.hxx:42
static constexpr RGB_t kYellow
Definition: RColor.hxx:220
RColor(uint8_t r, uint8_t g, uint8_t b, float alpha)
Construct color with provided r,g,b and alpha values.
Definition: RColor.hxx:60
void SetHLS(float hue, float light, float satur)
Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS).
Definition: RColor.cxx:240
static constexpr RGB_t kPurple
Definition: RColor.hxx:210
static constexpr float kOpaque
Definition: RColor.hxx:224
static constexpr RGB_t kGreen
Definition: RColor.hxx:207
uint8_t GetBlue() const
Returns blue color component 0..255.
Definition: RColor.hxx:152
bool GetHLS(float &hue, float &light, float &satur) const
Return the Hue, Light, Saturation (HLS) definition of this RColor.
Definition: RColor.cxx:193
std::string fName
name of color - if any
Definition: RColor.hxx:43
const std::string & GetName() const
Returns color as plain SVG name like "white" or "lightblue".
Definition: RColor.hxx:189
bool SetRGBHex(const std::string &hex)
Set RGB values as hex.
Definition: RColor.cxx:118
void SetAlphaFloat(float alpha)
Set alpha as float value from range 0..1.
Definition: RColor.hxx:95
static constexpr float kSemiTransparent
Definition: RColor.hxx:223
std::vector< uint8_t > AsRGBA() const
Returns color as RGBA array, trying also convert color name into RGBA value.
Definition: RColor.cxx:146
static constexpr RGB_t kFuchsia
Definition: RColor.hxx:212
static constexpr RGB_t kWhite
Definition: RColor.hxx:221
bool SetAlphaHex(const std::string &hex)
Set Alpha value as hex.
Definition: RColor.cxx:135
RColor & SetName(const std::string &name)
Set color as plain SVG name like "white" or "lightblue".
Definition: RColor.hxx:181
void SetRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha)
Set r/g/b/a components of color.
Definition: RColor.hxx:86
bool IsEmpty() const
Returns true if no color is specified.
Definition: RColor.hxx:123
RColor(const RGB_t &rgb)
Construct color with provided RGB_t value.
Definition: RColor.hxx:66
std::string AsSVG() const
Returns color value as it will be used in SVG drawing It either include hex format #66FF66 or just pl...
Definition: RColor.cxx:178
static constexpr RGB_t kGrey
Definition: RColor.hxx:211
static constexpr RGB_t kBlack
Definition: RColor.hxx:206
static constexpr RGB_t kTeal
Definition: RColor.hxx:215
static constexpr RGB_t kAqua
Definition: RColor.hxx:209
RColor(uint8_t r, uint8_t g, uint8_t b)
Construct color with provided r,g,b values.
Definition: RColor.hxx:57
static constexpr float kTransparent
Definition: RColor.hxx:222
uint8_t GetGreen() const
Returns green color component 0..255.
Definition: RColor.hxx:142
uint8_t GetAlpha() const
Returns color alpha (opacity) as uint8_t 0..255.
Definition: RColor.hxx:162
static constexpr RGB_t kBlue
Definition: RColor.hxx:214
static constexpr RGB_t kNavy
Definition: RColor.hxx:213
std::string AsHex(bool with_alpha=false) const
Returns color value in hex format like "66FF66" - without any prefix Alpha parameter can be optionall...
Definition: RColor.cxx:162
static constexpr RGB_t kOlive
Definition: RColor.hxx:216
bool HasAlpha() const
Returns true if color alpha (opacity) was specified.
Definition: RColor.hxx:120
static constexpr RGB_t kMaroon
Definition: RColor.hxx:218
uint8_t GetRed() const
Returns red color component 0..255.
Definition: RColor.hxx:132
void SetAlpha(uint8_t alpha)
Set alpha as value from range 0..255.
Definition: RColor.hxx:106
float GetAlphaFloat() const
Returns color alpha (opacity) as float from 0..1.
Definition: RColor.hxx:175
std::array< uint8_t, 3 > RGB_t
Definition: RColor.hxx:38
static std::string toHex(uint8_t v)
Converts integer from 0 to 255 into hex format with two digits like 00.
Definition: RColor.cxx:105
static constexpr RGB_t kSilver
Definition: RColor.hxx:217
const std::vector< uint8_t > & GetRGBA() const
Returns color as RGBA array - when exists.
Definition: RColor.hxx:126
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
auto * l
Definition: textangle.C:4