Logo ROOT   6.18/05
Reference Guide
RPalette.hxx
Go to the documentation of this file.
1/// \file ROOT/RPalette.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_RPalette
17#define ROOT7_RPalette
18
19#include <ROOT/RStringView.hxx>
20#include <ROOT/RColor.hxx>
21
22#include <utility>
23#include <vector>
24
25namespace ROOT {
26namespace Experimental {
27
28/** \class ROOT::Experimental::RPalette
29 A set of colors. `RColor`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 */
38class RPalette {
39public:
40 /** \class ROOT::Experimental::RPalette::OrdinalAndColor
41 An ordinal value and its associated color.
42 */
44 double fOrdinal; ///< The value associated with the color.
45 RColor 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
56private:
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
69public:
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 /// Given a RColor (that might either be a RGBA or a RPalette 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 RPalette &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!
135
136 ///\}
137};
138
139} // namespace Experimental
140} // namespace ROOT
141
142#endif
char name[80]
Definition: TGX11.cxx:109
point * points
Definition: X3DBuffer.c:22
A color: Red|Green|Blue|Alpha, or a position in a RPalette.
Definition: RColor.hxx:28
bool IsRGBA() const
Determine whether this RColor is storing RGBA (in contrast to an ordinal of a RPalette).
Definition: RColor.hxx:99
float GetPaletteOrdinal() const
If this is an ordinal in a palette, resolve the.
Definition: RColor.cxx:42
RColor ResolveRGBAColor(const RColor &col)
Given a RColor (that might either be a RGBA or a RPalette ordinal), get the RGBA-based color.
Definition: RPalette.hxx:118
RPalette(Discrete_t, const std::vector< OrdinalAndColor > &points)
Construct a RPalette from a vector of (ordinal|color) pairs.
Definition: RPalette.hxx:91
RPalette(Discrete_t, const std::vector< RColor > &points)
Construct a normalized RPalette from a vector of colors.
Definition: RPalette.hxx:102
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:96
bool IsDiscrete() const
Whether the palette is discrete, i.e. does no interpolation between colors.
Definition: RPalette.hxx:108
RColor GetColor(double ordinal)
Get the color associated with the ordinal value.
Definition: RPalette.cxx:62
RPalette(const std::vector< RColor > &interpPoints)
Construct a normalized RPalette from a vector of colors as interpolation points.
Definition: RPalette.hxx:97
RPalette(const std::vector< OrdinalAndColor > &interpPoints)
Construct a RPalette from a vector of (ordinal|color) pairs as interpolation points.
Definition: RPalette.hxx:85
std::vector< OrdinalAndColor > fColors
Palette colors: the color points and their ordinal value.
Definition: RPalette.hxx:58
bool IsNormalized() const
Whether the palette is normalized, i.e. covers colors in the ordinal range 0..1.
Definition: RPalette.hxx:105
bool fInterpolate
Whether to interpolate between the colors (in contrast to picking one of fColors).
Definition: RPalette.hxx:61
bool fNormalized
Whether the palette's ordinal numbers are normalized.
Definition: RPalette.hxx:64
static const RPalette & GetPalette(std::string_view name)
Get a global palette by name.
Definition: RPalette.cxx:101
bool IsGradient() const
Whether the palette is a smooth gradient generated by interpolating between the color points.
Definition: RPalette.hxx:111
static constexpr const Discrete_t kDiscrete
Tag value used to signal that the palette's colors should not be interpolated.
Definition: RPalette.hxx:76
basic_string_view< char > string_view
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
Tag type used to signal that the palette's colors should not be interpolated.
Definition: RPalette.hxx:71
An ordinal value and its associated color.
Definition: RPalette.hxx:43
friend bool operator<(const OrdinalAndColor &lhs, double rhs)
Compare an OrdinalAndColor and an ordinal value.
Definition: RPalette.hxx:53
double fOrdinal
The value associated with the color.
Definition: RPalette.hxx:44
RColor fColor
The color associated with the value.
Definition: RPalette.hxx:45
friend bool operator<(const OrdinalAndColor &lhs, const OrdinalAndColor &rhs)
Compare two OrdinalAndColors, for sorting.
Definition: RPalette.hxx:48