Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: nicer color in the debug of the OBBTreeNode

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