Statistics
| Branch: | Tag: | Revision:

amiro-os / components / Color.cpp @ 2af9778e

History | View | Annotate | Download (5.453 KB)

1
#include <ch.hpp>
2
#include <hal.h>
3

    
4
#include <amiro/Color.h>
5

    
6
using namespace chibios_rt;
7
using namespace amiro;
8

    
9
Color::Color()
10
    : red(0),
11
      green(0),
12
      blue(0) {
13
}
14

    
15
Color::Color(const Color& color)
16
    : red(color.red),
17
      green(color.green),
18
      blue(color.blue) {
19
}
20

    
21
Color::Color(int r, int g, int b)
22
    : red(r),
23
      green(g),
24
      blue(b) {
25
}
26

    
27
Color::Color(GlobalColor color) {
28
  static const struct {
29
    unsigned char red;
30
    unsigned char green;
31
    unsigned char blue;
32
  } global_colors[] = {
33
      { 240, 248, 255 },
34
      { 250, 235, 215 },
35
      { 0, 255, 255 },
36
      { 127, 255, 212 },
37
      { 240, 255, 255 },
38
      { 245, 245, 220 },
39
      { 255, 228, 196 },
40
      { 0, 0, 0 },
41
      { 255, 235, 205 },
42
      { 0, 0, 255 },
43
      { 138, 43, 226 },
44
      { 165, 42, 42 },
45
      { 222, 184, 135 },
46
      { 95, 158, 160 },
47
      { 127, 255, 0 },
48
      { 210, 105, 30 },
49
      { 255, 127, 80 },
50
      { 100, 149, 237 },
51
      { 255, 248, 220 },
52
      { 220, 20, 60 },
53
      { 0, 255, 255 },
54
      { 0, 0, 139 },
55
      { 0, 139, 139 },
56
      { 184, 134, 11 },
57
      { 169, 169, 169 },
58
      { 0, 100, 0 },
59
      { 169, 169, 169 },
60
      { 189, 183, 107 },
61
      { 139, 0, 139 },
62
      { 85, 107, 47 },
63
      { 255, 140, 0 },
64
      { 153, 50, 204 },
65
      { 139, 0, 0 },
66
      { 233, 150, 122 },
67
      { 143, 188, 143 },
68
      { 72, 61, 139 },
69
      { 47, 79, 79 },
70
      { 47, 79, 79 },
71
      { 0, 206, 209 },
72
      { 148, 0, 211 },
73
      { 255, 20, 147 },
74
      { 0, 191, 255 },
75
      { 105, 105, 105 },
76
      { 105, 105, 105 },
77
      { 30, 144, 255 },
78
      { 178, 34, 34 },
79
      { 255, 250, 240 },
80
      { 34, 139, 34 },
81
      { 255, 0, 255 },
82
      { 220, 220, 220 },
83
      { 248, 248, 255 },
84
      { 255, 215, 0 },
85
      { 218, 165, 32 },
86
      { 128, 128, 128 },
87
      { 128, 128, 128 },
88
      { 0, 128, 0 },
89
      { 173, 255, 47 },
90
      { 240, 255, 240 },
91
      { 255, 105, 180 },
92
      { 205, 92, 92 },
93
      { 75, 0, 130 },
94
      { 255, 255, 240 },
95
      { 240, 230, 140 },
96
      { 230, 230, 250 },
97
      { 255, 240, 245 },
98
      { 124, 252, 0 },
99
      { 255, 250, 205 },
100
      { 173, 216, 230 },
101
      { 240, 128, 128 },
102
      { 224, 255, 255 },
103
      { 250, 250, 210 },
104
      { 211, 211, 211 },
105
      { 144, 238, 144 },
106
      { 211, 211, 211 },
107
      { 255, 182, 193 },
108
      { 255, 160, 122 },
109
      { 32, 178, 170 },
110
      { 135, 206, 250 },
111
      { 119, 136, 153 },
112
      { 119, 136, 153 },
113
      { 176, 196, 222 },
114
      { 255, 255, 224 },
115
      { 0, 255, 0 },
116
      { 50, 205, 50 },
117
      { 250, 240, 230 },
118
      { 255, 0, 255 },
119
      { 128, 0, 0 },
120
      { 102, 205, 170 },
121
      { 0, 0, 205 },
122
      { 186, 85, 211 },
123
      { 147, 112, 219 },
124
      { 60, 179, 113 },
125
      { 123, 104, 238 },
126
      { 0, 250, 154 },
127
      { 72, 209, 204 },
128
      { 199, 21, 133 },
129
      { 25, 25, 112 },
130
      { 245, 255, 250 },
131
      { 255, 228, 225 },
132
      { 255, 228, 181 },
133
      { 255, 222, 173 },
134
      { 0, 0, 128 },
135
      { 253, 245, 230 },
136
      { 128, 128, 0 },
137
      { 107, 142, 35 },
138
      { 255, 165, 0 },
139
      { 255, 69, 0 },
140
      { 218, 112, 214 },
141
      { 238, 232, 170 },
142
      { 152, 251, 152 },
143
      { 175, 238, 238 },
144
      { 219, 112, 147 },
145
      { 255, 239, 213 },
146
      { 255, 218, 185 },
147
      { 205, 133, 63 },
148
      { 255, 192, 203 },
149
      { 221, 160, 221 },
150
      { 176, 224, 230 },
151
      { 128, 0, 128 },
152
      { 255, 0, 0 },
153
      { 188, 143, 143 },
154
      { 65, 105, 225 },
155
      { 139, 69, 19 },
156
      { 250, 128, 114 },
157
      { 244, 164, 96 },
158
      { 46, 139, 87 },
159
      { 255, 245, 238 },
160
      { 160, 82, 45 },
161
      { 192, 192, 192 },
162
      { 135, 206, 235 },
163
      { 106, 90, 205 },
164
      { 112, 128, 144 },
165
      { 112, 128, 144 },
166
      { 255, 250, 250 },
167
      { 0, 255, 127 },
168
      { 70, 130, 180 },
169
      { 210, 180, 140 },
170
      { 0, 128, 128 },
171
      { 216, 191, 216 },
172
      { 255, 99, 71 },
173
      { 64, 224, 208 },
174
      { 238, 130, 238 },
175
      { 245, 222, 179 },
176
      { 255, 255, 255 },
177
      { 245, 245, 245 },
178
      { 255, 255, 0 },
179
      { 154, 205, 50 }, };
180
  this->setRedGreenBlue(global_colors[color].red, global_colors[color].green,
181
                        global_colors[color].blue);
182
}
183

    
184
Color Color::darker(int factor) {
185
  if (factor <= 0)
186
    return *this;
187
  else if (factor < 100)
188
    return this->lighter(10000 / factor);
189

    
190
  int red = 100 * this->red / factor;
191
  int green = 100 * this->green / factor;
192
  int blue = 100 * this->blue / factor;
193

    
194
  return Color(red, green, blue);
195
}
196

    
197
void Color::getRedGreenBlue(int* r, int* g, int* b) {
198
  *r = this->red;
199
  *g = this->green;
200
  *b = this->blue;
201
}
202

    
203
int Color::getRed() {
204
  return this->red;
205
}
206

    
207
int Color::getGreen() {
208
  return this->green;
209
}
210

    
211
int Color::getBlue() {
212
  return this->blue;
213
}
214

    
215
Color Color::lighter(int factor) {
216
  if (factor <= 0)
217
    return *this;
218
  else if (factor < 100)
219
    return this->darker(10000 / factor);
220

    
221
  int red = factor * this->red / 100;
222
  int green = factor * this->green / 100;
223
  int blue = factor * this->blue / 100;
224

    
225
  red = (red < 0) ? 0 : (red > 255) ? 255 : red;
226
  green = (green < 0) ? 0 : (green > 255) ? 255 : green;
227
  blue = (blue < 0) ? 0 : (blue > 255) ? 255 : blue;
228

    
229
  return Color(red, green, blue);
230
}
231

    
232
void Color::setRedGreenBlue(int red, int green, int blue) {
233
  this->red = red;
234
  this->green = green;
235
  this->blue = blue;
236
}
237

    
238
Color& Color::operator=(Color const& color) {
239
  if (this != &color) {
240
    this->red = color.red;
241
    this->green = color.green;
242
    this->blue = color.blue;
243
  }
244
  return *this;
245
}
246

    
247
Color& Color::operator=(GlobalColor color) {
248
  return operator=(Color(color));
249
}