1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "color.h" |
---|
19 | #include <stdio.h> |
---|
20 | |
---|
21 | |
---|
22 | //! Red Color |
---|
23 | const Color Color::red(1,0,0, 1); |
---|
24 | //! Green Color |
---|
25 | const Color Color::green(0,1,0, 1) ; |
---|
26 | //! blue Color |
---|
27 | const Color Color::blue(0,0,1, 1); |
---|
28 | //! White Color |
---|
29 | const Color Color::white(1,1,1, 1); |
---|
30 | //! Black Color |
---|
31 | const Color Color::black(0,0,0,1); |
---|
32 | |
---|
33 | /** |
---|
34 | * @brief slerps the Color in the HSV color space into the direction of c |
---|
35 | * @param c the Color to slerp to |
---|
36 | * @param v the Value to slerp 0 means stay at *this, 1 means at c. |
---|
37 | */ |
---|
38 | void Color::slerpHSV(const Color& c, float v) |
---|
39 | { |
---|
40 | this->a() += (c.a() - this->a()) * v; |
---|
41 | Vector from(r(), g(), b()); |
---|
42 | Vector to(c.r(), c.g(), c.b()); |
---|
43 | |
---|
44 | from = RGBtoHSV(from); |
---|
45 | to = RGBtoHSV(to); |
---|
46 | |
---|
47 | from += (to - from) * v; |
---|
48 | |
---|
49 | to = HSVtoRGB(from); |
---|
50 | |
---|
51 | this->r() = to.x; |
---|
52 | this->g() = to.y; |
---|
53 | this->b() = to.z; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief simple slerp wrapper. |
---|
58 | * @param from from this color |
---|
59 | * @param to to this one |
---|
60 | * @param v how much |
---|
61 | * @see void Color::slerpHSV(const Color& c, float v) |
---|
62 | */ |
---|
63 | Color Color::slerpHSVColor(const Color& from, const Color& to, float v) |
---|
64 | { |
---|
65 | Color fromColor(from); |
---|
66 | fromColor.slerpHSV(to, v); |
---|
67 | return fromColor; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief nice and simple debug output for the colors (colorless :) ) |
---|
72 | */ |
---|
73 | void Color::debug() const |
---|
74 | { |
---|
75 | printf("r:%0.2f g:%0.2f, b:%0.2f, a:%0.2f\n", r(), g(), b(), a()); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief transforms from RGB to HSVtoRGB |
---|
82 | * @param RGB: the RGB-color [0-1] |
---|
83 | * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
84 | */ |
---|
85 | Vector Color::RGBtoHSV(const Vector& RGB) |
---|
86 | { |
---|
87 | Vector HSV; |
---|
88 | RGBtoHSV(RGB, HSV); |
---|
89 | return HSV; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * @brief transforms from RGB to HSVtoRGB |
---|
94 | * @param RGB: the RGB-color [0-1] |
---|
95 | * @param HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
96 | */ |
---|
97 | void Color::RGBtoHSV(const Vector& RGB, Vector& HSV) |
---|
98 | { |
---|
99 | float r = RGB.x; |
---|
100 | float g = RGB.y; |
---|
101 | float b = RGB.z; |
---|
102 | |
---|
103 | float h=0,s=1.0,v=1.0; |
---|
104 | float max_v,min_v,diff,r_dist,g_dist,b_dist; |
---|
105 | float undefined = 0.0; |
---|
106 | |
---|
107 | max_v = maxrgb(r,g,b); |
---|
108 | min_v = minrgb(r,g,b); |
---|
109 | diff = max_v - min_v; |
---|
110 | v = max_v; |
---|
111 | |
---|
112 | if( max_v != 0 ) |
---|
113 | s = diff/max_v; |
---|
114 | else |
---|
115 | s = 0.0; |
---|
116 | if( s == 0 ) |
---|
117 | h = undefined; |
---|
118 | else |
---|
119 | { |
---|
120 | r_dist = (max_v - r)/diff; |
---|
121 | g_dist = (max_v - g)/diff; |
---|
122 | b_dist = (max_v - b)/diff; |
---|
123 | if( r == max_v ) |
---|
124 | h = b_dist - g_dist; |
---|
125 | else |
---|
126 | if( g == max_v ) |
---|
127 | h = 2 + r_dist - b_dist; |
---|
128 | else |
---|
129 | if( b == max_v ) |
---|
130 | h = 4 + g_dist - r_dist; |
---|
131 | else |
---|
132 | printf("rgb2hsv::How did I get here?\n"); |
---|
133 | h *= 60.; |
---|
134 | if( h < 0) |
---|
135 | h += 360.0; |
---|
136 | } |
---|
137 | HSV = Vector(h, s, v); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * @brief converts a Color from HSV to RGBtoHSV |
---|
142 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
143 | * @returns RGB: Vector [0-1] |
---|
144 | */ |
---|
145 | Vector Color::HSVtoRGB(const Vector& HSV) |
---|
146 | { |
---|
147 | Vector RGB; |
---|
148 | HSVtoRGB(HSV, RGB); |
---|
149 | return RGB; |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * @brief converts a Color from HSV to RGBtoHSV |
---|
154 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
155 | * @param RGB: Vector [0-1] |
---|
156 | */ |
---|
157 | void Color::HSVtoRGB(const Vector& HSV, Vector& RGB) |
---|
158 | { |
---|
159 | float h = HSV.x; |
---|
160 | float s = HSV.y; |
---|
161 | float v = HSV.z; |
---|
162 | float r=0, g=0, b=0; |
---|
163 | float f,p,q,t; |
---|
164 | int i; |
---|
165 | |
---|
166 | if( s == 0 ) |
---|
167 | { |
---|
168 | r = v; |
---|
169 | g = v; |
---|
170 | b = v; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | if(h >= 360.) |
---|
175 | h = h - (float)((int)(ceilf(h) / 360.0)); |
---|
176 | h /= 60.; |
---|
177 | i = (int) h; |
---|
178 | f = h - i; |
---|
179 | p = v*(1-s); |
---|
180 | q = v*(1-(s*f)); |
---|
181 | t = v*(1-s*(1-f)); |
---|
182 | switch(i) |
---|
183 | { |
---|
184 | case 0: |
---|
185 | r = v; |
---|
186 | g = t; |
---|
187 | b = p; |
---|
188 | break; |
---|
189 | case 1: |
---|
190 | r = q; |
---|
191 | g = v; |
---|
192 | b = p; |
---|
193 | break; |
---|
194 | case 2: |
---|
195 | r = p; |
---|
196 | g = v; |
---|
197 | b = t; |
---|
198 | break; |
---|
199 | case 3: |
---|
200 | r = p; |
---|
201 | g = q; |
---|
202 | b = v; |
---|
203 | break; |
---|
204 | case 4: |
---|
205 | r = t; |
---|
206 | g = p; |
---|
207 | b = v; |
---|
208 | break; |
---|
209 | case 5: |
---|
210 | r = v; |
---|
211 | g = p; |
---|
212 | b = q; |
---|
213 | break; |
---|
214 | default: |
---|
215 | r = 1.0; |
---|
216 | g = 1.0; |
---|
217 | b = 1.0; |
---|
218 | //printf("hsv2rgb::How did I get here?\n"); |
---|
219 | // printf("h: %f, s: %f, v: %f; i: %d\n",hin,s,v,i); |
---|
220 | break; |
---|
221 | } |
---|
222 | } |
---|
223 | RGB = Vector(r,g,b); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * @returns the maximum of r g and a. |
---|
229 | * @param r Red. |
---|
230 | * @param g Green |
---|
231 | * @param b Blue |
---|
232 | */ |
---|
233 | float Color::maxrgb(float r, float g, float b) |
---|
234 | { |
---|
235 | float max; |
---|
236 | if( r > g) |
---|
237 | max = r; |
---|
238 | else |
---|
239 | max = g; |
---|
240 | if( b > max ) |
---|
241 | max = b; |
---|
242 | return( max ); |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * @returns the minimum of r g and a. |
---|
247 | * @param r Red. |
---|
248 | * @param g Green |
---|
249 | * @param b Blue |
---|
250 | */ |
---|
251 | float Color::minrgb(float r,float g,float b) |
---|
252 | { |
---|
253 | float min; |
---|
254 | |
---|
255 | if( r < g) |
---|
256 | min = r; |
---|
257 | else |
---|
258 | min = g; |
---|
259 | if( b < min ) |
---|
260 | min = b; |
---|
261 | return( min ); |
---|
262 | } |
---|
263 | |
---|