HOG2
Colors.h
Go to the documentation of this file.
1 //
2 // Colors.h
3 // hog2
4 //
5 // Created by Nathan Sturtevant on 7/29/17.
6 // Copyright © 2017 NS Software. All rights reserved.
7 //
8 
9 #ifndef Colors_h
10 #define Colors_h
11 
12 #include <string>
13 
17 class rgbColor {
18 public:
19  rgbColor() {}
20  rgbColor(float rr, float gg, float bb) :r(rr), g(gg), b(bb) {}
21  rgbColor &operator=(const rgbColor & ) = default;
22  rgbColor &operator*=(float p) { r*=p; g*=p; b*=p; return *this; }
23  rgbColor operator*(float p) const { rgbColor tmp(*this); tmp*=p; return tmp; }
24  rgbColor operator+(const rgbColor &c) const { return mix(*this, c, 0.5); }
25  static rgbColor mix(const rgbColor &c1, const rgbColor &c2, float perc)
26  {
27  return rgbColor((1-perc)*c1.r+c2.r*perc, (1-perc)*c1.g+c2.g*perc, (1-perc)*c1.b+c2.b*perc);
28  }
29  static rgbColor hsl(float h, float s, float l)
30  {
31  float r, g, b;
32 
33  if (s == 0)
34  {
35  r = g = b = l; // achromatic
36  }
37  else {
38  float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
39  float p = 2 * l - q;
40  r = hue2rgb(p, q, h + 1.0f/3.0f);
41  g = hue2rgb(p, q, h);
42  b = hue2rgb(p, q, h - 1.0f/3.0f);
43  }
44  return rgbColor(r, g, b);
45  }
46  void mix(const rgbColor &c, float perc)
47  {
48  r = (1-perc)*r+c.r*perc;
49  g = (1-perc)*g+c.g*perc;
50  b = (1-perc)*b+c.b*perc;
51  }
52  std::string hex() const
53  { char tmp[255];
54  sprintf(tmp, "#%X%X%X%X%X%X",
55  (int(r*255))/16, (int(r*255))%16,
56  (int(g*255))/16, (int(g*255))%16,
57  (int(b*255))/16, (int(b*255))%16);
58  return tmp;
59  }
60  void hex(const char *str) // get color from #RRGGBB format
61  {
62  if (str[0] != '#')
63  {
64  printf("Conversion failed\n");
65  return;
66  }
67  r = (unhexdigit(str[1])*16+unhexdigit(str[2]))/255.0;
68  g = (unhexdigit(str[3])*16+unhexdigit(str[4]))/255.0;
69  b = (unhexdigit(str[5])*16+unhexdigit(str[6]))/255.0;
70  }
71  float r,g,b;
72 private:
73  static float hue2rgb(float p, float q, float t)
74  {
75  if (t < 0) t += 1;
76  if (t > 1) t -= 1;
77  if (t < 1.0/6.0) return p + (q - p) * 6 * t;
78  if (t < 1.0/2.0) return q;
79  if (t < 2.0/3.0) return p + (q - p) * (2.0/3.0 - t) * 6;
80  return p;
81  }
82 
83  int unhexdigit(char c)
84  {
85  switch (c)
86  {
87  case '0': return 0;
88  case '1': return 1;
89  case '2': return 2;
90  case '3': return 3;
91  case '4': return 4;
92  case '5': return 5;
93  case '6': return 6;
94  case '7': return 7;
95  case '8': return 8;
96  case '9': return 9;
97  case 'A': return 10;
98  case 'B': return 11;
99  case 'C': return 12;
100  case 'D': return 13;
101  case 'E': return 14;
102  case 'F': return 15;
103  }
104  return 0;
105  }
106 };
107 
108 bool operator==(const rgbColor &r1, const rgbColor &r2);
109 bool operator!=(const rgbColor &r1, const rgbColor &r2);
110 
111 namespace Colors
112 {
116  rgbColor GetColor(float v, float vmin, float vmax, int type);
117 
118 
119  const rgbColor black = {0.0,0.0,0.0};
120  const rgbColor white = {1.0,1.0,1.0};
121  const rgbColor gray = {0.5,0.5,0.5};
122  const rgbColor bluegray = {0.4,0.5,0.6};
123  const rgbColor darkgray= {0.25,0.25,0.25};
124  const rgbColor darkbluegray= {0.15,0.25,0.35};
125  const rgbColor lightgray={0.75,0.75,0.75};
126  const rgbColor lightbluegray={0.65,0.75,0.85};
127 
128  const rgbColor red = {1.0,0.0,0.0}; // red
129  const rgbColor darkred= {0.5,0.0,0.0}; // red
130  const rgbColor lightred= {1.0,0.5,0.5}; // red
131  const rgbColor lighterred= {1.0,0.75,0.75}; // red
132  const rgbColor brown = {0.5,0.25,0.0}; // brown
133  const rgbColor lightbrown = {0.75,0.5,0.25}; // brown
134 
135  const rgbColor green = {0.0,1.0,0.0}; // green
136  const rgbColor darkgreen= {0.0,0.5,0.0}; // green
137  const rgbColor lightgreen= {0.5,1.0,0.5}; // green
138 
139  const rgbColor bluegreen = {0.0f,0.65f,0.5f}; // blue
140 
141 
142  const rgbColor blue = {0.0,0.0,1.0}; // blue
143  const rgbColor darkblue = {0.0,0.0,0.5}; // blue
144  const rgbColor lightblue = {0.5,0.5,1.0}; // blue
145  const rgbColor lighterblue = {0.75,0.75,1.0}; // blue
146 
147  const rgbColor lightyellow = {1.0,1.0,0.5}; // yellow
148  const rgbColor yellow = {1.0,1.0,0.0}; // yellow
149  const rgbColor darkyellow = {0.5,0.5,0.0}; // yellow
150  const rgbColor magenta = {1.0,0.0,1.0}; //
151  const rgbColor purple = {0.5,0.0,1.0}; // purple
152  const rgbColor darkpurple = {0.25,0.0,0.5}; //
153  const rgbColor cyan = {0.0,1.0,1.0}; // cyan
154 
155  const rgbColor orange = {1.0,0.5,0.0}; // orange
156  const rgbColor pink = {1.0,0.0,0.5}; // pink
157 
158  const rgbColor cb0 = {0, 0, 0}; // black
159  const rgbColor cb1 = {0, 114.0f/255.0f, 178/255.0f}; // blue
160  const rgbColor cb2 = {204/255.0f, 121/255.0f, 167/255.0f}; // reddish purple
161  const rgbColor cb3 = {230/255.0f, 159/255.0f, 0}; // orange
162  const rgbColor cb4 = {86/255.0f, 180/255.0f, 233/255.0f}; // sky blue
163  const rgbColor cb5 = {213/255.0f, 94/255.0f, 0}; // vermillion
164 }
165 
166 
167 #endif /* Colors_h */
rgbColor::operator+
rgbColor operator+(const rgbColor &c) const
Definition: Colors.h:24
Colors::cb0
const rgbColor cb0
Definition: Colors.h:158
Colors::GetColor
rgbColor GetColor(float v, float vmin, float vmax, int type)
Given min/max values, get a color from a color schema.
Definition: Colors.cpp:24
rgbColor::hsl
static rgbColor hsl(float h, float s, float l)
Definition: Colors.h:29
Colors::lightyellow
const rgbColor lightyellow
Definition: Colors.h:147
Colors::cb2
const rgbColor cb2
Definition: Colors.h:160
rgbColor::b
float b
Definition: Colors.h:71
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
rgbColor::mix
void mix(const rgbColor &c, float perc)
Definition: Colors.h:46
Colors::cb1
const rgbColor cb1
Definition: Colors.h:159
rgbColor::operator=
rgbColor & operator=(const rgbColor &)=default
Colors::pink
const rgbColor pink
Definition: Colors.h:156
rgbColor::rgbColor
rgbColor()
Definition: Colors.h:19
Colors::purple
const rgbColor purple
Definition: Colors.h:151
Colors::darkblue
const rgbColor darkblue
Definition: Colors.h:143
Colors::lightblue
const rgbColor lightblue
Definition: Colors.h:144
Colors::darkbluegray
const rgbColor darkbluegray
Definition: Colors.h:124
rgbColor::g
float g
Definition: Colors.h:71
Colors::darkpurple
const rgbColor darkpurple
Definition: Colors.h:152
operator!=
bool operator!=(const rgbColor &r1, const rgbColor &r2)
Definition: Colors.cpp:11
Colors
Definition: Colors.cpp:21
Colors::brown
const rgbColor brown
Definition: Colors.h:132
rgbColor::hex
void hex(const char *str)
Definition: Colors.h:60
Colors::bluegray
const rgbColor bluegray
Definition: Colors.h:122
Colors::lightbluegray
const rgbColor lightbluegray
Definition: Colors.h:126
Colors::black
const rgbColor black
Definition: Colors.h:119
Colors::cb4
const rgbColor cb4
Definition: Colors.h:162
Colors::lightbrown
const rgbColor lightbrown
Definition: Colors.h:133
Colors::darkgray
const rgbColor darkgray
Definition: Colors.h:123
Colors::cyan
const rgbColor cyan
Definition: Colors.h:153
Colors::darkred
const rgbColor darkred
Definition: Colors.h:129
Colors::lightred
const rgbColor lightred
Definition: Colors.h:130
rgbColor::r
float r
Definition: Colors.h:71
rgbColor::rgbColor
rgbColor(float rr, float gg, float bb)
Definition: Colors.h:20
Colors::cb5
const rgbColor cb5
Definition: Colors.h:163
Colors::darkyellow
const rgbColor darkyellow
Definition: Colors.h:149
rgbColor::operator*
rgbColor operator*(float p) const
Definition: Colors.h:23
rgbColor::operator*=
rgbColor & operator*=(float p)
Definition: Colors.h:22
Colors::red
const rgbColor red
Definition: Colors.h:128
Colors::orange
const rgbColor orange
Definition: Colors.h:155
Colors::lighterblue
const rgbColor lighterblue
Definition: Colors.h:145
Colors::darkgreen
const rgbColor darkgreen
Definition: Colors.h:136
Colors::yellow
const rgbColor yellow
Definition: Colors.h:148
Colors::gray
const rgbColor gray
Definition: Colors.h:121
Colors::white
const rgbColor white
Definition: Colors.h:120
Colors::lightgreen
const rgbColor lightgreen
Definition: Colors.h:137
Colors::blue
const rgbColor blue
Definition: Colors.h:142
rgbColor::hex
std::string hex() const
Definition: Colors.h:52
Colors::magenta
const rgbColor magenta
Definition: Colors.h:150
rgbColor::hue2rgb
static float hue2rgb(float p, float q, float t)
Definition: Colors.h:73
Colors::lightgray
const rgbColor lightgray
Definition: Colors.h:125
Colors::lighterred
const rgbColor lighterred
Definition: Colors.h:131
operator==
bool operator==(const rgbColor &r1, const rgbColor &r2)
Definition: Colors.cpp:16
Colors::green
const rgbColor green
Definition: Colors.h:135
rgbColor::mix
static rgbColor mix(const rgbColor &c1, const rgbColor &c2, float perc)
Definition: Colors.h:25
Colors::cb3
const rgbColor cb3
Definition: Colors.h:161
Colors::bluegreen
const rgbColor bluegreen
Definition: Colors.h:139
rgbColor::unhexdigit
int unhexdigit(char c)
Definition: Colors.h:83