23 #include <unordered_map> 28 : fColors(points), fInterpolate(interpolate), fNormalized(knownNormalized)
30 if (points.size() < 2)
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.);
47 static std::vector<TPalette::OrdinalAndColor> AddOrdinals(
const std::vector<TColor> &
points)
49 std::vector<TPalette::OrdinalAndColor> ret(points.size());
51 return {1. / (points.size() - 1) * (&col - points.data()), col};
53 std::transform(points.begin(), points.end(), ret.begin(), addOneOrdinal);
59 :
TPalette(interpolate, true, AddOrdinals(points))
67 auto iColor = std::lower_bound(
fColors.begin(),
fColors.end(), ordinal);
71 if ((iColor - 1)->fOrdinal - ordinal < ordinal - iColor->fOrdinal)
72 return (iColor - 1)->fColor;
73 return iColor->fColor;
79 using GlobalPalettes_t = std::unordered_map<std::string, TPalette>;
80 static GlobalPalettes_t CreateDefaultPalettes()
88 static GlobalPalettes_t &GetGlobalPalettes()
90 static GlobalPalettes_t globalPalettes = CreateDefaultPalettes();
91 return globalPalettes;
97 GetGlobalPalettes()[std::string(name)] = palette;
102 static const TPalette sNoPaletteWithThatName;
103 auto iGlobalPalette = GetGlobalPalettes().find(std::string(name));
104 if (iGlobalPalette == GetGlobalPalettes().end())
105 return sNoPaletteWithThatName;
106 return iGlobalPalette->second;
bool fInterpolate
Whether to interpolate between the colors (in contrast to picking one of fColors).
basic_string_view< char > string_view
std::vector< OrdinalAndColor > fColors
Palette colors: the color points and their ordinal value.
bool fNormalized
Whether the palette's ordinal numbers are normalized.
An ordinal value and its associated color.
static constexpr PredefinedRGB kBlack
TColor GetColor(double ordinal)
Get the color associated with the ordinal value.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
static constexpr PredefinedRGB kBlue
static const TPalette & GetPalette(std::string_view name)
Get a global palette by name.
static constexpr PredefinedRGB kRed
static void RegisterPalette(std::string_view name, const TPalette &palette)
Register a palette in the set of global palettes, making it available to GetPalette().
static constexpr PredefinedRGB kWhite
A color: Red|Green|Blue|Alpha, or a position in a TPalette.
#define R__ERROR_HERE(GROUP)