Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the gui-branche back.
merged with command:
svn merge -r8520:HEAD https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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
29/**
30 * @brief transforms from RGB to HSVtoRGB
31 * @param RGB: the RGB-color [0-1]
32 * @returns HSV: with values (h[0-360(degree)],s[0,1],v[0,1])
33 */
34Vector Color::RGBtoHSV(const Vector& RGB)
35{
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 */
46void Color::RGBtoHSV(const Vector& RGB, Vector& HSV)
47{
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");
81    h *= 60.;
82    if( h < 0)
83      h += 360.0;
84  }
85  HSV = Vector(h, s, v);
86}
87
88/**
89 * @brief converts a Color from HSV to RGBtoHSV
90 * @param HSV: Vector with values (h[0-360(degree)],s[0,1],v[0,1])
91 * @returns RGB: Vector [0-1]
92 */
93Vector Color::HSVtoRGB(const Vector& HSV)
94{
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 */
105void Color::HSVtoRGB(const Vector& HSV, Vector& RGB)
106{
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;
113
114  if( s == 0 ) {
115    r = v;
116    g = v;
117    b = v;
118  }
119  else {
120   if(h >= 360.)
121     h = h - (float)((int)(ceilf(h) / 360.0));
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  }
168  RGB = Vector(r,g,b);
169}
170
171
172// Needed by rgb2hsv()
173float 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()
187float 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
Note: See TracBrowser for help on using the repository browser.