File: rgbhsl.cpp

package info (click to toggle)
rss-glx 0.7.5-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,184 kB
  • ctags: 1,561
  • sloc: cpp: 11,577; ansic: 4,233; sh: 3,315; asm: 1,312; xml: 552; makefile: 194; perl: 49
file content (199 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (9)
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
/*
 * Copyright (C) 2002  Terence M. Welsh
 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
 *
 * Rgbhsl is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as 
 * published by the Free Software Foundation.
 *
 * Rgbhsl 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rgbhsl.h"

void rgb2hsl (float r, float g, float b, float &h, float &s, float &l)
{
	int huezone = 0;
	float rr, gg, bb;

	// find huezone
	if (r >= g) {
		huezone = 0;
		if (b > r)
			huezone = 4;
		else {
			if (b > g)
				huezone = 5;
		}
	} else {
		huezone = 1;
		if (b > g)
			huezone = 2;
		else {
			if (b > r)
				huezone = 3;
		}
	}

	// luminosity
	switch (huezone) {
	case 0:
	case 5:
		l = r;
		rr = 1.0f;
		gg = g / l;
		bb = b / l;
		break;
	case 1:
	case 2:
		l = g;
		gg = 1.0f;
		rr = r / l;
		bb = b / l;
		break;
	default:
		l = b;
		bb = 1.0f;
		rr = r / l;
		gg = g / l;
	}
	if (l == 0.0) {
		h = 0.0;
		s = 1.0;
		return;
	}
	// saturation
	switch (huezone) {
	case 0:
	case 1:
		s = 1.0f - b;
		bb = 0.0f;
		rr = 1.0f - ((1.0f - rr) / s);
		gg = 1.0f - ((1.0f - gg) / s);
		break;
	case 2:
	case 3:
		s = 1.0f - r;
		rr = 0.0f;
		gg = 1.0f - ((1.0f - gg) / s);
		bb = 1.0f - ((1.0f - bb) / s);
		break;
	default:
		s = 1.0f - g;
		gg = 0.0f;
		rr = 1.0f - ((1.0f - rr) / s);
		bb = 1.0f - ((1.0f - bb) / s);
	}

	// hue
	switch (huezone) {
	case 0:
		h = g / 6.0f;
		break;
	case 1:
		h = ((1.0f - r) / 6.0f) + 0.166667f;
		break;
	case 2:
		h = (b / 6.0f) + 0.333333f;
		break;
	case 3:
		h = ((1.0f - g) / 6.0f) + 0.5f;
		break;
	case 4:
		h = (r / 6.0f) + 0.666667f;
		break;
	default:
		h = ((1.0f - b) / 6.0f) + 0.833333f;
	}
}

void hsl2rgb (float h, float s, float l, float &r, float &g, float &b)
{
	// hue influence
	if (h < 0.166667) {	// full red, some green
		r = 1.0;
		g = h * 6.0f;
		b = 0.0;
	} else {
		if (h < 0.5) {	// full green
			g = 1.0;
			if (h < 0.333333) {	// some red
				r = 1.0f - ((h - 0.166667f) * 6.0f);
				b = 0.0;
			} else {	// some blue
				b = (h - 0.333333f) * 6.0f;
				r = 0.0;
			}
		} else {
			if (h < 0.833333) {	// full blue
				b = 1.0;
				if (h < 0.666667) {	// some green
					g = 1.0f - ((h - 0.5f) * 6.0f);
					r = 0.0;
				} else {	// some red
					r = (h - 0.666667f) * 6.0f;
					g = 0.0;
				}
			} else {	// full red, some blue
				r = 1.0;
				b = 1.0f - ((h - 0.833333f) * 6.0f);
				g = 0.0;
			}
		}
	}

	// saturation influence
	r = 1.0f - (s * (1.0f - r));
	g = 1.0f - (s * (1.0f - g));
	b = 1.0f - (s * (1.0f - b));

	// luminosity influence
	r *= l;
	g *= l;
	b *= l;
}

void hslTween (float h1, float s1, float l1, float h2, float s2, float l2, float tween, int direction, float &outh, float &outs, float &outl)
{
	// hue
	if (!direction) {	// forward around color wheel
		if (h2 >= h1)
			outh = h1 + (tween * (h2 - h1));
		else {
			outh = h1 + (tween * (1.0f - (h1 - h2)));
			if (outh > 1.0)
				outh -= 1.0;
		}
	} else {		// backward around color wheel
		if (h1 >= h2)
			outh = h1 - (tween * (h1 - h2));
		else {
			outh = h1 - (tween * (1.0f - (h2 - h1)));
			if (outh < 0.0)
				outh += 1.0;
		}
	}

	// saturation
	outs = s1 + (tween * (s2 - s1));

	// luminosity
	outl = l1 + (tween * (l2 - l1));
}

void rgbTween (float r1, float g1, float b1, float r2, float g2, float b2, float tween, int direction, float &outr, float &outg, float &outb)
{
	float h1, s1, l1, h2, s2, l2, outh, outs, outl;

	rgb2hsl (r1, g1, b1, h1, s1, l1);
	rgb2hsl (r2, g2, b2, h2, s2, l2);
	hslTween (h1, s1, l1, h2, s2, l2, tween, direction, outh, outs, outl);
	hsl2rgb (outh, outs, outl, outr, outg, outb);
}