Empirical
color_map.h
Go to the documentation of this file.
1 
10 #ifndef EMP_COLOR_MAP_H
11 #define EMP_COLOR_MAP_H
12 
13 #include <iomanip>
14 #include <map>
15 #include <string>
16 #include <tuple>
17 
18 #include "../base/vector.h"
19 #include "../tools/string_utils.h"
20 
21 namespace emp {
22 
23  namespace {
24  using dHueMapKey = std::tuple<int, double, double, int, int>;
25  using dHueMap = std::map<dHueMapKey, emp::vector<std::string> >;
26 
27  dHueMap hue_maps;
28  }
29 
31  std::string ColorHSL(double h, double s, double l) {
32  emp_assert(h >= 0 && h <= 360);
33  emp_assert(s >= 0 && s <= 100);
34  emp_assert(l >= 0 && l <= 100);
35  std::stringstream ss;
36  ss << "hsl(" << h << ',' << s << "%," << l << "%)";
37  return ss.str();
38  }
39 
41  std::string ColorRGB(int r, int g, int b) {
42  emp_assert(r >= 0 && r < 255);
43  emp_assert(g >= 0 && g < 255);
44  emp_assert(b >= 0 && b < 255);
45  std::stringstream ss;
46  ss << '#' << std::setw(2) << std::setfill('0') << std::hex << r
47  << std::setw(2) << std::setfill('0') << std::hex << g
48  << std::setw(2) << std::setfill('0') << std::hex << b;
49  return ss.str();
50  }
51 
53  std::string ColorRGB(int r, int g, int b, double a) {
54  emp_assert(r >= 0 && r < 255);
55  emp_assert(g >= 0 && g < 255);
56  emp_assert(b >= 0 && b < 255);
57  std::stringstream ss;
58  ss << "rgba(" << r << ',' << g << ',' << b << ',' << a << ')';
59  return ss.str();
60  }
61 
65  GetHueMap(size_t map_size, double min_h=0.0, double max_h=360.0, int s=100, int l=50) {
66  dHueMapKey map_key = std::make_tuple(map_size, min_h, max_h, s, l);
67 
68  // Grab the current map out of the cache.
69  emp::vector<std::string> & cur_map = hue_maps[map_key];
70 
71  // If we've already asked for an identical map before, skip map generation!
72  if (cur_map.size() != (std::size_t) map_size) {
73 
74  // Otherwise generate this map...
75  cur_map.resize(map_size);
76  double step_size = (max_h - min_h) / (double) map_size;
77  for (size_t i = 0; i < map_size; ++i) {
78  double h = min_h + step_size * i;
79  cur_map[i] = ColorHSL(h, s, l);
80  }
81  }
82 
83  return cur_map;
84  }
85 
88  GetHSLMap(size_t map_size, double min_h=0.0, double max_h=360.0,
89  int min_s=100, int max_s=100,
90  int min_l=50, int max_l=50) {
91 
92  // @CAO: Should cache maps!
93  emp::vector<std::string> cur_map(map_size);
94  double h_step = (max_h - min_h) / (double) map_size;
95  double s_step = (max_s - min_s) / (double) map_size;
96  double l_step = (max_l - min_l) / (double) map_size;
97  for (size_t i = 0; i < map_size; ++i) {
98  double h = min_h + h_step * i;
99  double s = min_s + s_step * i;
100  double l = min_l + l_step * i;
101  if (h > 360) h -= 360;
102  if (s > 100) s -= 100;
103  if (l > 100) l -= 100;
104  cur_map[i] = ColorHSL(h, s, l);
105  }
106 
107  return cur_map;
108  }
109 
110 
111 }
112 
113 
114 #endif
const emp::vector< std::string > & GetHueMap(size_t map_size, double min_h=0.0, double max_h=360.0, int s=100, int l=50)
Definition: color_map.h:65
size_t size() const
Definition: vector.h:151
void resize(size_t new_size)
Definition: vector.h:161
std::string ColorRGB(int r, int g, int b)
Generate a string to describe a JS color out of RGB values.
Definition: color_map.h:41
If we are in emscripten, make sure to include the header.
Definition: array.h:37
#define emp_assert(...)
Definition: assert.h:199
std::string ColorHSL(double h, double s, double l)
Generate a string to describe a JS color out of HSL values.
Definition: color_map.h:31
emp::vector< std::string > GetHSLMap(size_t map_size, double min_h=0.0, double max_h=360.0, int min_s=100, int max_s=100, int min_l=50, int max_l=50)
Generate a vector of color strings providing ranges of all of hue, satuation and luminosity.
Definition: color_map.h:88