aboutsummaryrefslogtreecommitdiff
path: root/src/color.c
blob: 5208766c2b0fd6033e824cc8eab8fed469bd81cb (plain)
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
#include "mandel.h"
#include "config.h"

#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))

static Color	color_hsl_to_rgb(ColorHSL color_hsl);
static Color	*st_hsl_rainbow(int count);
static int		st_compar_control_points(const void *ptr1, const void *ptr2);
static Color	*st_linear_iterpolation(int count, ControlPoint *points, size_t points_count);

unsigned int	color_texture_new(int count)
{
	unsigned int	texture;
	Color			*palette;

	if ((palette = st_hsl_rainbow(count)) == NULL)
		return 0;
	/* if ((palette = st_linear_iterpolation(count, g_theme, sizeof(g_theme) / sizeof(ControlPoint))) == NULL) */
	/* 	return 0; */
	GL_CALL(glGenTextures(1, &texture));
	GL_CALL(glBindTexture(GL_TEXTURE_1D, texture));
	GL_CALL(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
	GL_CALL(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
	GL_CALL(glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, count + 1, 0, GL_RGB, GL_UNSIGNED_BYTE, palette));
	free(palette);
	return texture;
}

static Color	*st_linear_iterpolation(int count, ControlPoint *points, size_t points_count)
{
	Color			*palette;

	if (points_count < 2)
		return NULL;
    if ((palette = malloc(sizeof(Color) * count)) == NULL)
		return NULL;
	qsort(points, points_count, sizeof(ControlPoint), st_compar_control_points);

	size_t	point_i = 0;

	double	x0 = points[point_i].position;
	double	x1 = points[point_i + 1].position;

	double	y0 = points[point_i].color.r;
	double	y1 = points[point_i + 1].color.r;

	double	delta_x = x1 - x0;
	double	delta_y = y1 - y0;

	double	m = delta_x / delta_y;
	double	b = y0 - x0;

	double red = 0;
	for (int i = 0; i < count; i++)
	{
		palette[i].r = (uint8_t)(255.0 * (m * red + b));
		red += 1.0 / (double)count;
		if (red >= x1)
		{
			if (point_i >= points_count)
				break;
			x0 = x1;
			point_i++;
			x1 = points[point_i + 1].position;
		}
		palette[i].g = 0;
		palette[i].b = 0;
		printf("%d\n", i);
	}
	return palette;
}

static int		st_compar_control_points(const void *ptr1, const void *ptr2)
{
	const ControlPoint	*point1 = ptr1;
	const ControlPoint	*point2 = ptr2;

	if (point1->position < point2->position)
		return -1;
	if (point1->position > point2->position)
		return 1;
	return 0;
}

static Color	*st_hsl_rainbow(int count)
{
	ColorHSL		hsl;
	Color			*palette;

    if ((palette = malloc(sizeof(Color) * count)) == NULL)
		return NULL;
    for (int i = 0; i < count; i++)
    {
		hsl.h = (uint8_t)(255.0 * ((double)i / (double)count));
		hsl.s = 150;
		hsl.l = 127;
		palette[i] = color_hsl_to_rgb(hsl);
    }
    for (int i = 0, j = count - 1; i < j; i++, j--)
	{
		Color tmp = palette[i];
		palette[i] = palette[j];
		palette[j] = tmp;
	}
	return palette;
}

static Color	color_hsl_to_rgb(ColorHSL color_hsl)
{
	Color	color_rgb;
	double	h;
	double	s;
	double	l;
	double	r;
	double	g;
	double	b;
	double temp1, temp2, tempr, tempg, tempb;

	h = color_hsl.h / 256.0;
	s = color_hsl.s / 256.0;
	l = color_hsl.l / 256.0;
	if(s == 0)
	{
		r = l;
		g = l;
		b = l;
	}
	else
	{
		if (l < 0.5) temp2 = l * (1 + s);
		else temp2 = (l + s) - (l * s);
		temp1 = 2 * l - temp2;
		tempr = h + 1.0 / 3.0;
		if (tempr > 1) tempr--;
		tempg = h;
		tempb = h - 1.0 / 3.0;
		if (tempb < 0) tempb++;

		if (tempr < 1.0 / 6.0) r = temp1 + (temp2 - temp1) * 6.0 * tempr;
		else if (tempr < 0.5) r = temp2;
		else if (tempr < 2.0 / 3.0) r = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempr) * 6.0;
		else r = temp1;

		if (tempg < 1.0 / 6.0) g = temp1 + (temp2 - temp1) * 6.0 * tempg;
		else if (tempg < 0.5) g = temp2;
		else if (tempg < 2.0 / 3.0) g = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempg) * 6.0;
		else g = temp1;

		if(tempb < 1.0 / 6.0) b = temp1 + (temp2 - temp1) * 6.0 * tempb;
		else if(tempb < 0.5) b = temp2;
		else if(tempb < 2.0 / 3.0) b = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempb) * 6.0;
		else b = temp1;
	}
	color_rgb.r = (int)(r * 256.0);
	color_rgb.g = (int)(g * 256.0);
	color_rgb.b = (int)(b * 256.0);
	return (color_rgb);
}