Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
37protected:
38
39 /** Provide possible attributes names and values */
40 RAttrMap CollectDefaults() const override { return RAttrMap().AddString("rgb", "").AddString("a", "").AddString("name", "").AddBool("auto", false); }
41
42 /** Set color as plain SVG name like "white" or "lightblue". Clears RGB component before */
43 void SetName(const std::string &_name) { SetValue("name", _name); }
44
45 /** Returns color as plain SVG name like "white" or "lightblue" */
46 std::string GetName() const { return GetValue<std::string>("name"); }
47
48 /** Clear color plain SVG name (if any) */
49 void ClearName() { ClearValue("name"); }
50
51 /** Returns true if color name was specified */
52 bool HasName() const { return HasValue("name"); }
53
54 /** Set color as hex string like 00FF00 */
55 void SetHex(const std::string &_hex) { SetValue("rgb", _hex); }
56
57 /** Remove color hex value */
58 void ClearHex() { ClearValue("rgb"); }
59
60 /** Returns true if color hex value was specified */
61 bool HasHex() const { return HasValue("rgb"); }
62
63 /** Returns color alpha (opacity) as hex string like FF. Default is empty */
64 std::string GetAlphaHex() const { return GetValue<std::string>("a"); }
65
66 /** Returns true if color alpha (opacity) was specified */
67 bool HasAlpha() const { return HasValue("a"); }
68
69 /** Set color alpha (opacity) value - from 0 to 1 */
70 void SetAlpha(float alpha) { return SetAlphaHex(RColor::toHex((uint8_t)(alpha * 255))); }
71
72 /** Set color alpha (opacity) value as hex */
73 void SetAlphaHex(const std::string &val) { SetValue("a", val); }
74
75 /** Remove color alpha value */
76 void ClearAlpha() { ClearValue("a"); }
77
78public:
79 /** Set r/g/b components of color as hex code, default for the color */
81 {
82 Clear();
83
84 if (!col.GetName().empty()) {
85 SetName(col.GetName());
86 } else {
87 auto rgba = col.GetRGBA();
88 if (rgba.size() > 2) SetHex(RColor::toHex(rgba[0]) + RColor::toHex(rgba[1]) + RColor::toHex(rgba[2]));
89 if (rgba.size() == 4) SetAlphaHex(RColor::toHex(rgba[3]));
90 }
91
92 return *this;
93 }
94
95 /** Extract RColor for given attribute */
97 {
98 RColor res;
99 if (HasName())
100 res.SetName(GetName());
101 else if (HasHex()) {
102 res.SetRGBHex(GetHex());
103 if (HasAlpha())
105 }
106 return res;
107 }
108
109 /** Remove all values which can correspond to RColor value */
110 void Clear()
111 {
112 ClearHex();
113 ClearAlpha();
114 ClearName();
115 ClearAuto();
116 }
117
118 /** Return color as hex string like 00FF00 */
119 std::string GetHex() const { return GetValue<std::string>("rgb"); }
120
121
122 /** Returns true if color should get auto value when primitive drawing is performed */
123 bool IsAuto() const { return GetValue<bool>("auto"); }
124
125 /** Set automatic mode for RAttrColor, will be assigned before primitive painted on the canvas */
126 void SetAuto(bool on = true) { SetValue("auto", on); }
127
128 /** Clear auto flag of the RAttrColor */
129 void ClearAuto() { ClearValue("auto"); }
130
131
132 friend bool operator==(const RAttrColor &lhs, const RAttrColor &rhs) { return lhs.GetColor() == rhs.GetColor(); }
133
134
136 {
137 SetColor(col);
138 return *this;
139 }
140
141};
142
143} // namespace Experimental
144} // namespace ROOT
145
146#endif
uint8_t
Base class for all attributes, used with RDrawable.
Definition RAttrBase.hxx:31
bool HasValue(const std::string &name, bool check_defaults=false) const
void ClearValue(const std::string &name)
Clear value if any with specified name.
void SetValue(const std::string &name, bool value)
Set boolean value.
Access RColor from drawable attributes.
void SetName(const std::string &_name)
Set color as plain SVG name like "white" or "lightblue".
RAttrColor & SetColor(const RColor &col)
Set r/g/b components of color as hex code, default for the color.
void SetHex(const std::string &_hex)
Set color as hex string like 00FF00.
void ClearName()
Clear color plain SVG name (if any)
bool IsAuto() const
Returns true if color should get auto value when primitive drawing is performed.
friend bool operator==(const RAttrColor &lhs, const RAttrColor &rhs)
void SetAlphaHex(const std::string &val)
Set color alpha (opacity) value as hex.
std::string GetHex() const
Return color as hex string like 00FF00.
void SetAlpha(float alpha)
Set color alpha (opacity) value - from 0 to 1.
void ClearHex()
Remove color hex value.
bool HasAlpha() const
Returns true if color alpha (opacity) was specified.
RColor GetColor() const
Extract RColor for given attribute.
void SetAuto(bool on=true)
Set automatic mode for RAttrColor, will be assigned before primitive painted on the canvas.
RAttrColor & operator=(const RColor &col)
std::string GetAlphaHex() const
Returns color alpha (opacity) as hex string like FF.
R__ATTR_CLASS(RAttrColor, "color")
void Clear()
Remove all values which can correspond to RColor value.
std::string GetName() const
Returns color as plain SVG name like "white" or "lightblue".
void ClearAlpha()
Remove color alpha value.
bool HasName() const
Returns true if color name was specified.
void ClearAuto()
Clear auto flag of the RAttrColor.
bool HasHex() const
Returns true if color hex value was specified.
RAttrMap CollectDefaults() const override
Provide possible attributes names and values.
RAttrMap & AddString(const std::string &name, const std::string &value)
Definition RAttrMap.hxx:146
RAttrMap & AddBool(const std::string &name, bool value)
Definition RAttrMap.hxx:143
The color class.
Definition RColor.hxx:35
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
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
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:136
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...