Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged gui back to the trunk
merged with command
merge -r8377:HEAD https://svn.orxonox.net/orxonox/branches/gui .

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