Euphoria
palette.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <string_view>
5 #include <array>
6 #include "range/v3/view/span.hpp"
7 
8 #include "base/rgb.h"
9 
10 
11 namespace eu
12 {
13  struct Random;
14 }
15 
16 namespace eu::core
17 {
18  struct Palette
19  {
20  // todo(Gustav): what is name used for? can we remove it and use a tostring on the palette_all enum?
21  // also does this represent a display name or a lookup name? both?
23  std::string_view name;
24 
25  ranges::span<const Rgbi> colors;
26 
27  constexpr Palette(const std::string_view& n, const ranges::span<const Rgbi>& c)
28  : name(n)
29  , colors(c)
30  {
31  }
32 
37  const Rgbi&
38  get_random_color(Random* r) const;
39 
45  [[nodiscard]] const Rgbi&
46  get_safe_index(unsigned int i) const;
47 
48  [[nodiscard]] const Rgbi&
49  get_closest_color(const Rgbi& c) const;
50 
51  [[nodiscard]] int
52  get_index_closest(const Rgbi& c) const;
53 
54  // todo(Gustav): provide a function that takes a (void) lambda
55  // returing a index, and we return a (safe) color from that index
56  // with this construct we can simplyfy switch() statements
57  // that needs to determine a color
58  };
59 
60 
61  template<std::size_t size>
63  {
64  std::string_view name;
65  std::array<const Rgbi, size> colors;
67 
68  constexpr StaticPalette
69  (
70  std::string_view n,
71  const std::array<const Rgbi, size>& c
72  )
73  : name(n), colors(c), pal{n, colors}
74  {
75  }
76 
77  constexpr const Palette& operator*() const
78  {
79  return pal;
80  }
81 
82  constexpr const Palette* operator->() const
83  {
84  return &pal;
85  }
86  };
87 
88 
89  template <typename... T>
90  constexpr StaticPalette<sizeof...(T)>
92  (
93  const std::string_view& name,
94  T... colors
95  )
96  {
97  return {name, {colors...}};
98  }
99 
101  {
102  std::string name;
103 
105  std::vector<Rgbi> colors;
106 
107  template <typename... R>
108  DynamicPalette(const std::string& n, const Rgbi& c0, const R&... c)
109  : name(n)
110  , colors {c0, c...}
111  {
112  }
113 
114  DynamicPalette(const std::string& n, const std::vector<Rgbi>& c);
115 
120  [[nodiscard]] static DynamicPalette
121  create_empty(const std::string& name);
122 
124  [[nodiscard]] static DynamicPalette
125  create_rainbow(int count, float saturation = 0.5f, float lightness = 0.5f);
126 
128  [[nodiscard]] static DynamicPalette create_rainbow
129  (
130  int count,
131  const Angle& from,
132  const Angle& to,
133  float saturation,
134  float lightness
135  );
136 
137  [[nodiscard]] Palette to_palette() const;
138 
139  private:
140  explicit DynamicPalette(const std::string& n);
141  };
142 
143 
144  // http://pixeljoint.com/forum/forum_posts.asp?TID=12795
146  (
147  "Dawnbringer",
148  Rgbi::from_hex(0x140C1C),
149  Rgbi::from_hex(0x442434),
150  Rgbi::from_hex(0x30346D),
151  Rgbi::from_hex(0x4E4A4E),
152  Rgbi::from_hex(0x854C30),
153  Rgbi::from_hex(0x346524),
154  Rgbi::from_hex(0xD04648),
155  Rgbi::from_hex(0x757161),
156  Rgbi::from_hex(0x597DCE),
157  Rgbi::from_hex(0xD27D2C),
158  Rgbi::from_hex(0x8595A1),
159  Rgbi::from_hex(0x6DAA2C),
160  Rgbi::from_hex(0xD2AA99),
161  Rgbi::from_hex(0x6DC2CA),
162  Rgbi::from_hex(0xDAD45E),
163  Rgbi::from_hex(0xDEEED6)
164  );
165 
167  (
168  "Named colors",
171  Rgbi{ NamedColor::gray },
172  Rgbi{ NamedColor::dark_gray },
173  Rgbi{ NamedColor::black },
174  Rgbi{ NamedColor::red },
175  Rgbi{ NamedColor::pure_red },
176  Rgbi{ NamedColor::blue },
177  Rgbi{ NamedColor::pure_blue },
178  Rgbi{ NamedColor::light_blue },
179  Rgbi{ NamedColor::normal_blue },
181  Rgbi{ NamedColor::green },
182  Rgbi{ NamedColor::pure_green },
183  Rgbi{ NamedColor::light_green },
184  Rgbi{ NamedColor::yellow },
185  Rgbi{ NamedColor::pure_yellow },
186  Rgbi{ NamedColor::orange },
187  Rgbi{ NamedColor::pure_orange },
188  Rgbi{ NamedColor::brown },
189  Rgbi{ NamedColor::pure_brown },
190  Rgbi{ NamedColor::purple },
191  Rgbi{ NamedColor::pure_purple },
192  Rgbi{ NamedColor::pink },
193  Rgbi{ NamedColor::pure_pink },
194  Rgbi{ NamedColor::pure_beige },
195  Rgbi{ NamedColor::tan },
196  Rgbi{ NamedColor::pure_tan },
197  Rgbi{ NamedColor::cyan },
198  Rgbi{ NamedColor::pure_cyan }
199  );
200 
201 }
constexpr StaticPalette< sizeof...(T)> make_static_palette(const std::string_view &name, T... colors)
Definition: palette.h:92
constexpr auto dawnbringer_palette
Definition: palette.h:145
constexpr auto named_colors_palette
Definition: palette.h:166
constexpr float c0
Definition: synth.h:80
Definition: assert.h:90
WEL512 Random Number Generator.
Definition: random.h:21
Definition: rgb.h:26
constexpr static Rgbi from_hex(unsigned int hex)
Definition: rgb.h:355
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
std::string_view name
the name of the palette
Definition: palette.h:23
int get_index_closest(const Rgbi &c) const
Definition: palette.cc:46
constexpr Palette(const std::string_view &n, const ranges::span< const Rgbi > &c)
Definition: palette.h:27
const Rgbi & get_closest_color(const Rgbi &c) const
Definition: palette.cc:39
constexpr const Palette & operator*() const
Definition: palette.h:77
constexpr const Palette * operator->() const
Definition: palette.h:82
std::array< const Rgbi, size > colors
Definition: palette.h:65
std::string_view name
Definition: palette.h:64
constexpr StaticPalette(std::string_view n, const std::array< const Rgbi, size > &c)
Definition: palette.h:69