Logo ROOT   6.12/07
Reference Guide
TPalette.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_TPalette
17 #define ROOT7_TPalette
18 
19 #include <RStringView.h>
20 #include <ROOT/TColor.hxx>
21 
22 #include <utility>
23 #include <vector>
24 
25 namespace ROOT {
26 namespace Experimental {
27 
28 /** \class ROOT::Experimental::TPalette
29  A set of colors. `TColor`s can be conveniently generated from this.
30 
31  A palette associates a color with an ordinal number: for a normalized palette,
32  this number ranges from 0..1. For user-valued palettes, the palette yields a color for
33  user-coordinates (for instance histogram content), in an arbitrary range.
34 
35  A palette can be a smooth gradients by interpolation of support points, or a set of
36  discrete colors.
37  */
38 class TPalette {
39 public:
40  /** \class ROOT::Experimental::TPalette::OrdinalAndColor
41  An ordinal value and its associated color.
42  */
43  struct OrdinalAndColor {
44  double fOrdinal; ///< The value associated with the color.
45  TColor fColor; ///< The color associated with the value.
46 
47  /// Compare two `OrdinalAndColor`s, for sorting.
48  friend bool operator<(const OrdinalAndColor &lhs, const OrdinalAndColor &rhs)
49  {
50  return lhs.fOrdinal < rhs.fOrdinal;
51  }
52  /// Compare an `OrdinalAndColor` and an ordinal value.
53  friend bool operator<(const OrdinalAndColor &lhs, double rhs) { return lhs.fOrdinal < rhs; }
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  TPalette(bool interpolate, bool knownNormalized, const std::vector<OrdinalAndColor> &points);
67  TPalette(bool interpolate, const std::vector<TColor> &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: `TPalette palette(TPalette::kDiscrete, {{-100., TColor::kWhite}, {100., TColor::kRed}})`
76  static constexpr const Discrete_t kDiscrete{};
77 
78  TPalette() = default;
79 
80  /// Construct a TPalette 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  TPalette(const std::vector<OrdinalAndColor> &interpPoints): TPalette(true, false, interpPoints) {}
86 
87  /// Construct a TPalette 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  TPalette(Discrete_t, const std::vector<OrdinalAndColor> &points): TPalette(false, false, points) {}
92 
93  /// Construct a normalized TPalette 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  TPalette(const std::vector<TColor> &interpPoints): TPalette(true, interpPoints) {}
98 
99  /// Construct a normalized TPalette 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  TPalette(Discrete_t, const std::vector<TColor> &points): TPalette(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  TColor GetColor(double ordinal);
116 
117  /// Given a TColor (that might either be a RGBA or a TPalette ordinal), get the RGBA-based color.
119  {
120  if (col.IsRGBA())
121  return col;
122  return GetColor(col.GetPaletteOrdinal());
123  }
124 
125  ///\{
126  ///\name Global Palettes
127 
128  /// Register a palette in the set of global palettes, making it available to `GetPalette()`.
129  /// This function is not thread safe; any concurrent call to global Palette manipulation must be synchronized!
130  static void RegisterPalette(std::string_view name, const TPalette &palette);
131 
132  /// Get a global palette by name. Returns an empty palette if no palette with that name is known.
133  /// This function is not thread safe; any concurrent call to global Palette manipulation must be synchronized!
134  static const TPalette &GetPalette(std::string_view name);
135 
136  ///\}
137 };
138 
139 } // namespace Experimental
140 } // namespace ROOT
141 
142 #endif
TColor fColor
The color associated with the value.
Definition: TPalette.hxx:45
bool IsDiscrete() const
Whether the palette is discrete, i.e. does no interpolation between colors.
Definition: TPalette.hxx:108
static constexpr const Discrete_t kDiscrete
Tag value used to signal that the palette&#39;s colors should not be interpolated.
Definition: TPalette.hxx:76
double fOrdinal
The value associated with the color.
Definition: TPalette.hxx:44
bool fInterpolate
Whether to interpolate between the colors (in contrast to picking one of fColors).
Definition: TPalette.hxx:61
basic_string_view< char > string_view
Definition: RStringView.h:35
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
std::vector< OrdinalAndColor > fColors
Palette colors: the color points and their ordinal value.
Definition: TPalette.hxx:58
bool IsRGBA() const
Determine whether this TColor is storing RGBA (in contrast to an ordinal of a TPalette).
Definition: TColor.hxx:98
bool fNormalized
Whether the palette&#39;s ordinal numbers are normalized.
Definition: TPalette.hxx:64
friend bool operator<(const OrdinalAndColor &lhs, const OrdinalAndColor &rhs)
Compare two OrdinalAndColors, for sorting.
Definition: TPalette.hxx:48
Tag type used to signal that the palette&#39;s colors should not be interpolated.
Definition: TPalette.hxx:71
TColor ResolveRGBAColor(const TColor &col)
Given a TColor (that might either be a RGBA or a TPalette ordinal), get the RGBA-based color...
Definition: TPalette.hxx:118
TPalette(Discrete_t, const std::vector< TColor > &points)
Construct a normalized TPalette from a vector of colors.
Definition: TPalette.hxx:102
bool IsNormalized() const
Whether the palette is normalized, i.e. covers colors in the ordinal range 0..1.
Definition: TPalette.hxx:105
An ordinal value and its associated color.
Definition: TPalette.hxx:43
point * points
Definition: X3DBuffer.c:20
TColor GetColor(double ordinal)
Get the color associated with the ordinal value.
Definition: TPalette.cxx:62
TPalette(Discrete_t, const std::vector< OrdinalAndColor > &points)
Construct a TPalette from a vector of (ordinal|color) pairs.
Definition: TPalette.hxx:91
static const TPalette & GetPalette(std::string_view name)
Get a global palette by name.
Definition: TPalette.cxx:100
static void RegisterPalette(std::string_view name, const TPalette &palette)
Register a palette in the set of global palettes, making it available to GetPalette().
Definition: TPalette.cxx:95
A set of colors.
Definition: TPalette.hxx:38
friend bool operator<(const OrdinalAndColor &lhs, double rhs)
Compare an OrdinalAndColor and an ordinal value.
Definition: TPalette.hxx:53
float GetPaletteOrdinal() const
If this is an ordinal in a palette, resolve the.
Definition: TColor.cxx:33
TPalette(const std::vector< OrdinalAndColor > &interpPoints)
Construct a TPalette from a vector of (ordinal|color) pairs as interpolation points.
Definition: TPalette.hxx:85
TPalette(const std::vector< TColor > &interpPoints)
Construct a normalized TPalette from a vector of colors as interpolation points.
Definition: TPalette.hxx:97
A color: Red|Green|Blue|Alpha, or a position in a TPalette.
Definition: TColor.hxx:27
bool IsGradient() const
Whether the palette is a smooth gradient generated by interpolating between the color points...
Definition: TPalette.hxx:111
char name[80]
Definition: TGX11.cxx:109