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
21// TODO: see also imagemagick's C++ interface for RColor operations!
22// https://www.imagemagick.org/api/magick++-classes.php
23
24/** \class RColor
25\ingroup GpadROOT7
26\brief The color class
27\author Axel Naumann <axel@cern.ch>
28\author Sergey Linev <S.Linev@gsi.de>
29\date 2017-09-26
30\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
31*/
32
33class RColor {
34
35 using RGB_t = std::array<uint8_t, 3>;
36
37private:
38
39 std::string fColor; ///< string representation of color
40
41 static std::string toHex(uint8_t v);
42
43 static std::vector<uint8_t> ConvertNameToRGB(const std::string &name);
44
45 bool SetRGBHex(const std::string &hex);
46 bool SetAlphaHex(const std::string &hex);
47
48public:
49
50 RColor() = default;
51
52 /** Construct color with provided r,g,b values */
53 RColor(uint8_t r, uint8_t g, uint8_t b) { SetRGB(r, g, b); }
54
55 /** Construct color with provided r,g,b and alpha values */
56 RColor(uint8_t r, uint8_t g, uint8_t b, float alpha)
57 {
58 SetRGBA(r, g, b, alpha);
59 }
60
61 /** Construct color with provided RGB_t value */
62 RColor(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); };
63
64 /** Construct color with provided string */
65 RColor(const std::string &color) { SetColor(color); };
66
67 /** Construct color with provided ordinal value */
68 RColor(float ordinal) { SetOrdinal(ordinal); };
69
70 /** Returns true if color is empty */
71 bool IsEmpty() const { return fColor.empty(); }
72
73 bool IsRGB() const;
74 bool IsRGBA() const;
75 bool IsName() const;
76 bool IsAuto() const;
77 bool IsOrdinal() const;
78
79 /** Set r/g/b components of color */
80 void SetRGB(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); }
81
82 /** Set r/g/b components of color */
83 void SetRGB(uint8_t r, uint8_t g, uint8_t b);
84
85 /** Set r/g/b/a components of color, a is integer between 0..255 */
86 void SetRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha);
87
88 /** Set alpha as value from range 0..255 */
89 void SetAlpha(uint8_t alpha);
90
91 /** Set alpha as float value from range 0..1 */
92 void SetAlphaFloat(float alpha)
93 {
94 if (alpha <= 0.)
95 SetAlpha(0);
96 else if (alpha >= 1.)
97 SetAlpha(255);
98 else
99 SetAlpha((uint8_t)(alpha * 255));
100 }
101
102 /** Returns true if color alpha (opacity) was specified */
103 bool HasAlpha() const { return IsRGBA(); }
104
105 /** Returns color as RGBA array, trying also convert color name into RGBA value */
106 std::vector<uint8_t> AsRGBA() const;
107
108 /** Returns red color component 0..255 */
109 uint8_t GetRed() const
110 {
111 auto rgba = AsRGBA();
112 return rgba.size() > 2 ? rgba[0] : 0;
113 }
114
115 /** Returns green color component 0..255 */
116 uint8_t GetGreen() const
117 {
118 auto rgba = AsRGBA();
119 return rgba.size() > 2 ? rgba[1] : 0;
120 }
121
122 /** Returns blue color component 0..255 */
123 uint8_t GetBlue() const
124 {
125 auto rgba = AsRGBA();
126 return rgba.size() > 2 ? rgba[2] : 0;
127 }
128
129 /** Returns color alpha (opacity) as uint8_t 0..255 */
130 uint8_t GetAlpha() const
131 {
132 auto rgba = AsRGBA();
133 return rgba.size() > 3 ? rgba[3] : 0xFF;
134 }
135
136 /** Returns color alpha (opacity) as float from 0..1 */
137 float GetAlphaFloat() const
138 {
139 return GetAlpha() / 255.;
140 }
141
142 /** Set color as plain SVG name like "white" or "lightblue" */
143 bool SetName(const std::string &name)
144 {
145 fColor = name;
146 if (!IsName()) {
147 Clear();
148 return false;
149 }
150 return true;
151 }
152
153 void SetOrdinal(float val);
154 float GetOrdinal() const;
155
156 /** Returns color as it stored as string */
157 const std::string& AsString() const { return fColor; }
158
159 /** Set color as string */
160 void SetColor(const std::string &col) { fColor = col; }
161
162 /** Return the Hue, Light, Saturation (HLS) definition of this RColor */
163 bool GetHLS(float &hue, float &light, float &satur) const;
164
165 /** Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS). */
166 void SetHLS(float hue, float light, float satur);
167
168 std::string AsHex(bool with_alpha = false) const;
169 std::string AsSVG() const;
170
171 void Clear()
172 {
173 fColor.clear();
174 }
175
176 static const RColor &AutoColor();
177
178 R__DLLEXPORT static constexpr RGB_t kBlack{{0, 0, 0}};
179 R__DLLEXPORT static constexpr RGB_t kGreen{{0, 0x80, 0}};
180 R__DLLEXPORT static constexpr RGB_t kLime{{0, 0xFF, 0}};
181 R__DLLEXPORT static constexpr RGB_t kAqua{{0, 0xFF, 0xFF}};
182 R__DLLEXPORT static constexpr RGB_t kPurple{{0x80, 0, 0x80}};
183 R__DLLEXPORT static constexpr RGB_t kGrey{{0x80, 0x80, 0x80}};
184 R__DLLEXPORT static constexpr RGB_t kFuchsia{{0xFF, 0, 0xFF}};
185 R__DLLEXPORT static constexpr RGB_t kNavy{{0, 0, 0x80}};
186 R__DLLEXPORT static constexpr RGB_t kBlue{{0, 0, 0xff}};
187 R__DLLEXPORT static constexpr RGB_t kTeal{{0, 0x80, 0x80}};
188 R__DLLEXPORT static constexpr RGB_t kOlive{{0x80, 0x80, 0}};
189 R__DLLEXPORT static constexpr RGB_t kSilver{{0xc0, 0xc0, 0xc0}};
190 R__DLLEXPORT static constexpr RGB_t kMaroon{{0x80, 0, 0}};
191 R__DLLEXPORT static constexpr RGB_t kRed{{0xff, 0, 0}};
192 R__DLLEXPORT static constexpr RGB_t kYellow{{0xff, 0xff, 0}};
193 R__DLLEXPORT static constexpr RGB_t kWhite{{0xff, 0xff, 0xff}};
194 R__DLLEXPORT static constexpr float kTransparent{0.};
195 R__DLLEXPORT static constexpr float kSemiTransparent{0.5};
196 R__DLLEXPORT static constexpr float kOpaque{1.};
197
198 friend bool operator==(const RColor &lhs, const RColor &rhs)
199 {
200 if (lhs.fColor == rhs.fColor) return true;
201
202 auto l = lhs.AsRGBA();
203 auto r = rhs.AsRGBA();
204
205 return !l.empty() && (l == r);
206 }
207};
208
209} // namespace Experimental
210} // namespace ROOT
211
212#endif
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char name[80]
Definition TGX11.cxx:110
The color class.
Definition RColor.hxx:33
static std::vector< uint8_t > ConvertNameToRGB(const std::string &name)
Converts string name of color in RGB value - when possible.
Definition RColor.cxx:156
static R__DLLEXPORT constexpr RGB_t kRed
Definition RColor.hxx:191
static R__DLLEXPORT constexpr RGB_t kLime
Definition RColor.hxx:180
friend bool operator==(const RColor &lhs, const RColor &rhs)
Definition RColor.hxx:198
bool IsOrdinal() const
Returns if color codes ordinal value from palette.
Definition RColor.cxx:91
void SetRGB(const RGB_t &rgb)
Set r/g/b components of color.
Definition RColor.hxx:80
std::string fColor
string representation of color
Definition RColor.hxx:39
static R__DLLEXPORT constexpr RGB_t kYellow
Definition RColor.hxx:192
void SetOrdinal(float val)
Set color as ordinal value from RPalette When object will be painted on the client side,...
Definition RColor.cxx:103
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:56
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:351
static R__DLLEXPORT constexpr RGB_t kPurple
Definition RColor.hxx:182
static R__DLLEXPORT constexpr float kOpaque
Definition RColor.hxx:196
static R__DLLEXPORT constexpr RGB_t kGreen
Definition RColor.hxx:179
uint8_t GetBlue() const
Returns blue color component 0..255.
Definition RColor.hxx:123
bool GetHLS(float &hue, float &light, float &satur) const
Return the Hue, Light, Saturation (HLS) definition of this RColor.
Definition RColor.cxx:304
bool SetRGBHex(const std::string &hex)
Set RGB values as hex.
Definition RColor.cxx:246
void SetAlphaFloat(float alpha)
Set alpha as float value from range 0..1.
Definition RColor.hxx:92
static R__DLLEXPORT constexpr float kSemiTransparent
Definition RColor.hxx:195
RColor(float ordinal)
Construct color with provided ordinal value
Definition RColor.hxx:68
std::vector< uint8_t > AsRGBA() const
Returns color as RGBA array, trying also convert color name into RGBA value.
Definition RColor.cxx:197
static const RColor & AutoColor()
Set the color value from the Hue, Light, Saturation (HLS).
Definition RColor.cxx:383
static R__DLLEXPORT constexpr RGB_t kFuchsia
Definition RColor.hxx:184
bool SetName(const std::string &name)
Set color as plain SVG name like "white" or "lightblue".
Definition RColor.hxx:143
static R__DLLEXPORT constexpr RGB_t kWhite
Definition RColor.hxx:193
bool IsRGBA() const
returns true if color stored as RGBA
Definition RColor.cxx:50
bool SetAlphaHex(const std::string &hex)
Set Alpha value as hex.
Definition RColor.cxx:263
bool IsEmpty() const
Returns true if color is empty.
Definition RColor.hxx:71
RColor(const RGB_t &rgb)
Construct color with provided RGB_t value.
Definition RColor.hxx:62
void SetAlpha(uint8_t alpha)
Set alpha as value from range 0..255.
Definition RColor.cxx:133
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:291
static R__DLLEXPORT constexpr RGB_t kGrey
Definition RColor.hxx:183
static R__DLLEXPORT constexpr RGB_t kBlack
Definition RColor.hxx:178
bool IsName() const
Returns true if color specified as name.
Definition RColor.cxx:75
static R__DLLEXPORT constexpr RGB_t kTeal
Definition RColor.hxx:187
static R__DLLEXPORT constexpr RGB_t kAqua
Definition RColor.hxx:181
RColor(uint8_t r, uint8_t g, uint8_t b)
Construct color with provided r,g,b values.
Definition RColor.hxx:53
void SetColor(const std::string &col)
Set color as string.
Definition RColor.hxx:160
static R__DLLEXPORT constexpr float kTransparent
Definition RColor.hxx:194
const std::string & AsString() const
Returns color as it stored as string.
Definition RColor.hxx:157
uint8_t GetGreen() const
Returns green color component 0..255.
Definition RColor.hxx:116
uint8_t GetAlpha() const
Returns color alpha (opacity) as uint8_t 0..255.
Definition RColor.hxx:130
static R__DLLEXPORT constexpr RGB_t kBlue
Definition RColor.hxx:186
static R__DLLEXPORT constexpr RGB_t kNavy
Definition RColor.hxx:185
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:275
static R__DLLEXPORT constexpr RGB_t kOlive
Definition RColor.hxx:188
float GetOrdinal() const
Return ordinal value, which was set before with SetOrdinal() call.
Definition RColor.cxx:124
bool HasAlpha() const
Returns true if color alpha (opacity) was specified.
Definition RColor.hxx:103
static R__DLLEXPORT constexpr RGB_t kMaroon
Definition RColor.hxx:190
uint8_t GetRed() const
Returns red color component 0..255.
Definition RColor.hxx:109
float GetAlphaFloat() const
Returns color alpha (opacity) as float from 0..1.
Definition RColor.hxx:137
std::array< uint8_t, 3 > RGB_t
Definition RColor.hxx:35
static std::string toHex(uint8_t v)
Converts integer from 0 to 255 into hex format with two digits like 00.
Definition RColor.cxx:233
static R__DLLEXPORT constexpr RGB_t kSilver
Definition RColor.hxx:189
bool IsRGB() const
returns true if color stored as RGB
Definition RColor.cxx:42
RColor(const std::string &color)
Construct color with provided string
Definition RColor.hxx:65
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.
Definition RColor.cxx:66
bool IsAuto() const
Returns true if color specified as auto color.
Definition RColor.cxx:83
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
#define R__DLLEXPORT
TLine l
Definition textangle.C:4