Logo ROOT  
Reference Guide
RPalette.hxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2017, 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_RPalette
10 #define ROOT7_RPalette
11 
12 #include <ROOT/RStringView.hxx>
13 #include <ROOT/RColor.hxx>
14 
15 #include <utility>
16 #include <vector>
17 
18 namespace ROOT {
19 namespace Experimental {
20 
21 /** \class RPalette
22 \ingroup GpadROOT7
23 \brief A set of colors. `RColor`s can be conveniently generated from this.
24 
25  A palette associates a color with an ordinal number: for a normalized palette,
26  this number ranges from 0..1. For user-valued palettes, the palette yields a color for
27  user-coordinates (for instance histogram content), in an arbitrary range.
28 
29  A palette can be a smooth gradients by interpolation of support points, or a set of
30  discrete colors.
31 
32 \author Axel Naumann <axel@cern.ch>
33 \date 2017-09-26
34 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
35 
36 */
37 
38 class RPalette {
39 public:
40  /// An ordinal value and its associated color.
41  struct OrdinalAndColor {
42  double fOrdinal{0.}; ///< The value associated with the color.
43  RColor fColor; ///< The color associated with the value.
44 
45  /// Compare two `OrdinalAndColor`s, for sorting.
46  friend bool operator<(const OrdinalAndColor &lhs, const OrdinalAndColor &rhs)
47  {
48  return lhs.fOrdinal < rhs.fOrdinal;
49  }
50 
51  /// Compare an `OrdinalAndColor` and an ordinal value.
52  friend bool operator<(const OrdinalAndColor &lhs, double rhs) { return lhs.fOrdinal < rhs; }
53 
54  };
55 
56 private:
57  /// Palette colors: the color points and their ordinal value.
58  std::vector<OrdinalAndColor> fColors;
59 
60  /// Whether to interpolate between the colors (in contrast to picking one of fColors).
61  bool fInterpolate = true;
62 
63  /// Whether the palette's ordinal numbers are normalized.
64  bool fNormalized = true;
65 
66  RPalette(bool interpolate, bool knownNormalized, const std::vector<OrdinalAndColor> &points);
67  RPalette(bool interpolate, const std::vector<RColor> &points);
68 
69 public:
70  /// Tag type used to signal that the palette's colors should not be interpolated.
71  struct Discrete_t {
72  };
73 
74  /// Tag value used to signal that the palette's colors should not be interpolated. Can be passed to the
75  /// constructor: `RPalette palette(RPalette::kDiscrete, {{-100., RColor::kWhite}, {100., RColor::kRed}})`
76  static constexpr const Discrete_t kDiscrete{};
77 
78  RPalette() = default;
79 
80  /// Construct a RPalette from a vector of (ordinal|color) pairs as interpolation points.
81  /// Palette colors will be these points for the ordinal, and interpolated in between the
82  /// ordinal points. The points will be sorted.
83  /// The palette is normalized if the lowest ordinal is 0. and the highest ordinal is 1.;
84  /// otherwise, the palette is a user-valued palette.
85  RPalette(const std::vector<OrdinalAndColor> &interpPoints): RPalette(true, false, interpPoints) {}
86 
87  /// Construct a RPalette from a vector of (ordinal|color) pairs. For a given value, the palette returns
88  /// the color with an ordinal that is closest to the value. The points will be sorted.
89  /// The palette is normalized if the lowest ordinal is 0. and the highest ordinal is 1.;
90  /// otherwise, the palette is a user-valued palette.
91  RPalette(Discrete_t, const std::vector<OrdinalAndColor> &points): RPalette(false, false, points) {}
92 
93  /// Construct a normalized RPalette from a vector of colors as interpolation points. The ordinal associated
94  /// with each color is equidistant from 0..1, i.e. for three colors it will be 0., 0.5 and 1, respectively.
95  /// Palette colors will be these points for the ordinal associated with the color,
96  /// and interpolated in between the ordinal points.
97  RPalette(const std::vector<RColor> &interpPoints): RPalette(true, interpPoints) {}
98 
99  /// Construct a normalized RPalette from a vector of colors. The ordinal associated
100  /// with each color is equidistant from 0..1, i.e. for three colors it will be 0., 0.5 and 1, respectively.
101  /// For a given value, the palette returns the color with an ordinal that is closest to the value.
102  RPalette(Discrete_t, const std::vector<RColor> &points): RPalette(false, points) {}
103 
104  /// Whether the palette is normalized, i.e. covers colors in the ordinal range 0..1.
105  bool IsNormalized() const { return fNormalized; }
106 
107  /// Whether the palette is discrete, i.e. does no interpolation between colors.
108  bool IsDiscrete() const { return !fInterpolate; }
109 
110  /// Whether the palette is a smooth gradient generated by interpolating between the color points.
111  bool IsGradient() const { return fInterpolate; }
112 
113  /// Get the color associated with the ordinal value. The value is expected to be 0..1 for a normalized
114  /// palette.
115  RColor GetColor(double ordinal);
116 
117  ///\{
118  ///\name Global Palettes
119 
120  /// Register a palette in the set of global palettes, making it available to `GetPalette()`.
121  /// This function is not thread safe; any concurrent call to global Palette manipulation must be synchronized!
122  static void RegisterPalette(std::string_view name, const RPalette &palette);
123 
124  /// Get a global palette by name. Returns an empty palette if no palette with that name is known.
125  /// This function is not thread safe; any concurrent call to global Palette manipulation must be synchronized!
126  static const RPalette &GetPalette(std::string_view name = "");
127 
128  ///\}
129 };
130 
131 } // namespace Experimental
132 } // namespace ROOT
133 
134 #endif
ROOT::Experimental::RPalette::GetColor
RColor GetColor(double ordinal)
Get the color associated with the ordinal value.
Definition: RPalette.cxx:55
ROOT::Experimental::RPalette::GetPalette
static const RPalette & GetPalette(std::string_view name="")
Get a global palette by name.
Definition: RPalette.cxx:142
ROOT::Experimental::RPalette::RPalette
RPalette(const std::vector< RColor > &interpPoints)
Construct a normalized RPalette from a vector of colors as interpolation points.
Definition: RPalette.hxx:97
ROOT::Experimental::RPalette::Discrete_t
Tag type used to signal that the palette's colors should not be interpolated.
Definition: RPalette.hxx:71
ROOT::Experimental::RPalette::RPalette
RPalette(const std::vector< OrdinalAndColor > &interpPoints)
Construct a RPalette from a vector of (ordinal|color) pairs as interpolation points.
Definition: RPalette.hxx:85
ROOT::Experimental::RPalette::IsGradient
bool IsGradient() const
Whether the palette is a smooth gradient generated by interpolating between the color points.
Definition: RPalette.hxx:111
ROOT::Experimental::RPalette::OrdinalAndColor::operator<
friend bool operator<(const OrdinalAndColor &lhs, const OrdinalAndColor &rhs)
Compare two OrdinalAndColors, for sorting.
Definition: RPalette.hxx:46
ROOT::Experimental::RPalette::fInterpolate
bool fInterpolate
Whether to interpolate between the colors (in contrast to picking one of fColors).
Definition: RPalette.hxx:61
ROOT::Experimental::RPalette::fNormalized
bool fNormalized
Whether the palette's ordinal numbers are normalized.
Definition: RPalette.hxx:64
string_view
basic_string_view< char > string_view
Definition: libcpp_string_view.h:785
ROOT::Experimental::RPalette::kDiscrete
static constexpr const Discrete_t kDiscrete
Tag value used to signal that the palette's colors should not be interpolated.
Definition: RPalette.hxx:76
ROOT::Experimental::RColor
The color class.
Definition: RColor.hxx:34
ROOT::Experimental::RPalette::OrdinalAndColor::fColor
RColor fColor
The color associated with the value.
Definition: RPalette.hxx:43
ROOT::Experimental::RPalette::IsDiscrete
bool IsDiscrete() const
Whether the palette is discrete, i.e. does no interpolation between colors.
Definition: RPalette.hxx:108
RColor.hxx
ROOT::Experimental::RPalette::fColors
std::vector< OrdinalAndColor > fColors
Palette colors: the color points and their ordinal value.
Definition: RPalette.hxx:58
ROOT::Experimental::RPalette::RPalette
RPalette(Discrete_t, const std::vector< RColor > &points)
Construct a normalized RPalette from a vector of colors.
Definition: RPalette.hxx:102
RStringView.hxx
ROOT::Experimental::RPalette::RegisterPalette
static void RegisterPalette(std::string_view name, const RPalette &palette)
Register a palette in the set of global palettes, making it available to GetPalette().
Definition: RPalette.cxx:137
ROOT::Experimental::RPalette::RPalette
RPalette()=default
ROOT::Experimental::RPalette::OrdinalAndColor::operator<
friend bool operator<(const OrdinalAndColor &lhs, double rhs)
Compare an OrdinalAndColor and an ordinal value.
Definition: RPalette.hxx:52
points
point * points
Definition: X3DBuffer.c:22
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Experimental::RPalette::OrdinalAndColor::fOrdinal
double fOrdinal
The value associated with the color.
Definition: RPalette.hxx:42
ROOT::Experimental::RPalette
A set of colors.
Definition: RPalette.hxx:38
ROOT::Experimental::RPalette::RPalette
RPalette(Discrete_t, const std::vector< OrdinalAndColor > &points)
Construct a RPalette from a vector of (ordinal|color) pairs.
Definition: RPalette.hxx:91
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Experimental::RPalette::OrdinalAndColor
An ordinal value and its associated color.
Definition: RPalette.hxx:41
ROOT::Experimental::RPalette::IsNormalized
bool IsNormalized() const
Whether the palette is normalized, i.e. covers colors in the ordinal range 0..1.
Definition: RPalette.hxx:105