Euphoria
palette.cc
Go to the documentation of this file.
1 #include "core/palette.h"
2 
3 #include "assert/assert.h"
4 #include "base/numeric.h"
5 #include "base/random.h"
6 #include "base/colors.h"
7 
8 
9 namespace eu::core
10 {
11  namespace
12  {
13  float
14  get_distance_squared(const Rgbi& lhs, const Rgbi& rhs)
15  {
16  return
17  square(static_cast<float>(lhs.r - rhs.r)) +
18  square(static_cast<float>(lhs.g - rhs.g)) +
19  square(static_cast<float>(lhs.b - rhs.b))
20  ;
21  }
22  }
23 
24  const Rgbi&
26  {
28  }
29 
30 
31  const Rgbi&
32  Palette::get_safe_index(unsigned int index) const
33  {
34  return colors[index % colors.size()];
35  }
36 
37 
38  const Rgbi&
40  {
41  return colors[get_index_closest(c)];
42  }
43 
44 
45  int
47  {
48  ASSERT(!colors.empty());
49  auto diff_best = get_distance_squared(c, colors[0]);
50 
51  int index_best = 0;
52  const int size = c_sizet_to_int(colors.size());
53  for(int index = 1; index < size; index += 1)
54  {
55  const auto diff = get_distance_squared(c, colors[index]);
56  if(diff < diff_best)
57  {
58  diff_best = diff;
59  index_best = index;
60  }
61  }
62 
63  return index_best;
64  }
65 
67 
68  Palette
70  {
71  return {name, colors};
72  }
73 
74 
75  DynamicPalette::DynamicPalette(const std::string& n, const std::vector<Rgbi>& c)
76  : name(n)
77  , colors(c)
78  {
79  }
80 
81 
83  DynamicPalette::create_empty(const std::string& name)
84  {
85  return DynamicPalette{name};
86  }
87 
88 
90  DynamicPalette::create_rainbow(int count, float saturation, float lightness)
91  {
92  return create_rainbow
93  (
94  count,
97  (
98  max(1.0f, static_cast<float>(count - 1) / static_cast<float>(count))
99  ),
100  saturation,
101  lightness
102  );
103  }
104 
105 
108  (
109  int count,
110  const Angle& from,
111  const Angle& to,
112  float saturation,
113  float lightness
114  )
115  {
116  ASSERT(count > 1);
117 
118  auto pal = DynamicPalette::create_empty("Rainbow");
119 
120  for(int color_index = 0; color_index < count; color_index += 1)
121  {
122  float d = static_cast<float>(color_index) / static_cast<float>(count - 1);
123  const auto rgbf = to_rgb
124  (
125  Hsl
126  {
127  lerp_angle(from, d, to),
128  saturation,
129  lightness
130  }
131  );
132  pal.colors.push_back(to_rgbi(rgbf));
133  }
134 
135  return pal;
136  }
137 
138 
139  DynamicPalette::DynamicPalette(const std::string& n)
140  : name(n)
141  {
142  }
143 
144 }
145 
#define ASSERT(x)
Definition: assert.h:29
int c_sizet_to_int(size_t t)
Definition: cint.cc:11
Rgbi to_rgbi(const Rgb &c)
Definition: rgb.cc:352
Rgb to_rgb(const Rgbi &c)
Definition: rgb.cc:229
float square(float r)
Definition: numeric.cc:94
Angle lerp_angle(const Angle &from, float v, const Angle &to)
Definition: angle.cc:194
const T & get_random_item_in_vector(Random *r, const std::vector< T > &v)
Definition: random.h:68
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
constexpr static Angle from_radians(float radians)
Definition: angle.h:22
constexpr float from_percent_of_360() const
Definition: angle.h:56
Definition: rgb.h:104
WEL512 Random Number Generator.
Definition: random.h:21
Definition: rgb.h:26
static DynamicPalette create_rainbow(int count, float saturation=0.5f, float lightness=0.5f)
based on the r documentation https://rdrr.io/r/grDevices/palettes.html
Definition: palette.cc:90
DynamicPalette(const std::string &n, const Rgbi &c0, const R &... c)
Definition: palette.h:108
Palette to_palette() const
Definition: palette.cc:69
std::vector< Rgbi > colors
list of the colors
Definition: palette.h:105
static DynamicPalette create_empty(const std::string &name)
Create a empty palette with a name.
Definition: palette.cc:83
const Rgbi & get_random_color(Random *r) const
Get a random color.
Definition: palette.cc:25
ranges::span< const Rgbi > colors
Definition: palette.h:25
const Rgbi & get_safe_index(unsigned int i) const
Get a color based on the index.
Definition: palette.cc:32
int get_index_closest(const Rgbi &c) const
Definition: palette.cc:46
const Rgbi & get_closest_color(const Rgbi &c) const
Definition: palette.cc:39