Changeset 5010 in orxonox.OLD for orxonox/trunk/src/lib/util
- Timestamp:
- Aug 14, 2005, 3:48:22 AM (19 years ago)
- Location:
- orxonox/trunk/src/lib/util
- Files:
-
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/util/color.cc
r5009 r5010 20 20 21 21 #include "color.h" 22 #include "vector.h" 22 23 23 24 using namespace std; 24 25 25 Vector Color::RGBtoHSV (Vector RGB) 26 27 28 29 #include <stdio.h> 30 31 // Needed by rgb2hsv() 32 float Color::maxrgb(float r, float g, float b) 26 33 { 27 Vector HSV;34 float max; 28 35 29 float var_Min = min( RGB.x, RGB.y, RGB.z ); //Min. value of RGB 30 float var_Max = max( RGB.x, RGB.y, RGB.z ); //Max. value of RGB 31 float del_Max = var_Max - var_Min; //Delta RGB value 32 33 flaot V = var_Max; 34 35 if ( del_Max == 0 ) //This is a gray, no chroma... 36 { 37 HSV.x = 0 //HSV results = 0 ÷ 1 38 HSV.y = 0 39 } 40 else //Chromatic data... 41 { 42 HSV.y = del_Max / var_Max; 43 44 float del_R = ( ( ( var_Max - RGB.x ) / 6 ) + ( del_Max / 2 ) ) / del_Max; 45 float del_G = ( ( ( var_Max - RGB.y ) / 6 ) + ( del_Max / 2 ) ) / del_Max; 46 float del_B = ( ( ( var_Max - RGB.z ) / 6 ) + ( del_Max / 2 ) ) / del_Max; 47 48 if ( RGB.x == var_Max ) 49 HSV.x = del_B - del_G; 50 else if ( var_G == var_Max ) 51 HSV.x = ( 1 / 3 ) + del_R - del_B; 52 else if ( var_B == var_Max ) 53 HSV.z = ( 2 / 3 ) + del_G - del_R; 54 55 if ( HSV.x < 0 ) ; HSV.x += 1; 56 if ( HSV.x > 1 ) ; HSV.x -= 1; 57 } 58 return HSV; 36 if( r > g) 37 max = r; 38 else 39 max = g; 40 if( b > max ) 41 max = b; 42 return( max ); 59 43 } 60 44 61 45 62 Vector Color::HSVtoRGB (Vector HSV) 46 // Needed by rgb2hsv() 47 float Color::minrgb(float r,float g,float b) 63 48 { 49 float min; 64 50 51 if( r < g) 52 min = r; 53 else 54 min = g; 55 if( b < min ) 56 min = b; 57 return( min ); 65 58 } 59 60 61 /* Taken from "Fund'l of 3D Computer Graphics", Alan Watt (1989) 62 Assumes (r,g,b) range from 0.0 to 1.0 63 Sets h in degrees: 0.0 to 360.; 64 s,v in [0.,1.] 65 */ 66 Vector Color::RGBtoHSV(const Vector& RGB) 67 { 68 float r = RGB.x; 69 float g = RGB.y; 70 float b = RGB.z; 71 72 float h=0,s=1.0,v=1.0; 73 float max_v,min_v,diff,r_dist,g_dist,b_dist; 74 float undefined = 0.0; 75 76 max_v = maxrgb(r,g,b); 77 min_v = minrgb(r,g,b); 78 diff = max_v - min_v; 79 v = max_v; 80 81 if( max_v != 0 ) 82 s = diff/max_v; 83 else 84 s = 0.0; 85 if( s == 0 ) 86 h = undefined; 87 else { 88 r_dist = (max_v - r)/diff; 89 g_dist = (max_v - g)/diff; 90 b_dist = (max_v - b)/diff; 91 if( r == max_v ) 92 h = b_dist - g_dist; 93 else 94 if( g == max_v ) 95 h = 2 + r_dist - b_dist; 96 else 97 if( b == max_v ) 98 h = 4 + g_dist - r_dist; 99 else 100 printf("rgb2hsv::How did I get here?\n"); 101 h *= 60; 102 if( h < 0) 103 h += 360.0; 104 } 105 return Vector(h, s, v); 106 } 107 108 /* Taken from "Fund'l of 3D Computer Graphics", Alan Watt (1989) 109 Assumes H in degrees, s,v in [0.,1.0]; 110 (r,g,b) range from 0.0 to 1.0 111 */ 112 Vector Color::HSVtoRGB(const Vector& HSV) 113 { 114 float h = HSV.x; 115 float s = HSV.y; 116 float v = HSV.z; 117 float r=0, g=0, b=0; 118 float f,p,q,t; 119 int i; 120 121 if( s == 0 ) { 122 r = v; 123 g = v; 124 b = v; 125 } 126 else { 127 if(h == 360.) 128 h = 0.0; 129 h /= 60.; 130 i = (int) h; 131 f = h - i; 132 p = v*(1-s); 133 q = v*(1-(s*f)); 134 t = v*(1-s*(1-f)); 135 switch(i) { 136 case 0: 137 r = v; 138 g = t; 139 b = p; 140 break; 141 case 1: 142 r = q; 143 g = v; 144 b = p; 145 break; 146 case 2: 147 r = p; 148 g = v; 149 b = t; 150 break; 151 case 3: 152 r = p; 153 g = q; 154 b = v; 155 break; 156 case 4: 157 r = t; 158 g = p; 159 b = v; 160 break; 161 case 5: 162 r = v; 163 g = p; 164 b = q; 165 break; 166 default: 167 r = 1.0; 168 g = 1.0; 169 b = 1.0; 170 //printf("hsv2rgb::How did I get here?\n"); 171 // printf("h: %f, s: %f, v: %f; i: %d\n",hin,s,v,i); 172 break; 173 } 174 } 175 return Vector(r,g,b); 176 } -
orxonox/trunk/src/lib/util/color.h
r5009 r5010 15 15 class Color 16 16 { 17 Vector RGBtoHSV (Vector RGB); 18 Vector HSVtoRGB (Vector HSV); 17 public: 18 static Vector RGBtoHSV (const Vector& RGB); 19 static Vector HSVtoRGB (const Vector& HSV); 20 21 static float minrgb(float r, float g, float b); 22 static float maxrgb(float r, float g, float b); 19 23 }; 20 24
Note: See TracChangeset
for help on using the changeset viewer.