Logo ROOT  
Reference Guide
RAttrColor.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_RAttrColor
10#define ROOT7_RAttrColor
11
12#include <ROOT/RAttrBase.hxx>
13
14#include <ROOT/RColor.hxx>
15
16#include <array>
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 RAttrColor
25\ingroup GpadROOT7
26\brief Access RColor from drawable attributes
27\author Sergey Linev <S.Linev@gsi.de>
28\date 2020-03-09
29\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is
30welcome!
31*/
32
33class RAttrColor : public RAttrBase {
34
36 AddString("rgb", "").AddString("a", "").AddString("name", "").AddBool("auto", false));
37
38protected:
39
40 /** Set color as plain SVG name like "white" or "lightblue". Clears RGB component before */
41 void SetName(const std::string &_name) { SetValue("name", _name); }
42
43 /** Returns color as plain SVG name like "white" or "lightblue" */
44 std::string GetName() const { return GetValue<std::string>("name"); }
45
46 /** Clear color plain SVG name (if any) */
47 void ClearName() { ClearValue("name"); }
48
49 /** Returns true if color name was specified */
50 bool HasName() const { return HasValue("name"); }
51
52 /** Set color as hex string like 00FF00 */
53 void SetHex(const std::string &_hex) { SetValue("rgb", _hex); }
54
55 /** Remove color hex value */
56 void ClearHex() { ClearValue("rgb"); }
57
58 /** Returns true if color hex value was specified */
59 bool HasHex() const { return HasValue("rgb"); }
60
61 /** Returns color alpha (opacity) as hex string like FF. Default is empty */
62 std::string GetAlphaHex() const { return GetValue<std::string>("a"); }
63
64 /** Returns true if color alpha (opacity) was specified */
65 bool HasAlpha() const { return HasValue("a"); }
66
67 /** Set color alpha (opacity) value - from 0 to 1 */
68 void SetAlpha(float alpha) { return SetAlphaHex(RColor::toHex((uint8_t)(alpha * 255))); }
69
70 /** Set color alpha (opacity) value as hex */
71 void SetAlphaHex(const std::string &val) { SetValue("a", val); }
72
73 /** Remove color alpha value */
74 void ClearAlpha() { ClearValue("a"); }
75
76public:
77 /** Set r/g/b components of color as hex code, default for the color */
79 {
80 Clear();
81
82 if (!col.GetName().empty()) {
83 SetName(col.GetName());
84 } else {
85 auto rgba = col.GetRGBA();
86 if (rgba.size() > 2) SetHex(RColor::toHex(rgba[0]) + RColor::toHex(rgba[1]) + RColor::toHex(rgba[2]));
87 if (rgba.size() == 4) SetAlphaHex(RColor::toHex(rgba[3]));
88 }
89
90 return *this;
91
92 }
93
94 /** Extract RColor for given attribute */
96 {
97 RColor res;
98 if (HasName())
99 res.SetName(GetName());
100 else if (HasHex()) {
101 res.SetRGBHex(GetHex());
102 if (HasAlpha())
104 }
105 return res;
106 }
107
108 /** Remove all values which can correspond to RColor value */
109 void Clear()
110 {
111 ClearHex();
112 ClearAlpha();
113 ClearName();
114 ClearAuto();
115 }
116
117 /** Return color as hex string like 00FF00 */
118 std::string GetHex() const { return GetValue<std::string>("rgb"); }
119
120
121 /** Returns true if color should get auto value when primitive drawing is performed */
122 bool IsAuto() const { return GetValue<bool>("auto"); }
123
124 /** Set automatic mode for RAttrColor, will be assigned before primitive painted on the canvas */
125 void SetAuto(bool on = true) { SetValue("auto", on); }
126
127 /** Clear auto flag of the RAttrColor */
128 void ClearAuto() { ClearValue("auto"); }
129
130
131 friend bool operator==(const RAttrColor &lhs, const RAttrColor &rhs) { return lhs.GetColor() == rhs.GetColor(); }
132
133
135 {
136 SetColor(col);
137 return *this;
138 }
139
140};
141
142} // namespace Experimental
143} // namespace ROOT
144
145#endif
uint8_t
Definition: Converters.cxx:858
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:27
bool HasValue(const std::string &name, bool check_defaults=false) const
Definition: RAttrBase.hxx:163
void ClearValue(const std::string &name)
Clear value if any with specified name.
Definition: RAttrBase.cxx:114
void SetValue(const std::string &name, bool value)
Set boolean value.
Definition: RAttrBase.cxx:133
Access RColor from drawable attributes.
Definition: RAttrColor.hxx:33
void SetName(const std::string &_name)
Set color as plain SVG name like "white" or "lightblue".
Definition: RAttrColor.hxx:41
RAttrColor & SetColor(const RColor &col)
Set r/g/b components of color as hex code, default for the color.
Definition: RAttrColor.hxx:78
void SetHex(const std::string &_hex)
Set color as hex string like 00FF00.
Definition: RAttrColor.hxx:53
void ClearName()
Clear color plain SVG name (if any)
Definition: RAttrColor.hxx:47
bool IsAuto() const
Returns true if color should get auto value when primitive drawing is performed.
Definition: RAttrColor.hxx:122
friend bool operator==(const RAttrColor &lhs, const RAttrColor &rhs)
Definition: RAttrColor.hxx:131
void SetAlphaHex(const std::string &val)
Set color alpha (opacity) value as hex.
Definition: RAttrColor.hxx:71
std::string GetHex() const
Return color as hex string like 00FF00.
Definition: RAttrColor.hxx:118
void SetAlpha(float alpha)
Set color alpha (opacity) value - from 0 to 1.
Definition: RAttrColor.hxx:68
void ClearHex()
Remove color hex value.
Definition: RAttrColor.hxx:56
bool HasAlpha() const
Returns true if color alpha (opacity) was specified.
Definition: RAttrColor.hxx:65
RColor GetColor() const
Extract RColor for given attribute.
Definition: RAttrColor.hxx:95
void SetAuto(bool on=true)
Set automatic mode for RAttrColor, will be assigned before primitive painted on the canvas.
Definition: RAttrColor.hxx:125
R__ATTR_CLASS(RAttrColor, "color_", AddString("rgb", "").AddString("a", "").AddString("name", "").AddBool("auto", false))
RAttrColor & operator=(const RColor &col)
Definition: RAttrColor.hxx:134
std::string GetAlphaHex() const
Returns color alpha (opacity) as hex string like FF.
Definition: RAttrColor.hxx:62
void Clear()
Remove all values which can correspond to RColor value.
Definition: RAttrColor.hxx:109
std::string GetName() const
Returns color as plain SVG name like "white" or "lightblue".
Definition: RAttrColor.hxx:44
void ClearAlpha()
Remove color alpha value.
Definition: RAttrColor.hxx:74
bool HasName() const
Returns true if color name was specified.
Definition: RAttrColor.hxx:50
void ClearAuto()
Clear auto flag of the RAttrColor.
Definition: RAttrColor.hxx:128
bool HasHex() const
Returns true if color hex value was specified.
Definition: RAttrColor.hxx:59
The color class.
Definition: RColor.hxx:34
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
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
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
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