Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5010 was 5010, checked in by bensch, 19 years ago

orxonox/trunk: better color-transformation-algorithms

File size: 3.0 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   this code has been borrowed from:
16   http://www.easyrgb.com/math.php
17*/
18
19//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
20
21#include "color.h"
22#include "vector.h"
23
24using namespace std;
25
26
27
28
29#include <stdio.h>
30
31// Needed by rgb2hsv()
32float Color::maxrgb(float r, float g, float b)
33{
34  float max;
35
36  if( r > g)
37    max = r;
38  else
39    max = g;
40  if( b > max )
41    max = b;
42  return( max );
43}
44
45
46// Needed by rgb2hsv()
47float Color::minrgb(float r,float g,float b)
48{
49  float min;
50
51  if( r < g)
52    min = r;
53  else
54    min = g;
55  if( b < min )
56    min = b;
57  return( min );
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*/
66Vector 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*/
112Vector 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}
Note: See TracBrowser for help on using the repository browser.