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