Logo ROOT   6.12/07
Reference Guide
TColor.hxx
Go to the documentation of this file.
1 /// \file ROOT/TPalette.hxx
2 /// \ingroup Gpad ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2017-09-26
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_TColor
17 #define ROOT7_TColor
18 
19 #include <array>
20 
21 namespace ROOT {
22 namespace Experimental {
23 
24 /** \class ROOT::Experimental::TColor
25  A color: Red|Green|Blue|Alpha, or a position in a TPalette
26  */
27 class TColor {
28 public:
29  /** \class ROOT::Experimental::TColor::TAlpha
30  The alpha value of a color: 0 is completely transparent, 1 is completely opaque.
31  */
32  struct Alpha {
33  float fVal;
34  explicit operator float() const { return fVal; }
35  };
36  /// An opaque color.
37  static constexpr Alpha kOpaque{1.};
38  /// A completely transparent color.
39  static constexpr Alpha kTransparent{0.};
40 
41  enum class EKind {
42  kRGBA, ///< The color is defined as specific RGBA values.
43  kPalettePos, ///< The color is defined as a value in the `TFrame`'s `TPalette`.
44  kAuto ///< The color will be set upon drawing the canvas choosing a `TPalatte` color, see `TColor(Auto_t)`
45  };
46 
47 private:
48  // TODO: use a `variant` here!
49  /// The "R" in RGBA (0 <= R <= 1), or the palette pos if fKind is `kPalettePos`.
50  float fRedOrPalettePos = 0.;
51 
52  /// The "G" in RGBA (0 <= G <= 1). Unused if `fKind != kRGBA`.
53  float fGreen = 0.;
54 
55  /// The "B" in RGBA (0 <= B <= 1). Unused if `fKind != kRGBA`.
56  float fBlue = 0.;
57 
58  /// The "A" in RGBA (0 <= A <= 1). Unused if `fKind != kRGBA`. `fAlpha == 0` means so transparent it's invisible,
59  /// `fAlpha == 1` means completely opaque.
60  float fAlpha = 1.;
61 
62  /// How the color is defined.
64 
65  /// throw an exception if the color isn't specified as `kRGBA` or `kAuto`, the two cases where
66  /// asking for RBGA members makes sense.
67  bool AssertNotPalettePos() const;
68 
69 public:
70  using PredefinedRGB = std::array<float, 3>;
71 
72  // Default constructor: good old solid black.
73  constexpr TColor() = default;
74 
75  /// Initialize a TColor with red, green, blue and alpha component.
76  constexpr TColor(float r, float g, float b, float alpha): fRedOrPalettePos(r), fGreen(g), fBlue(b), fAlpha(alpha) {}
77 
78  /// Initialize a TColor with red, green, blue and alpha component.
79  constexpr TColor(float r, float g, float b, Alpha alpha = kOpaque): TColor(r, g, b, alpha.fVal) {}
80 
81  /// Initialize a TColor with red, green, blue and alpha component.
82  constexpr TColor(const PredefinedRGB &predef): TColor(predef[0], predef[1], predef[2]) {}
83 
84  /// Initialize a `TColor` with a `TPalette` ordinal. The actual color is determined from the pad's
85  /// (or rather its `TFrame`'s) `TPalette`
86  constexpr TColor(float paletteOrdinal): fRedOrPalettePos(paletteOrdinal), fKind(EKind::kPalettePos) {}
87 
88  /**\class AutoTag
89  Used to signal that this color shall be automatically chosen by the drawing routines, by picking a color
90  from the `TPad`'s (or rather its `TFrame`'s) current `TPalette`.
91  */
92  class AutoTag {};
93 
94  /// Constructs an automatically assigned color. Call as `TColor col(TColor::kAuto)`.
95  constexpr TColor(AutoTag): fKind(EKind::kAuto) {}
96 
97  /// Determine whether this TColor is storing RGBA (in contrast to an ordinal of a TPalette).
98  bool IsRGBA() const { return fKind == EKind::kRGBA; }
99 
100  /// Determine whether this `TColor` is storing an ordinal of a TPalette (in contrast to RGBA).
101  bool IsPaletteOrdinal() const { return fKind == EKind::kPalettePos; }
102 
103  /// Determine whether this `TColor` will be assigned a actual color upon drawing.
104  bool IsAuto() const { return fKind == EKind::kAuto; }
105 
106  /// If this is an ordinal in a palette, resolve the
107  float GetPaletteOrdinal() const;
108 
109  friend bool operator==(const TColor &lhs, const TColor &rhs)
110  {
111  if (lhs.fKind != rhs.fKind)
112  return false;
113  switch (lhs.fKind) {
114  case EKind::kPalettePos:
115  return lhs.fRedOrPalettePos == rhs.fRedOrPalettePos;
116  case EKind::kRGBA:
117  return lhs.fRedOrPalettePos == rhs.fRedOrPalettePos && lhs.fGreen == rhs.fGreen && lhs.fBlue == rhs.fBlue &&
118  lhs.fAlpha == rhs.fAlpha;
119  case EKind::kAuto:
120  return true; // is that what we need?
121  }
122  return false;
123  }
124 
125  /// For RGBA or auto colors, get the red component (0..1).
126  float GetRed() const {
127  if (AssertNotPalettePos())
128  return fRedOrPalettePos;
129  return 0.;
130  }
131 
132  /// For RGBA or auto colors, get the green component (0..1).
133  float GetGreen() const {
134  if (AssertNotPalettePos())
135  return fGreen;
136  return 0.;
137  }
138 
139  /// For RGBA or auto colors, get the blue component (0..1).
140  float GetBlue() const {
141  if (AssertNotPalettePos())
142  return fBlue;
143  return 0.;
144  }
145 
146  /// For RGBA or auto colors, get the alpha component (0..1).
147  float GetAlpha() const {
148  if (AssertNotPalettePos())
149  return fAlpha;
150  return 0.;
151  }
152 
153  /// For RGBA or auto colors, set the red component.
154  void SetRed(float r) {
155  if (AssertNotPalettePos())
156  fRedOrPalettePos = r;
157  }
158 
159  /// For RGBA or auto colors, set the green component.
160  void SetGreen(float g) {
161  if (AssertNotPalettePos())
162  fGreen = g;
163  }
164 
165  /// For RGBA or auto colors, set the blue component.
166  void SetBlue(float b) {
167  if (AssertNotPalettePos())
168  fBlue = b;
169  }
170 
171  /// For RGBA or auto colors, set the alpha component.
172  void SetAlpha(float a) {
173  if (AssertNotPalettePos())
174  fAlpha = a;
175  }
176 
177  /// For RGBA or auto colors, set the alpha component.
178  void SetAlpha(Alpha a) {
179  if (AssertNotPalettePos())
180  fAlpha = (float)a;
181  }
182 
183  ///\{
184  ///\name Default colors
185  static constexpr PredefinedRGB kRed{{1., 0., 0.}};
186  static constexpr PredefinedRGB kGreen{{0., 1., 0.}};
187  static constexpr PredefinedRGB kBlue{{0., 0, 1.}};
188  static constexpr PredefinedRGB kWhite{{1., 1, 1.}};
189  static constexpr PredefinedRGB kBlack{{0., 0., 0.}};
190  static constexpr AutoTag kAuto{};
191  ///\}
192 };
193 
194 #if 0
195 // TODO: see also imagemagick's C++ interface for TColor operations!
196 // https://www.imagemagick.org/api/magick++-classes.php
197 
198 /// User-defined literal to add alpha to a `TColor`
199 ///
200 /// Use as
201 /// ```
202 /// using namespace ROOT::Experimental;
203 /// TColor red = TColor::kRed + 0.2_alpha;
204 /// ```
205 inline TPadCoord::Normal operator"" _alpha(long double val)
206 {
207  return TPadCoord::Normal{(double)val};
208 }
209 #endif
210 
211 } // namespace Experimental
212 } // namespace ROOT
213 
214 #endif
The color will be set upon drawing the canvas choosing a TPalatte color, see TColor(Auto_t) ...
constexpr TColor(float paletteOrdinal)
Initialize a TColor with a TPalette ordinal.
Definition: TColor.hxx:86
float GetGreen() const
For RGBA or auto colors, get the green component (0..1).
Definition: TColor.hxx:133
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
static constexpr PredefinedRGB kGreen
Definition: TColor.hxx:186
float GetAlpha() const
For RGBA or auto colors, get the alpha component (0..1).
Definition: TColor.hxx:147
constexpr TColor(float r, float g, float b, float alpha)
Initialize a TColor with red, green, blue and alpha component.
Definition: TColor.hxx:76
static constexpr Alpha kOpaque
An opaque color.
Definition: TColor.hxx:37
void SetRed(float r)
For RGBA or auto colors, set the red component.
Definition: TColor.hxx:154
constexpr TColor(AutoTag)
Constructs an automatically assigned color. Call as TColor col(TColor::kAuto).
Definition: TColor.hxx:95
float GetBlue() const
For RGBA or auto colors, get the blue component (0..1).
Definition: TColor.hxx:140
void SetAlpha(Alpha a)
For RGBA or auto colors, set the alpha component.
Definition: TColor.hxx:178
static constexpr Alpha kTransparent
A completely transparent color.
Definition: TColor.hxx:39
std::array< float, 3 > PredefinedRGB
Definition: TColor.hxx:70
bool IsRGBA() const
Determine whether this TColor is storing RGBA (in contrast to an ordinal of a TPalette).
Definition: TColor.hxx:98
float GetRed() const
For RGBA or auto colors, get the red component (0..1).
Definition: TColor.hxx:126
constexpr TColor(const PredefinedRGB &predef)
Initialize a TColor with red, green, blue and alpha component.
Definition: TColor.hxx:82
float fRedOrPalettePos
The "R" in RGBA (0 <= R <= 1), or the palette pos if fKind is kPalettePos.
Definition: TColor.hxx:50
bool IsAuto() const
Determine whether this TColor will be assigned a actual color upon drawing.
Definition: TColor.hxx:104
Used to signal that this color shall be automatically chosen by the drawing routines, by picking a color from the TPad&#39;s (or rather its TFrame&#39;s) current TPalette.
Definition: TColor.hxx:92
constexpr TColor(float r, float g, float b, Alpha alpha=kOpaque)
Initialize a TColor with red, green, blue and alpha component.
Definition: TColor.hxx:79
The color is defined as a value in the TFrame&#39;s TPalette.
bool AssertNotPalettePos() const
throw an exception if the color isn&#39;t specified as kRGBA or kAuto, the two cases where asking for RBG...
Definition: TColor.cxx:40
static constexpr PredefinedRGB kBlack
Definition: TColor.hxx:189
static constexpr AutoTag kAuto
Definition: TColor.hxx:190
float fBlue
The "B" in RGBA (0 <= B <= 1). Unused if fKind != kRGBA.
Definition: TColor.hxx:56
static constexpr PredefinedRGB kBlue
Definition: TColor.hxx:187
ROOT::R::TRInterface & r
Definition: Object.C:4
static constexpr PredefinedRGB kRed
Definition: TColor.hxx:185
auto * a
Definition: textangle.C:12
constexpr TColor()=default
float fGreen
The "G" in RGBA (0 <= G <= 1). Unused if fKind != kRGBA.
Definition: TColor.hxx:53
float GetPaletteOrdinal() const
If this is an ordinal in a palette, resolve the.
Definition: TColor.cxx:33
friend bool operator==(const TColor &lhs, const TColor &rhs)
Definition: TColor.hxx:109
The color is defined as specific RGBA values.
static constexpr PredefinedRGB kWhite
Definition: TColor.hxx:188
void SetGreen(float g)
For RGBA or auto colors, set the green component.
Definition: TColor.hxx:160
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
A color: Red|Green|Blue|Alpha, or a position in a TPalette.
Definition: TColor.hxx:27
A normalized coordinate: 0 in the left, bottom corner, 1 in the top, right corner of the TPad...
Definition: TPadCoord.hxx:72
EKind fKind
How the color is defined.
Definition: TColor.hxx:63
float fAlpha
The "A" in RGBA (0 <= A <= 1).
Definition: TColor.hxx:60
void SetBlue(float b)
For RGBA or auto colors, set the blue component.
Definition: TColor.hxx:166
bool IsPaletteOrdinal() const
Determine whether this TColor is storing an ordinal of a TPalette (in contrast to RGBA)...
Definition: TColor.hxx:101
void SetAlpha(float a)
For RGBA or auto colors, set the alpha component.
Definition: TColor.hxx:172
static constexpr double g