forked from ComputerNerd/Retro-Graphics-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_convert.cpp
232 lines (193 loc) · 6.14 KB
/
color_convert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
This file is part of Retro Graphics Toolkit
Retro Graphics Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
Retro Graphics Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
Copyright Sega16 (or whatever you wish to call me) (2012-2018)
*/
#include "color_compare.h"
#include "color_convert.h"
#include "system.h"
#include "palette.h"
#include "nearestColor.h"
#include "classpalettebar.h"
#include "class_global.h"
#include "gui.h"
uint8_t nespaltab[64 * 3];
uint8_t nespaltab_alt[64 * 3];
unsigned nearest_color_index(int val, unsigned startindex) {
return nearestOneChannel(val, palTab + startindex, 8);
}
unsigned nearest_color_index(int val) {
return nearest_color_index(val, palTypeGen);
}
static double max3(double a, double b, double c) {
if ((a > b) && (a > c))
return a;
if (b > c)
return b;
return c;
}
static double min3(double a, double b, double c) {
if ((a < b) && (a < c))
return a;
if (b < c)
return b;
return c;
}
/**
* Converts an RGB color value to HSL. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and l in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSL representation
*/
void rgbToHsl255(unsigned r, unsigned g, unsigned b, double * hh, double * ss, double * ll) {
double rd = double(r) / 255.0, gd = double(g) / 255.0, bd = double(b) / 255.0;
rgbToHsl(rd, gd, bd, hh, ss, ll);
}
void rgbToHsl(double r, double g, double b, double * hh, double * ss, double * ll) {
double max = max3(r, g, b);
double min = min3(r, g, b);
double h, s, l = (max + min) / 2.0;
if (max == min)
h = s = 0.0; // achromatic
else {
double d = max - min;
s = l > 0.5 ? d / (2.0 - max - min) : d / (max + min);
/*if (max == r)
h = (g - b) / d + (g < b ? 6 : 0);
else if (max == g)
h = (b - r) / d + 2.0;
else
h = (r - g) / d + 4.0;
h /= 6.0;*/
//From: http://easyrgb.com/index.php?X=MATH&H=18#text18
double del_R = ((( max - r ) / 6.0) + (d / 2.0)) / d;
double del_G = ((( max - g ) / 6.0) + (d / 2.0)) / d;
double del_B = ((( max - b ) / 6.0) + (d / 2.0)) / d;
if (r == max ) h = del_B - del_G;
else if (g == max ) h = (1.0 / 3.0) + del_R - del_B;
else if (b == max ) h = (2.0 / 3.0) + del_G - del_R;
if (h < 0.0) h += 1.0;
if (h > 1.0) h -= 1.0;
}
if (h > 1.0)
printf("Warning %f\n", h);
*hh = h;
*ll = l;
*ss = s;
}
static double hueToRGB(double v1, double v2, double vH) {
if (vH < 0.0)
vH += 1.0;
if (vH > 1.0)
vH -= 1.0;
if ((6.0 * vH) < 1.0)
return (v1 + (v2 - v1) * 6.0 * vH);
if ((2.0 * vH) < 1.0)
return v2;
if ((3.0 * vH) < 2.0)
return (v1 + (v2 - v1) * ((2.0 / 3.0) - vH) * 6.0);
return v1;
}
// HSL to RGB Based on: https://www.programmingalgorithms.com/algorithm/hsl-to-rgb/
void hslToRgb(double hue, double s, double l, uint8_t&r, uint8_t&g, uint8_t&b) {
r = 0;
g = 0;
b = 0;
if (s == 0.0)
r = g = b = l * 255.0;
else {
double v2 = (l < 0.5) ? (l * (1.0 + s)) : ((l + s) - (l * s));
double v1 = 2.0 * l - v2;
r = (255.0 * hueToRGB(v1, v2, hue + (1.0f / 3)));
g = (255.0 * hueToRGB(v1, v2, hue));
b = (255.0 * hueToRGB(v1, v2, hue - (1.0f / 3)));
}
}
static inline uint32_t sq(uint32_t x) {
return x * x;
}
uint32_t count_colors(uint8_t * image_ptr, uint32_t w, uint32_t h, uint8_t *colors_found, bool useAlpha) {
/*!
Scans for colors in an image stops at over 256 as if there is an excess of 256 colors there is no reason to continue
*/
uint32_t colors_amount = 3;
colors_found[0] = *image_ptr++;
colors_found[1] = *image_ptr++;
colors_found[2] = *image_ptr++;
if (useAlpha)
++image_ptr;
uint8_t start = 1;
uint32_t y;
for (y = 0; y < h; ++y) {
for (uint32_t x = start; x < w; ++x) {
start = 0;
uint8_t r, g, b;
r = *image_ptr++;
g = *image_ptr++;
b = *image_ptr++;
if (useAlpha)
image_ptr++;
bool new_col = true;
for (uint32_t c = 0; c < colors_amount; c += 3) {
if (r == colors_found[c] && g == colors_found[c + 1] && b == colors_found[c + 2]) {
new_col = false;
break;//exit loop
}
}
if (new_col) {
colors_found[colors_amount] = r;
colors_found[colors_amount + 1] = g;
colors_found[colors_amount + 2] = b;
colors_amount += 3;
}
if (colors_amount >= 765) {
printf("\nOver 255 colors timing out no need for operation to continue.\n");
return colors_amount / 3;
}
}
}
putchar('\n');
return colors_amount / 3;
}
void updateNesTab(unsigned emps, bool alt) {
uint32_t rgb_out;
if (alt) {
for (unsigned temp = 0; temp < 64; ++temp) {
rgb_out = nesPalToRgb(temp | emps);
nespaltab_alt[temp * 3] = (rgb_out >> 16) & 255; //red
nespaltab_alt[temp * 3 + 1] = (rgb_out >> 8) & 255; //green
nespaltab_alt[temp * 3 + 2] = rgb_out & 255; //blue
}
} else {
for (unsigned temp = 0; temp < 64; ++temp) {
rgb_out = nesPalToRgb(temp | emps);
nespaltab[temp * 3] = (rgb_out >> 16) & 255; //red
nespaltab[temp * 3 + 1] = (rgb_out >> 8) & 255; //green
nespaltab[temp * 3 + 2] = rgb_out & 255; //blue
}
}
}
void updateEmphasisCB(Fl_Widget*, void*) {
if (mode_editor == spriteEditor) {
currentProject->subSystem &= ~(NESempMask << NESempShiftAlt);
currentProject->subSystem |= (unsigned)palBar.slide[palBar.toTab(spriteEditor)][2]->value() << NESempShiftAlt;
} else {
currentProject->subSystem &= ~(NESempMask << NESempShift);
currentProject->subSystem |= (unsigned)palBar.slide[palBar.toTab(mode_editor)][2]->value() << NESempShift;
}
currentProject->pal->updateEmphasis();
window->redraw();
}