[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 | |
---|
| 22 | void Color::debug() const |
---|
| 23 | { |
---|
| 24 | printf("r:%0.2f g:%0.2f, b:%0.2f, a:%0.2f\n", r(), g(), b(), a()); |
---|
| 25 | } |
---|
| 26 | |
---|
[8619] | 27 | |
---|
| 28 | |
---|
[5011] | 29 | /** |
---|
[7195] | 30 | * @brief transforms from RGB to HSVtoRGB |
---|
| 31 | * @param RGB: the RGB-color [0-1] |
---|
[5011] | 32 | * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 33 | */ |
---|
[5010] | 34 | Vector Color::RGBtoHSV(const Vector& RGB) |
---|
| 35 | { |
---|
[7195] | 36 | Vector HSV; |
---|
| 37 | RGBtoHSV(RGB, HSV); |
---|
| 38 | return HSV; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * @brief transforms from RGB to HSVtoRGB |
---|
| 43 | * @param RGB: the RGB-color [0-1] |
---|
| 44 | * @param HSV: with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 45 | */ |
---|
| 46 | void Color::RGBtoHSV(const Vector& RGB, Vector& HSV) |
---|
| 47 | { |
---|
[5010] | 48 | float r = RGB.x; |
---|
| 49 | float g = RGB.y; |
---|
| 50 | float b = RGB.z; |
---|
| 51 | |
---|
| 52 | float h=0,s=1.0,v=1.0; |
---|
| 53 | float max_v,min_v,diff,r_dist,g_dist,b_dist; |
---|
| 54 | float undefined = 0.0; |
---|
| 55 | |
---|
| 56 | max_v = maxrgb(r,g,b); |
---|
| 57 | min_v = minrgb(r,g,b); |
---|
| 58 | diff = max_v - min_v; |
---|
| 59 | v = max_v; |
---|
| 60 | |
---|
| 61 | if( max_v != 0 ) |
---|
| 62 | s = diff/max_v; |
---|
| 63 | else |
---|
| 64 | s = 0.0; |
---|
| 65 | if( s == 0 ) |
---|
| 66 | h = undefined; |
---|
| 67 | else { |
---|
| 68 | r_dist = (max_v - r)/diff; |
---|
| 69 | g_dist = (max_v - g)/diff; |
---|
| 70 | b_dist = (max_v - b)/diff; |
---|
| 71 | if( r == max_v ) |
---|
| 72 | h = b_dist - g_dist; |
---|
| 73 | else |
---|
| 74 | if( g == max_v ) |
---|
| 75 | h = 2 + r_dist - b_dist; |
---|
| 76 | else |
---|
| 77 | if( b == max_v ) |
---|
| 78 | h = 4 + g_dist - r_dist; |
---|
| 79 | else |
---|
| 80 | printf("rgb2hsv::How did I get here?\n"); |
---|
[5011] | 81 | h *= 60.; |
---|
[5010] | 82 | if( h < 0) |
---|
| 83 | h += 360.0; |
---|
[5009] | 84 | } |
---|
[7195] | 85 | HSV = Vector(h, s, v); |
---|
[3365] | 86 | } |
---|
[1853] | 87 | |
---|
[5011] | 88 | /** |
---|
[7195] | 89 | * @brief converts a Color from HSV to RGBtoHSV |
---|
[5011] | 90 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 91 | * @returns RGB: Vector [0-1] |
---|
| 92 | */ |
---|
[5010] | 93 | Vector Color::HSVtoRGB(const Vector& HSV) |
---|
[3543] | 94 | { |
---|
[7195] | 95 | Vector RGB; |
---|
| 96 | HSVtoRGB(HSV, RGB); |
---|
| 97 | return RGB; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * @brief converts a Color from HSV to RGBtoHSV |
---|
| 102 | * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1]) |
---|
| 103 | * @param RGB: Vector [0-1] |
---|
| 104 | */ |
---|
| 105 | void Color::HSVtoRGB(const Vector& HSV, Vector& RGB) |
---|
| 106 | { |
---|
[5010] | 107 | float h = HSV.x; |
---|
| 108 | float s = HSV.y; |
---|
| 109 | float v = HSV.z; |
---|
| 110 | float r=0, g=0, b=0; |
---|
| 111 | float f,p,q,t; |
---|
| 112 | int i; |
---|
[5009] | 113 | |
---|
[5010] | 114 | if( s == 0 ) { |
---|
| 115 | r = v; |
---|
| 116 | g = v; |
---|
| 117 | b = v; |
---|
| 118 | } |
---|
| 119 | else { |
---|
[7919] | 120 | if(h >= 360.) |
---|
| 121 | h = h - (float)((int)(ceilf(h) / 360.0)); |
---|
[5010] | 122 | h /= 60.; |
---|
| 123 | i = (int) h; |
---|
| 124 | f = h - i; |
---|
| 125 | p = v*(1-s); |
---|
| 126 | q = v*(1-(s*f)); |
---|
| 127 | t = v*(1-s*(1-f)); |
---|
| 128 | switch(i) { |
---|
| 129 | case 0: |
---|
| 130 | r = v; |
---|
| 131 | g = t; |
---|
| 132 | b = p; |
---|
| 133 | break; |
---|
| 134 | case 1: |
---|
| 135 | r = q; |
---|
| 136 | g = v; |
---|
| 137 | b = p; |
---|
| 138 | break; |
---|
| 139 | case 2: |
---|
| 140 | r = p; |
---|
| 141 | g = v; |
---|
| 142 | b = t; |
---|
| 143 | break; |
---|
| 144 | case 3: |
---|
| 145 | r = p; |
---|
| 146 | g = q; |
---|
| 147 | b = v; |
---|
| 148 | break; |
---|
| 149 | case 4: |
---|
| 150 | r = t; |
---|
| 151 | g = p; |
---|
| 152 | b = v; |
---|
| 153 | break; |
---|
| 154 | case 5: |
---|
| 155 | r = v; |
---|
| 156 | g = p; |
---|
| 157 | b = q; |
---|
| 158 | break; |
---|
| 159 | default: |
---|
| 160 | r = 1.0; |
---|
| 161 | g = 1.0; |
---|
| 162 | b = 1.0; |
---|
| 163 | //printf("hsv2rgb::How did I get here?\n"); |
---|
| 164 | // printf("h: %f, s: %f, v: %f; i: %d\n",hin,s,v,i); |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | } |
---|
[7195] | 168 | RGB = Vector(r,g,b); |
---|
[3543] | 169 | } |
---|
[5011] | 170 | |
---|
| 171 | |
---|
| 172 | // Needed by rgb2hsv() |
---|
| 173 | float Color::maxrgb(float r, float g, float b) |
---|
| 174 | { |
---|
| 175 | float max; |
---|
| 176 | if( r > g) |
---|
| 177 | max = r; |
---|
| 178 | else |
---|
| 179 | max = g; |
---|
| 180 | if( b > max ) |
---|
| 181 | max = b; |
---|
| 182 | return( max ); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | // Needed by rgb2hsv() |
---|
| 187 | float Color::minrgb(float r,float g,float b) |
---|
| 188 | { |
---|
| 189 | float min; |
---|
| 190 | |
---|
| 191 | if( r < g) |
---|
| 192 | min = r; |
---|
| 193 | else |
---|
| 194 | min = g; |
---|
| 195 | if( b < min ) |
---|
| 196 | min = b; |
---|
| 197 | return( min ); |
---|
| 198 | } |
---|
| 199 | |
---|