23#include <unordered_map>
27RPalette::RPalette(
bool interpolate,
bool knownNormalized,
const std::vector<RPalette::OrdinalAndColor> &
points)
28 : fColors(
points), fInterpolate(interpolate), fNormalized(knownNormalized)
31 throw std::runtime_error(
"Must have at least two points to build a palette!");
35 if (!knownNormalized) {
37 double high =
fColors.back().fOrdinal;
38 double low =
fColors.front().fOrdinal;
39 double prec = (high - low) * 1
E-6;
41 auto reasonablyEqual = [&](
double val,
double expected) ->
bool {
return std::fabs(val - expected) < prec; };
42 fNormalized = reasonablyEqual(low, 0.) && reasonablyEqual(high, 1.);
47static std::vector<RPalette::OrdinalAndColor> AddOrdinals(
const std::vector<RColor> &
points)
49 std::vector<RPalette::OrdinalAndColor> ret(
points.size());
51 return {1. / (
points.size() - 1) * (&col -
points.data()), col};
53 std::transform(
points.begin(),
points.end(), ret.begin(), addOneOrdinal);
67 auto iColor = std::lower_bound(
fColors.begin(),
fColors.end(), ordinal);
72 && (iColor - 1)->fOrdinal - ordinal < ordinal - iColor->fOrdinal)
73 return (iColor - 1)->fColor;
74 return iColor->fColor;
80using GlobalPalettes_t = std::unordered_map<std::string, RPalette>;
81static GlobalPalettes_t CreateDefaultPalettes()
89static GlobalPalettes_t &GetGlobalPalettes()
91 static GlobalPalettes_t globalPalettes = CreateDefaultPalettes();
92 return globalPalettes;
98 GetGlobalPalettes()[std::string(
name)] = palette;
103 static const RPalette sNoPaletteWithThatName;
104 auto iGlobalPalette = GetGlobalPalettes().find(std::string(
name));
105 if (iGlobalPalette == GetGlobalPalettes().end())
106 return sNoPaletteWithThatName;
107 return iGlobalPalette->second;
#define R__ERROR_HERE(GROUP)
A color: Red|Green|Blue|Alpha, or a position in a RPalette.
static constexpr RGBA kWhite
static constexpr RGBA kBlue
static constexpr RGBA kBlack
static constexpr RGBA kRed
static void RegisterPalette(std::string_view name, const RPalette &palette)
Register a palette in the set of global palettes, making it available to GetPalette().
RColor GetColor(double ordinal)
Get the color associated with the ordinal value.
std::vector< OrdinalAndColor > fColors
Palette colors: the color points and their ordinal value.
bool fInterpolate
Whether to interpolate between the colors (in contrast to picking one of fColors).
bool fNormalized
Whether the palette's ordinal numbers are normalized.
static const RPalette & GetPalette(std::string_view name)
Get a global palette by name.
basic_string_view< char > string_view
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
constexpr Double_t E()
Base of natural log:
An ordinal value and its associated color.