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 | //! Orx Color |
---|
33 | const Color Color::orx(.2, .5, .7, .8); //!< TODO Define the ORX-color :) |
---|
34 | |
---|
35 | |
---|
36 | void Color::slerpHSV(const Color& c, float v) |
---|
37 | { |
---|
38 | this->a() += (c.a() - this->a()) * v; |
---|
39 | Vector from(r(), g(), b()); |
---|
40 | Vector to(c.r(), c.g(), c.b()); |
---|
41 | |
---|
42 | from = RGBtoHSV(from); |
---|
43 | to = RGBtoHSV(to); |
---|
44 | |
---|
45 | from += (to - from) * v; |
---|
46 | |
---|
47 | to = HSVtoRGB(from); |
---|
48 | |
---|
49 | this->r() = to.x; |
---|
50 | this->g() = to.y; |
---|
51 | this->b() = to.z; |
---|
52 | } |
---|
53 | |
---|
54 | Color Color::slerpHSVColor(const Color& from, const Color& to, float v) |
---|
55 | { |
---|
56 | Color fromColor(from); |
---|
57 | fromColor.slerpHSV(to, v); |
---|
58 | return fromColor; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | void Color::debug() const |
---|
63 | { |
---|
64 | printf("r:%0.2f g:%0.2f, b:%0.2f, a:%0.2f\n", r(), g(), b(), a()); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * @brief transforms from RGB to HSVtoRGB |
---|
71 | * @param RGB: the RGB-color [0-1] |
---|
72 | * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
73 | */ |
---|
74 | Vector Color::RGBtoHSV(const Vector& RGB) |
---|
75 | { |
---|
76 | Vector HSV; |
---|
77 | RGBtoHSV(RGB, HSV); |
---|
78 | return HSV; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * @brief transforms from RGB to HSVtoRGB |
---|
83 | * @param RGB: the RGB-color [0-1] |
---|
84 | * @param HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
85 | */ |
---|
86 | void Color::RGBtoHSV(const Vector& RGB, Vector& HSV) |
---|
87 | { |
---|
88 | float r = RGB.x; |
---|
89 | float g = RGB.y; |
---|
90 | float b = RGB.z; |
---|
91 | |
---|
92 | float h=0,s=1.0,v=1.0; |
---|
93 | float max_v,min_v,diff,r_dist,g_dist,b_dist; |
---|
94 | float undefined = 0.0; |
---|
95 | |
---|
96 | max_v = maxrgb(r,g,b); |
---|
97 | min_v = minrgb(r,g,b); |
---|
98 | diff = max_v - min_v; |
---|
99 | v = max_v; |
---|
100 | |
---|
101 | if( max_v != 0 ) |
---|
102 | s = diff/max_v; |
---|
103 | else |
---|
104 | s = 0.0; |
---|
105 | if( s == 0 ) |
---|
106 | h = undefined; |
---|
107 | else |
---|
108 | { |
---|
109 | r_dist = (max_v - r)/diff; |
---|
110 | g_dist = (max_v - g)/diff; |
---|
111 | b_dist = (max_v - b)/diff; |
---|
112 | if( r == max_v ) |
---|
113 | h = b_dist - g_dist; |
---|
114 | else |
---|
115 | if( g == max_v ) |
---|
116 | h = 2 + r_dist - b_dist; |
---|
117 | else |
---|
118 | if( b == max_v ) |
---|
119 | h = 4 + g_dist - r_dist; |
---|
120 | else |
---|
121 | printf("rgb2hsv::How did I get here?\n"); |
---|
122 | h *= 60.; |
---|
123 | if( h < 0) |
---|
124 | h += 360.0; |
---|
125 | } |
---|
126 | HSV = Vector(h, s, v); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief converts a Color from HSV to RGBtoHSV |
---|
131 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
132 | * @returns RGB: Vector [0-1] |
---|
133 | */ |
---|
134 | Vector Color::HSVtoRGB(const Vector& HSV) |
---|
135 | { |
---|
136 | Vector RGB; |
---|
137 | HSVtoRGB(HSV, RGB); |
---|
138 | return RGB; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * @brief converts a Color from HSV to RGBtoHSV |
---|
143 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
144 | * @param RGB: Vector [0-1] |
---|
145 | */ |
---|
146 | void Color::HSVtoRGB(const Vector& HSV, Vector& RGB) |
---|
147 | { |
---|
148 | float h = HSV.x; |
---|
149 | float s = HSV.y; |
---|
150 | float v = HSV.z; |
---|
151 | float r=0, g=0, b=0; |
---|
152 | float f,p,q,t; |
---|
153 | int i; |
---|
154 | |
---|
155 | if( s == 0 ) |
---|
156 | { |
---|
157 | r = v; |
---|
158 | g = v; |
---|
159 | b = v; |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | if(h >= 360.) |
---|
164 | h = h - (float)((int)(ceilf(h) / 360.0)); |
---|
165 | h /= 60.; |
---|
166 | i = (int) h; |
---|
167 | f = h - i; |
---|
168 | p = v*(1-s); |
---|
169 | q = v*(1-(s*f)); |
---|
170 | t = v*(1-s*(1-f)); |
---|
171 | switch(i) |
---|
172 | { |
---|
173 | case 0: |
---|
174 | r = v; |
---|
175 | g = t; |
---|
176 | b = p; |
---|
177 | break; |
---|
178 | case 1: |
---|
179 | r = q; |
---|
180 | g = v; |
---|
181 | b = p; |
---|
182 | break; |
---|
183 | case 2: |
---|
184 | r = p; |
---|
185 | g = v; |
---|
186 | b = t; |
---|
187 | break; |
---|
188 | case 3: |
---|
189 | r = p; |
---|
190 | g = q; |
---|
191 | b = v; |
---|
192 | break; |
---|
193 | case 4: |
---|
194 | r = t; |
---|
195 | g = p; |
---|
196 | b = v; |
---|
197 | break; |
---|
198 | case 5: |
---|
199 | r = v; |
---|
200 | g = p; |
---|
201 | b = q; |
---|
202 | break; |
---|
203 | default: |
---|
204 | r = 1.0; |
---|
205 | g = 1.0; |
---|
206 | b = 1.0; |
---|
207 | //printf("hsv2rgb::How did I get here?\n"); |
---|
208 | // printf("h: %f, s: %f, v: %f; i: %d\n",hin,s,v,i); |
---|
209 | break; |
---|
210 | } |
---|
211 | } |
---|
212 | RGB = Vector(r,g,b); |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | // Needed by rgb2hsv() |
---|
217 | float Color::maxrgb(float r, float g, float b) |
---|
218 | { |
---|
219 | float max; |
---|
220 | if( r > g) |
---|
221 | max = r; |
---|
222 | else |
---|
223 | max = g; |
---|
224 | if( b > max ) |
---|
225 | max = b; |
---|
226 | return( max ); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | // Needed by rgb2hsv() |
---|
231 | float Color::minrgb(float r,float g,float b) |
---|
232 | { |
---|
233 | float min; |
---|
234 | |
---|
235 | if( r < g) |
---|
236 | min = r; |
---|
237 | else |
---|
238 | min = g; |
---|
239 | if( b < min ) |
---|
240 | min = b; |
---|
241 | return( min ); |
---|
242 | } |
---|
243 | |
---|