-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdirect8bpp.c
229 lines (190 loc) · 5.05 KB
/
direct8bpp.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
double r; // a fraction between 0 and 1
double g; // a fraction between 0 and 1
double b; // a fraction between 0 and 1
} rgb;
typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
static hsv rgb2hsv(rgb in);
static rgb hsv2rgb(hsv in);
hsv rgb2hsv(rgb in)
{
hsv out;
double min, max, delta;
min = in.r < in.g ? in.r : in.g;
min = min < in.b ? min : in.b;
max = in.r > in.g ? in.r : in.g;
max = max > in.b ? max : in.b;
out.v = max; // v
delta = max - min;
if (delta < 0.00001)
{
out.s = 0;
out.h = 0; // undefined, maybe nan?
return out;
}
if( max > 0.0 ) { // NOTE: if Max is == 0, this divide would cause a crash
out.s = (delta / max); // s
} else {
// if max is 0, then r = g = b = 0
// s = 0, h is undefined
out.s = 0.0;
out.h = NAN; // its now undefined
return out;
}
if( in.r >= max ) // > is bogus, just keeps compilor happy
out.h = ( in.g - in.b ) / delta; // between yellow & magenta
else
if( in.g >= max )
out.h = 2.0 + ( in.b - in.r ) / delta; // between cyan & yellow
else
out.h = 4.0 + ( in.r - in.g ) / delta; // between magenta & cyan
out.h *= 60.0; // degrees
if( out.h < 0.0 )
out.h += 360.0;
return out;
}
rgb hsv2rgb(hsv in)
{
double hh, p, q, t, ff;
long i;
rgb out;
if(in.s <= 0.0) { // < is bogus, just shuts up warnings
out.r = in.v;
out.g = in.v;
out.b = in.v;
return out;
}
hh = in.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch(i) {
case 0:
out.r = in.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = in.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = in.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = in.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = in.v;
break;
case 5:
default:
out.r = in.v;
out.g = p;
out.b = q;
break;
}
return out;
}
double H(rgb *a)
{
double result;
double r=a->r,g=a->g,b=a->b;
result=((r-g)+(r-b))/2;
result=result/(((r-g)*(r-g))+((r-b)*(g-b)));
result=sqrt(result);
result=acos(result);
return result;
}
int cmp(const void *aa, const void *bb)
{
/* int32_t R,G,B,dist;*/
rgb *a,*b;
double ad,bd;
hsv ha,hb;
a=(rgb *)aa;
b=(rgb *)bb;
ad=(a->r*a->r)*2+(a->g*a->g)*4+(a->b*a->b)*3;
bd=(b->r*b->r)*2+(b->g*b->g)*4+(b->b*b->b)*3;
ha=rgb2hsv(*a);
hb=rgb2hsv(*b);
ad=ha.h+ha.v;
bd=hb.h+hb.v;
/*
R=(int32_t)(a->r-b->r)*(int32_t)(a->r-b->r);
G=(int32_t)(a->g-b->g)*(int32_t)(a->g-b->g);
B=(int32_t)(a->b-b->b)*(int32_t)(a->b-b->b);
dist=2*R+4*G+3*B;
*/
/* if (ad<bd) return -1;
else if (ad>bd) return 1;
else return 0;*/
if (ha.h<hb.h)
if (ha.s<hb.s)
if (ha.v<hb.v) return -1;
else if (ha.v>hb.v) return 1;
else if (ha.s>hb.s)
if (ha.v<hb.v) return -1;
else if (ha.v>hb.v) return 1;
else if (ha.h>hb.h)
if (ha.s<hb.s)
if (ha.v<hb.v) return -1;
else if (ha.v>hb.v) return 1;
else if (ha.s>hb.s)
if (ha.v<hb.v) return -1;
else if (ha.v>hb.v) return 1;
else return 0;
/* if (ha.h<hb.h) return -1;
else if (ha.h>hb.h) return 1;
else if (ha.s<hb.s) return -1;
else if (ha.s>hb.s) return 1;
else if (ha.v<hb.v) return -1;
else if (ha.v>hb.v) return 1;
else {
return 0;
}*/
}
static int col8 [] = {0,32,84,112,144,174,224,255};
static int col4 [] = {0,84,174,255};
int main(void)
{
rgb colors[256];
int r,g,b;
int i=0;
// for (i=0;i<256;i++)
// printf ("{%d,%d,%d}%c\n",col4[(i>>6)&0x3],col8[(i>>3)&0x7],col8[i&7],i<255?',':' ');
//printf ("{%d,%d,%d}%c\n",col8[(i>>5)&0x7],col4[(i>>3)&0x3],col8[i&7],i<255?',':' ');
//printf ("{%d,%d,%d}%c\n",col8[(i>>5)&0x7],col8[(i>>2)&0x7],col4[i&3],i<255?',':' ');
for (b=0;b<4;b++)
for (g=0;g<8;g++)
for (r=0;r<8;r++) {
printf ("{%d,%d,%d}%c\n",col8[r],col8[g],col4[b],i<255?',':' ');
/* colors[i].r=r*(255.0f/3.0f)/255.0f;
colors[i].g=g*(255.0f/7.0f)/255.0f;
colors[i].b=b*(255.0f/7.0f)/255.0f;*/
i++;
}
// qsort(&colors,256,sizeof(rgb),cmp);
for (i=0;i<256;i++)
// printf ("{%.0f,%.0f,%.0f}%c //%f %f %f\n",colors[i].r*255,colors[i].g*255,colors[i].b*255,i<255?',':' ',colors[i].r,colors[i].g,colors[i].b);
// printf ("%.0f\t%.0f\t%.0f\n",colors[i].r*255,colors[i].g*255,colors[i].b*255,i<255?',':' ',colors[i].r,colors[i].g,colors[i].b);
return 0;
}