~amavect/makeu

makeu/color.c -rw-r--r-- 3.9 KiB
8a2a5c28amavect revise error exiting, allocation errors 1 year, 3 months ago
                                                                                
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
/* Amavect! */
/* color functions */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include "fns.h"

ulong
unsetalpha(ulong color)
{
	ulong alpha, red, green, blue;
	alpha = (color >> 0*8) & 0xFF;
	if(alpha == 0)
		return 0;
	red = (color >> 3*8) & 0xFF;
	green = (color >> 2*8) & 0xFF;
	blue = (color >> 1*8) & 0xFF;
	red = (ulong)ceil(red * 255.0 / alpha);
	green = (ulong)ceil(green * 255.0 / alpha);
	blue = (ulong)ceil(blue * 255.0 / alpha);
	if(red > 255)
		red = 255;
	if(green > 255)
		green = 255;
	if(blue > 255)
		blue = 255;
	return (red<<3*8) | (green<<2*8) | (blue<<1*8) | (alpha<<0*8);
}

ulong
rgbtocolor(RGB rgb)
{
	return (rgb.red<<24) + (rgb.green<<16) + (rgb.blue<<8) + 0xFF;;
}

RGB
colortorgb(ulong color)
{
	RGB rgb;
	rgb.red = (color>>24) & 0xFF;
	rgb.green = (color>>16) & 0xFF;
	rgb.blue = (color>>8) & 0xFF;
	return rgb;
}

/* stores color to the array of size 3 */
void
colortoarray(uchar col[4], ulong color)
{
	col[0] = (color >> 0) & 0xFF;
	col[1] = (color >> 8) & 0xFF;
	col[2] = (color >> 16) & 0xFF;
	col[3] = (color >> 24) & 0xFF;
}

RGBd
torgbd(RGB rgb)
{
	RGBd rgbd;
	rgbd.red = rgb.red / 255.0;
	rgbd.green = rgb.green / 255.0;
	rgbd.blue = rgb.blue / 255.0;
	return rgbd;
}

RGB
torgb(RGBd rgbd)
{
	RGB rgb;
	/* add 0.5 to round to nearest int */
	rgb.red = (uchar)(rgbd.red * 255 + 0.5);
	rgb.green = (uchar)(rgbd.green * 255 + 0.5);
	rgb.blue = (uchar)(rgbd.blue * 255 + 0.5);
	return rgb;
}

/* 
 * map HSV to RGB
 * implemented off of wikipedia lol
 */
RGBd
hsvdtorgbd(HSVd hsvd)
{
	RGBd rgbd;
	double c, m, x; /* chroma, m, x */
	c = hsvd.val * hsvd.sat;
	x = c * (1 - fabs(fmod(hsvd.hue, 2) - 1));
	rgbd.red = 0;
	rgbd.green = 0;
	rgbd.blue = 0;
	if(0 <= hsvd.hue && hsvd.hue <= 1){
		rgbd.red = c;
		rgbd.green = x;
	}else if(1 < hsvd.hue && hsvd.hue <= 2){
		rgbd.red = x;
		rgbd.green = c;
	}else if(2 < hsvd.hue && hsvd.hue <= 3){
		rgbd.green = c;
		rgbd.blue = x;
	}else if(3 < hsvd.hue && hsvd.hue <= 4){
		rgbd.green = x;
		rgbd.blue = c;
	}else if(4 < hsvd.hue && hsvd.hue <= 5){
		rgbd.red = x;
		rgbd.blue = c;
	}else if(5 < hsvd.hue && hsvd.hue <= 6){
		rgbd.red = c;
		rgbd.blue = x;
	}
	m = hsvd.val - c;
	rgbd.red += m, rgbd.green += m, rgbd.blue += m;
	return rgbd;
}

HSVd
rgbdtohsvd(RGBd rgbd)
{
	HSVd hsvd;
	double max, min;
	
	if(rgbd.red >= rgbd.green && rgbd.red > rgbd.blue){
		max = rgbd.red;
		if(rgbd.green > rgbd.blue)
			min = rgbd.blue;
		else
			min = rgbd.green;
		hsvd.hue = (rgbd.green - rgbd.blue) / (max - min);
		hsvd.sat = (max - min) / max;
		hsvd.val = max;
		if(hsvd.hue < 0)
			hsvd.hue += 6;
	}else if(rgbd.green >= rgbd.blue && rgbd.green > rgbd.red){
		max = rgbd.green;
		if(rgbd.red > rgbd.blue)
			min = rgbd.blue;
		else
			min = rgbd.red;
		hsvd.hue = 2 + (rgbd.blue - rgbd.red) / (max - min);
		hsvd.sat = (max - min) / max;
		hsvd.val = max;
	}else if(rgbd.blue >= rgbd.red && rgbd.blue > rgbd.green){
		max = rgbd.blue;
		if(rgbd.red > rgbd.green)
			min = rgbd.green;
		else
			min = rgbd.red;
		hsvd.hue = 4 + (rgbd.red - rgbd.green) / (max - min);
		hsvd.sat = (max - min) / max;
		hsvd.val = max;
	}else{ /* r==g==b */
		hsvd.hue = 0;
		hsvd.sat = 0; /* max==min */
		hsvd.val = rgbd.red;
	}
	
	return hsvd;
}

HSVd
rgbtohsvd(RGB rgb)
{
	return rgbdtohsvd(torgbd(rgb));
}

RGB
hsvdtorgb(HSVd hsvd)
{
	return torgb(hsvdtorgbd(hsvd));
}

/* 
 * returns DNotacolor (0xFFFFFF00) if invalid, else the color
 * assumes input str is a valid string
 */
ulong
strtocolor(char *str)
{
	uchar alpha, chan;
	ulong c;
	int i;
	
	if(strlen(str) != 10)
		return DNotacolor;
	if(str[10] != '\0')
		return DNotacolor;
	if(str[0] != '0' || str[1] != 'x')
		return DNotacolor;
	for(i = 2; i < 10; i++){
		if(!(
			str[i] >= '0' && str[i] <= '9' || 
			str[i] >= 'a' && str[i] <= 'f' ||
			str[i] >= 'A' && str[i] <= 'F'))
			return DNotacolor;
	}
	c = strtoul(str, nil, 0);
	alpha = c & 0xFF;
	for(i = 8; i <= 24; i+=8){
		chan = c >> i & 0xFF;
		if(chan > alpha)
			return DNotacolor;
	}
	return c;
}