[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 | //! 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 | |
---|
[8448] | 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 | |
---|
[8619] | 67 | |
---|
| 68 | |
---|
[5011] | 69 | /** |
---|
[7195] | 70 | * @brief transforms from RGB to HSVtoRGB |
---|
| 71 | * @param RGB: the RGB-color [0-1] |
---|
[5011] | 72 | * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 73 | */ |
---|
[5010] | 74 | Vector Color::RGBtoHSV(const Vector& RGB) |
---|
| 75 | { |
---|
[7195] | 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 | { |
---|
[5010] | 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; |
---|
[8986] | 107 | else |
---|
| 108 | { |
---|
[5010] | 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; |
---|
[8986] | 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"); |
---|
[5011] | 122 | h *= 60.; |
---|
[5010] | 123 | if( h < 0) |
---|
| 124 | h += 360.0; |
---|
[5009] | 125 | } |
---|
[7195] | 126 | HSV = Vector(h, s, v); |
---|
[3365] | 127 | } |
---|
[1853] | 128 | |
---|
[5011] | 129 | /** |
---|
[7195] | 130 | * @brief converts a Color from HSV to RGBtoHSV |
---|
[5011] | 131 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 132 | * @returns RGB: Vector [0-1] |
---|
| 133 | */ |
---|
[5010] | 134 | Vector Color::HSVtoRGB(const Vector& HSV) |
---|
[3543] | 135 | { |
---|
[7195] | 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 | { |
---|
[5010] | 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; |
---|
[5009] | 154 | |
---|
[8986] | 155 | if( s == 0 ) |
---|
| 156 | { |
---|
[5010] | 157 | r = v; |
---|
| 158 | g = v; |
---|
| 159 | b = v; |
---|
| 160 | } |
---|
[8986] | 161 | else |
---|
| 162 | { |
---|
| 163 | if(h >= 360.) |
---|
| 164 | h = h - (float)((int)(ceilf(h) / 360.0)); |
---|
[5010] | 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)); |
---|
[8986] | 171 | switch(i) |
---|
| 172 | { |
---|
[5010] | 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; |
---|
[8986] | 207 | //printf("hsv2rgb::How did I get here?\n"); |
---|
| 208 | // printf("h: %f, s: %f, v: %f; i: %d\n",hin,s,v,i); |
---|
[5010] | 209 | break; |
---|
| 210 | } |
---|
| 211 | } |
---|
[7195] | 212 | RGB = Vector(r,g,b); |
---|
[3543] | 213 | } |
---|
[5011] | 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 | |
---|