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