[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5009] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5009] | 18 | #include "color.h" |
---|
[7195] | 19 | #include <stdio.h> |
---|
[1853] | 20 | |
---|
[8448] | 21 | |
---|
[8986] | 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 | |
---|
[9755] | 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 | */ |
---|
[8986] | 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 | |
---|
[9755] | 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 | */ |
---|
[8986] | 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 | |
---|
[9755] | 70 | /** |
---|
| 71 | * @brief nice and simple debug output for the colors (colorless :) ) |
---|
| 72 | */ |
---|
[8448] | 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 | |
---|
[8619] | 78 | |
---|
| 79 | |
---|
[5011] | 80 | /** |
---|
[7195] | 81 | * @brief transforms from RGB to HSVtoRGB |
---|
| 82 | * @param RGB: the RGB-color [0-1] |
---|
[5011] | 83 | * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 84 | */ |
---|
[5010] | 85 | Vector Color::RGBtoHSV(const Vector& RGB) |
---|
| 86 | { |
---|
[7195] | 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 | { |
---|
[5010] | 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; |
---|
[8986] | 118 | else |
---|
| 119 | { |
---|
[5010] | 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; |
---|
[8986] | 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"); |
---|
[5011] | 133 | h *= 60.; |
---|
[5010] | 134 | if( h < 0) |
---|
| 135 | h += 360.0; |
---|
[5009] | 136 | } |
---|
[7195] | 137 | HSV = Vector(h, s, v); |
---|
[3365] | 138 | } |
---|
[1853] | 139 | |
---|
[5011] | 140 | /** |
---|
[7195] | 141 | * @brief converts a Color from HSV to RGBtoHSV |
---|
[5011] | 142 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 143 | * @returns RGB: Vector [0-1] |
---|
| 144 | */ |
---|
[5010] | 145 | Vector Color::HSVtoRGB(const Vector& HSV) |
---|
[3543] | 146 | { |
---|
[7195] | 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 | { |
---|
[5010] | 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; |
---|
[5009] | 165 | |
---|
[8986] | 166 | if( s == 0 ) |
---|
| 167 | { |
---|
[5010] | 168 | r = v; |
---|
| 169 | g = v; |
---|
| 170 | b = v; |
---|
| 171 | } |
---|
[8986] | 172 | else |
---|
| 173 | { |
---|
| 174 | if(h >= 360.) |
---|
| 175 | h = h - (float)((int)(ceilf(h) / 360.0)); |
---|
[5010] | 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)); |
---|
[8986] | 182 | switch(i) |
---|
| 183 | { |
---|
[5010] | 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; |
---|
[8986] | 218 | //printf("hsv2rgb::How did I get here?\n"); |
---|
| 219 | // printf("h: %f, s: %f, v: %f; i: %d\n",hin,s,v,i); |
---|
[5010] | 220 | break; |
---|
| 221 | } |
---|
| 222 | } |
---|
[7195] | 223 | RGB = Vector(r,g,b); |
---|
[3543] | 224 | } |
---|
[5011] | 225 | |
---|
| 226 | |
---|
[9755] | 227 | /** |
---|
| 228 | * @returns the maximum of r g and a. |
---|
| 229 | * @param r Red. |
---|
| 230 | * @param g Green |
---|
| 231 | * @param b Blue |
---|
| 232 | */ |
---|
[5011] | 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 | |
---|
[9755] | 245 | /** |
---|
| 246 | * @returns the minimum of r g and a. |
---|
| 247 | * @param r Red. |
---|
| 248 | * @param g Green |
---|
| 249 | * @param b Blue |
---|
| 250 | */ |
---|
[5011] | 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 | |
---|