1 /*
2 * Copyright (C) 2002 Terence M. Welsh
3 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
4 *
5 * Rgbhsl is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Rgbhsl is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "rgbhsl.h"
20
rgb2hsl(float r,float g,float b,float & h,float & s,float & l)21 void rgb2hsl (float r, float g, float b, float &h, float &s, float &l)
22 {
23 int huezone = 0;
24 float rr, gg, bb;
25
26 // find huezone
27 if (r >= g) {
28 huezone = 0;
29 if (b > r)
30 huezone = 4;
31 else {
32 if (b > g)
33 huezone = 5;
34 }
35 } else {
36 huezone = 1;
37 if (b > g)
38 huezone = 2;
39 else {
40 if (b > r)
41 huezone = 3;
42 }
43 }
44
45 // luminosity
46 switch (huezone) {
47 case 0:
48 case 5:
49 l = r;
50 rr = 1.0f;
51 gg = g / l;
52 bb = b / l;
53 break;
54 case 1:
55 case 2:
56 l = g;
57 gg = 1.0f;
58 rr = r / l;
59 bb = b / l;
60 break;
61 default:
62 l = b;
63 bb = 1.0f;
64 rr = r / l;
65 gg = g / l;
66 }
67 if (l == 0.0) {
68 h = 0.0;
69 s = 1.0;
70 return;
71 }
72 // saturation
73 switch (huezone) {
74 case 0:
75 case 1:
76 s = 1.0f - b;
77 bb = 0.0f;
78 rr = 1.0f - ((1.0f - rr) / s);
79 gg = 1.0f - ((1.0f - gg) / s);
80 break;
81 case 2:
82 case 3:
83 s = 1.0f - r;
84 rr = 0.0f;
85 gg = 1.0f - ((1.0f - gg) / s);
86 bb = 1.0f - ((1.0f - bb) / s);
87 break;
88 default:
89 s = 1.0f - g;
90 gg = 0.0f;
91 rr = 1.0f - ((1.0f - rr) / s);
92 bb = 1.0f - ((1.0f - bb) / s);
93 }
94
95 // hue
96 switch (huezone) {
97 case 0:
98 h = g / 6.0f;
99 break;
100 case 1:
101 h = ((1.0f - r) / 6.0f) + 0.166667f;
102 break;
103 case 2:
104 h = (b / 6.0f) + 0.333333f;
105 break;
106 case 3:
107 h = ((1.0f - g) / 6.0f) + 0.5f;
108 break;
109 case 4:
110 h = (r / 6.0f) + 0.666667f;
111 break;
112 default:
113 h = ((1.0f - b) / 6.0f) + 0.833333f;
114 }
115 }
116
hsl2rgb(float h,float s,float l,float & r,float & g,float & b)117 void hsl2rgb (float h, float s, float l, float &r, float &g, float &b)
118 {
119 // hue influence
120 if (h < 0.166667) { // full red, some green
121 r = 1.0;
122 g = h * 6.0f;
123 b = 0.0;
124 } else {
125 if (h < 0.5) { // full green
126 g = 1.0;
127 if (h < 0.333333) { // some red
128 r = 1.0f - ((h - 0.166667f) * 6.0f);
129 b = 0.0;
130 } else { // some blue
131 b = (h - 0.333333f) * 6.0f;
132 r = 0.0;
133 }
134 } else {
135 if (h < 0.833333) { // full blue
136 b = 1.0;
137 if (h < 0.666667) { // some green
138 g = 1.0f - ((h - 0.5f) * 6.0f);
139 r = 0.0;
140 } else { // some red
141 r = (h - 0.666667f) * 6.0f;
142 g = 0.0;
143 }
144 } else { // full red, some blue
145 r = 1.0;
146 b = 1.0f - ((h - 0.833333f) * 6.0f);
147 g = 0.0;
148 }
149 }
150 }
151
152 // saturation influence
153 r = 1.0f - (s * (1.0f - r));
154 g = 1.0f - (s * (1.0f - g));
155 b = 1.0f - (s * (1.0f - b));
156
157 // luminosity influence
158 r *= l;
159 g *= l;
160 b *= l;
161 }
162
hslTween(float h1,float s1,float l1,float h2,float s2,float l2,float tween,int direction,float & outh,float & outs,float & outl)163 void hslTween (float h1, float s1, float l1, float h2, float s2, float l2, float tween, int direction, float &outh, float &outs, float &outl)
164 {
165 // hue
166 if (!direction) { // forward around color wheel
167 if (h2 >= h1)
168 outh = h1 + (tween * (h2 - h1));
169 else {
170 outh = h1 + (tween * (1.0f - (h1 - h2)));
171 if (outh > 1.0)
172 outh -= 1.0;
173 }
174 } else { // backward around color wheel
175 if (h1 >= h2)
176 outh = h1 - (tween * (h1 - h2));
177 else {
178 outh = h1 - (tween * (1.0f - (h2 - h1)));
179 if (outh < 0.0)
180 outh += 1.0;
181 }
182 }
183
184 // saturation
185 outs = s1 + (tween * (s2 - s1));
186
187 // luminosity
188 outl = l1 + (tween * (l2 - l1));
189 }
190
rgbTween(float r1,float g1,float b1,float r2,float g2,float b2,float tween,int direction,float & outr,float & outg,float & outb)191 void rgbTween (float r1, float g1, float b1, float r2, float g2, float b2, float tween, int direction, float &outr, float &outg, float &outb)
192 {
193 float h1, s1, l1, h2, s2, l2, outh, outs, outl;
194
195 rgb2hsl (r1, g1, b1, h1, s1, l1);
196 rgb2hsl (r2, g2, b2, h2, s2, l2);
197 hslTween (h1, s1, l1, h2, s2, l2, tween, direction, outh, outs, outl);
198 hsl2rgb (outh, outs, outl, outr, outg, outb);
199 }
200