Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/color.cc @ 7195

Last change on this file since 7195 was 7195, checked in by bensch, 18 years ago

orxonox/trunk: performance issues

File size: 3.4 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "color.h"
19#include <stdio.h>
20
21using namespace std;
22
23
24/**
25 * @brief transforms from RGB to HSVtoRGB
26 * @param RGB: the RGB-color [0-1]
27 * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1])
28 */
29Vector Color::RGBtoHSV(const Vector& RGB)
30{
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 */
41void Color::RGBtoHSV(const Vector& RGB, Vector& HSV)
42{
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");
76    h *= 60.;
77    if( h < 0)
78      h += 360.0;
79  }
80  HSV = Vector(h, s, v);
81}
82
83/**
84 * @brief converts a Color from HSV to RGBtoHSV
85 * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1])
86 * @returns RGB: Vector [0-1]
87 */
88Vector Color::HSVtoRGB(const Vector& HSV)
89{
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 */
100void Color::HSVtoRGB(const Vector& HSV, Vector& RGB)
101{
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;
108
109  if( s == 0 ) {
110    r = v;
111    g = v;
112    b = v;
113  }
114  else {
115   if(h == 360.)
116     h = 0.0;
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  }
163  RGB = Vector(r,g,b);
164}
165
166
167// Needed by rgb2hsv()
168float 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()
182float 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
Note: See TracBrowser for help on using the repository browser.