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 
17 namespace ROOT {
18 namespace Experimental {
19 
20 class 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 
34 class RColor {
35 
36  friend class RAttrColor;
37 
38  using RGB_t = std::array<uint8_t, 3>;
39 
40 private:
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 
52 public:
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 r/g/b/a components of color */
95  void SetRGBA(uint8_t r, uint8_t g, uint8_t b, float alpha)
96  {
97  fName.clear();
98  fRGBA.resize(4);
99  SetRGB(r,g,b);
100  SetAlphaFloat(alpha);
101  }
102 
103  /** Set alpha as float value from range 0..1 */
104  void SetAlphaFloat(float alpha)
105  {
106  if (alpha <= 0.)
107  SetAlpha(0);
108  else if (alpha >= 1.)
109  SetAlpha(255);
110  else
111  SetAlpha((uint8_t)(alpha * 255));
112  }
113 
114  /** Set alpha as value from range 0..255 */
115  void SetAlpha(uint8_t alpha)
116  {
117  if (fRGBA.empty()) {
119  fName.clear();
120  }
121 
122  if (fRGBA.size() < 4)
123  fRGBA.resize(4);
124 
125  fRGBA[3] = alpha;
126  }
127 
128  /** Returns true if color alpha (opacity) was specified */
129  bool HasAlpha() const { return fRGBA.size() == 4; }
130 
131  /** Returns true if no color is specified */
132  bool IsEmpty() const { return fName.empty() && (fRGBA.size() == 0); }
133 
134  /** Returns color as RGBA array - when exists */
135  const std::vector<uint8_t> &GetRGBA() const { return fRGBA; }
136 
137  /** Returns color as RGBA array, trying also convert color name into RGBA value */
138  std::vector<uint8_t> AsRGBA() const;
139 
140  /** Returns red color component 0..255 */
141  uint8_t GetRed() const
142  {
143  if (fRGBA.size() > 2)
144  return fRGBA[0];
145 
146  std::vector<uint8_t> rgb;
147  return ConvertToRGB(fName, rgb) ? rgb[0] : 0;
148  }
149 
150  /** Returns green color component 0..255 */
152  {
153  if (fRGBA.size() > 2)
154  return fRGBA[1];
155 
156  std::vector<uint8_t> rgb;
157  return ConvertToRGB(fName, rgb) ? rgb[1] : 0;
158  }
159 
160  /** Returns blue color component 0..255 */
161  uint8_t GetBlue() const
162  {
163  if (fRGBA.size() > 2)
164  return fRGBA[2];
165 
166  std::vector<uint8_t> rgb;
167  return ConvertToRGB(fName, rgb) ? rgb[2] : 0;
168  }
169 
170  /** Returns color alpha (opacity) as uint8_t 0..255 */
172  {
173  if (fRGBA.size() > 0)
174  return fRGBA.size() == 4 ? fRGBA[3] : 255;
175 
176  std::vector<uint8_t> rgba;
177  if (ConvertToRGB(fName, rgba) && (rgba.size() == 3))
178  return rgba[3];
179 
180  return 255;
181  }
182 
183  /** Returns color alpha (opacity) as float from 0..1 */
184  float GetAlphaFloat() const
185  {
186  return GetAlpha() / 255.;
187  }
188 
189  /** Set color as plain SVG name like "white" or "lightblue". Clears RGB component before */
190  RColor &SetName(const std::string &name)
191  {
192  fRGBA.clear();
193  fName = name;
194  return *this;
195  }
196 
197  /** Returns color as plain SVG name like "white" or "lightblue" */
198  const std::string &GetName() const { return fName; }
199 
200  /** Return the Hue, Light, Saturation (HLS) definition of this RColor */
201  bool GetHLS(float &hue, float &light, float &satur) const;
202 
203  /** Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS). */
204  void SetHLS(float hue, float light, float satur);
205 
206  std::string AsHex(bool with_alpha = false) const;
207  std::string AsSVG() const;
208 
209  void Clear()
210  {
211  fRGBA.clear();
212  fName.clear();
213  }
214 
215  static constexpr RGB_t kBlack{{0, 0, 0}};
216  static constexpr RGB_t kGreen{{0, 0x80, 0}};
217  static constexpr RGB_t kLime{{0, 0xFF, 0}};
218  static constexpr RGB_t kAqua{{0, 0xFF, 0xFF}};
219  static constexpr RGB_t kPurple{{0x80, 0, 0x80}};
220  static constexpr RGB_t kGrey{{0x80, 0x80, 0x80}};
221  static constexpr RGB_t kFuchsia{{0xFF, 0, 0xFF}};
222  static constexpr RGB_t kNavy{{0, 0, 0x80}};
223  static constexpr RGB_t kBlue{{0, 0, 0xff}};
224  static constexpr RGB_t kTeal{{0, 0x80, 0x80}};
225  static constexpr RGB_t kOlive{{0x80, 0x80, 0}};
226  static constexpr RGB_t kSilver{{0xc0, 0xc0, 0xc0}};
227  static constexpr RGB_t kMaroon{{0x80, 0, 0}};
228  static constexpr RGB_t kRed{{0xff, 0, 0}};
229  static constexpr RGB_t kYellow{{0xff, 0xff, 0}};
230  static constexpr RGB_t kWhite{{0xff, 0xff, 0xff}};
231  static constexpr float kTransparent{0.};
232  static constexpr float kSemiTransparent{0.5};
233  static constexpr float kOpaque{1.};
234 
235  friend bool operator==(const RColor &lhs, const RColor &rhs)
236  {
237  if ((lhs.fName == rhs.fName) && (lhs.fRGBA == rhs.fRGBA)) return true;
238 
239  auto l = lhs.AsRGBA();
240  auto r = rhs.AsRGBA();
241 
242  return !l.empty() && (l==r);
243  }
244 };
245 
246 } // namespace Experimental
247 } // namespace ROOT
248 
249 #endif
l
auto * l
Definition: textangle.C:4
ROOT::Experimental::RColor::SetRGBA
void SetRGBA(uint8_t r, uint8_t g, uint8_t b, float alpha)
Set r/g/b/a components of color.
Definition: RColor.hxx:95
ROOT::Experimental::RColor::SetName
RColor & SetName(const std::string &name)
Set color as plain SVG name like "white" or "lightblue".
Definition: RColor.hxx:190
ROOT::Experimental::RColor::kPurple
static constexpr RGB_t kPurple
Definition: RColor.hxx:219
ROOT::Experimental::RColor::AsRGBA
std::vector< uint8_t > AsRGBA() const
Returns color as RGBA array, trying also convert color name into RGBA value.
Definition: RColor.cxx:146
ROOT::Experimental::RColor::kAqua
static constexpr RGB_t kAqua
Definition: RColor.hxx:218
ROOT::Experimental::RColor::GetAlphaFloat
float GetAlphaFloat() const
Returns color alpha (opacity) as float from 0..1.
Definition: RColor.hxx:184
ROOT::Experimental::RColor::kBlack
static constexpr RGB_t kBlack
Definition: RColor.hxx:215
ROOT::Experimental::RColor::kOlive
static constexpr RGB_t kOlive
Definition: RColor.hxx:225
ROOT::Experimental::RColor::kNavy
static constexpr RGB_t kNavy
Definition: RColor.hxx:222
ROOT::Experimental::RColor::kFuchsia
static constexpr RGB_t kFuchsia
Definition: RColor.hxx:221
ROOT::Experimental::RColor::RColor
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
ROOT::Experimental::RColor::kWhite
static constexpr RGB_t kWhite
Definition: RColor.hxx:230
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::Experimental::RAttrColor
Access RColor from drawable attributes.
Definition: RAttrColor.hxx:33
ROOT::Experimental::RColor::RColor
RColor(uint8_t r, uint8_t g, uint8_t b)
Construct color with provided r,g,b values.
Definition: RColor.hxx:57
ROOT::Experimental::RColor
The color class.
Definition: RColor.hxx:34
ROOT::Experimental::RColor::toHex
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
ROOT::Experimental::RColor::GetBlue
uint8_t GetBlue() const
Returns blue color component 0..255.
Definition: RColor.hxx:161
uint8_t
uint8_t
Definition: Converters.cxx:858
ROOT::Experimental::RColor::GetRed
uint8_t GetRed() const
Returns red color component 0..255.
Definition: RColor.hxx:141
ROOT::Experimental::RColor::kLime
static constexpr RGB_t kLime
Definition: RColor.hxx:217
ROOT::Experimental::RColor::SetRGB
void SetRGB(uint8_t r, uint8_t g, uint8_t b)
Set r/g/b components of color.
Definition: RColor.hxx:75
ROOT::Experimental::RColor::IsEmpty
bool IsEmpty() const
Returns true if no color is specified.
Definition: RColor.hxx:132
ROOT::Experimental::RColor::Clear
void Clear()
Definition: RColor.hxx:209
ROOT::Experimental::RColor::GetAlpha
uint8_t GetAlpha() const
Returns color alpha (opacity) as uint8_t 0..255.
Definition: RColor.hxx:171
ROOT::Experimental::RColor::RGB_t
std::array< uint8_t, 3 > RGB_t
Definition: RColor.hxx:38
v
@ v
Definition: rootcling_impl.cxx:3635
b
#define b(i)
Definition: RSha256.hxx:100
ROOT::Experimental::RColor::SetAlphaFloat
void SetAlphaFloat(float alpha)
Set alpha as float value from range 0..1.
Definition: RColor.hxx:104
ROOT::Experimental::RColor::kBlue
static constexpr RGB_t kBlue
Definition: RColor.hxx:223
ROOT::Experimental::RColor::RColor
RColor(const std::string &name)
Construct color with provided name.
Definition: RColor.hxx:69
ROOT::Experimental::RColor::kMaroon
static constexpr RGB_t kMaroon
Definition: RColor.hxx:227
ROOT::Experimental::RColor::kTeal
static constexpr RGB_t kTeal
Definition: RColor.hxx:224
ROOT::Experimental::RColor::RColor
RColor(const RGB_t &rgb)
Construct color with provided RGB_t value.
Definition: RColor.hxx:66
ROOT::Experimental::RColor::AsSVG
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
ROOT::Experimental::RColor::HasAlpha
bool HasAlpha() const
Returns true if color alpha (opacity) was specified.
Definition: RColor.hxx:129
ROOT::Experimental::RColor::RColor
RColor()=default
ROOT::Experimental::RColor::kSemiTransparent
static constexpr float kSemiTransparent
Definition: RColor.hxx:232
ROOT::Experimental::RColor::GetGreen
uint8_t GetGreen() const
Returns green color component 0..255.
Definition: RColor.hxx:151
ROOT::Experimental::RColor::GetHLS
bool GetHLS(float &hue, float &light, float &satur) const
Return the Hue, Light, Saturation (HLS) definition of this RColor.
Definition: RColor.cxx:193
ROOT::Experimental::RColor::ConvertToRGB
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
ROOT::Experimental::RColor::kGreen
static constexpr RGB_t kGreen
Definition: RColor.hxx:216
ROOT::Experimental::RColor::SetRGBHex
bool SetRGBHex(const std::string &hex)
Set RGB values as hex.
Definition: RColor.cxx:118
ROOT::Experimental::RColor::kYellow
static constexpr RGB_t kYellow
Definition: RColor.hxx:229
ROOT::Experimental::RColor::operator==
friend bool operator==(const RColor &lhs, const RColor &rhs)
Definition: RColor.hxx:235
ROOT::Experimental::RColor::fName
std::string fName
name of color - if any
Definition: RColor.hxx:43
ROOT::Experimental::RColor::GetRGBA
const std::vector< uint8_t > & GetRGBA() const
Returns color as RGBA array - when exists.
Definition: RColor.hxx:135
ROOT::Experimental::RColor::kOpaque
static constexpr float kOpaque
Definition: RColor.hxx:233
ROOT::Experimental::RColor::SetRGBA
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
ROOT::Experimental::RColor::GetName
const std::string & GetName() const
Returns color as plain SVG name like "white" or "lightblue".
Definition: RColor.hxx:198
ROOT::Experimental::RColor::SetRGB
void SetRGB(const RGB_t &rgb)
Set r/g/b components of color.
Definition: RColor.hxx:72
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Experimental::RColor::SetAlphaHex
bool SetAlphaHex(const std::string &hex)
Set Alpha value as hex.
Definition: RColor.cxx:135
ROOT::Experimental::RColor::fRGBA
std::vector< uint8_t > fRGBA
RGB + Alpha.
Definition: RColor.hxx:42
ROOT::Experimental::RColor::kGrey
static constexpr RGB_t kGrey
Definition: RColor.hxx:220
ROOT::Experimental::RColor::AsHex
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
ROOT::Experimental::RColor::SetAlpha
void SetAlpha(uint8_t alpha)
Set alpha as value from range 0..255.
Definition: RColor.hxx:115
ROOT::Experimental::RColor::SetHLS
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
ROOT::Experimental::RColor::kTransparent
static constexpr float kTransparent
Definition: RColor.hxx:231
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Experimental::RColor::kRed
static constexpr RGB_t kRed
Definition: RColor.hxx:228
ROOT::Experimental::RColor::kSilver
static constexpr RGB_t kSilver
Definition: RColor.hxx:226
g
#define g(i)
Definition: RSha256.hxx:105