RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
color.h
Go to the documentation of this file.
1 /*
2 // Copyright 2007 Alexandros Panagopoulos
3 //
4 // This software is distributed under the terms of the GNU Lesser General Public Licence
5 //
6 // This file is part of Be3D library.
7 //
8 // Be3D is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // Be3D is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with Be3D. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef _COLOR_H2397_INCLUDED_
23 #define _COLOR_H2397_INCLUDED_
24 
25 #pragma once
26 
27 #include <math.h>
28 
29 class Color {
30  public:
31  float r,g,b;
32 
33  public:
34  Color() : r(0), g(0), b(0) { };
35  Color(float _x, float _y, float _z) : r(_x), g(_y), b(_z) { };
36  Color(const Color& v) : r(v.r), g(v.g), b(v.b) { };
37  Color(const unsigned char *c) : r((float)c[0]/255.0f), g((float)c[1]/255.0f), b((float)c[2]/255.0f) { }
38 
39  inline float length() const { return sqrtf(r*r + g*g + b*b); }
40  //inline float length2() const { return r*r + g*g + b*b; }
41  //inline void normalize() { *this /= length(); }
42  inline float avg() const { return (r + g + b)/3.0f; }
43 
44  // products (not as operators, to avoid confusion)
45  //inline float dot(const Color& v) { return r*v.r + g*v.g + b*v.b; }
46  //inline Color cross(const Color& v) { return Color(g*v.b-v.g*b, b*v.r-v.b*r, r*v.g-v.r*g); }
47 
48  inline Color compProduct(const Color& cl) { return Color(r*cl.r, g*cl.g, b*cl.b); }
49  inline void clampTop() { if (r>1) r=1; if (g>1) g=1; if (b>1) b=1; }
50  inline void clampBottom() { if (r<0) r=0; if (g<0) g=0; if (b<0) b=0; }
51  inline void clamp() { clampTop(); clampBottom(); }
52 
53  // unary operators
54  friend inline Color operator - (const Color& v) { return Color(-v.r, -v.g, -v.b); }
55 
56  // scalar operators
57  friend inline Color operator * (const Color& v, float f) { return Color(v.r*f, v.g*f, v.b*f); };
58  friend inline Color operator * (float f, const Color& v) { return Color(v.r*f, v.g*f, v.b*f); };
59  friend inline Color operator / (const Color& v, float f) { return Color(v.r/f, v.g/f, v.b/f); };
60  friend inline Color operator / (float f, const Color& v) { return Color(v.r/f, v.g/f, v.b/f); };
61  inline Color& operator *= (float f) { r*=f; g*=f; b*=f; return *this; };
62  inline Color& operator /= (float f) { r/=f; g/=f; b/=f; return *this; };
63 
64  // vector operators
65  inline Color& operator += (const Color& v) { r+=v.r; g+=v.g; b+=v.b; return *this; };
66  inline Color& operator -= (const Color& v) { r-=v.r; g-=v.g; b-=v.b; return *this; };
67  friend inline Color operator + (const Color& v1, const Color& v2) { return Color(v1.r+v2.r, v1.g+v2.g, v1.b+v2.b); };
68  friend inline Color operator - (const Color& v1, const Color& v2) { return Color(v1.r-v2.r, v1.g-v2.g, v1.b-v2.b); };
69 
70  // equality operators
71  inline bool operator == (const Color& v) { return (v.r == r && v.g == g && v.b == b); }
72  inline bool operator != (const Color& v) { return (v.r == r && v.g == g && v.b == b); }
73  inline bool equalsTheta (const Color& v, float theta) { return ((fabs(v.r-r)<theta) && (fabs(v.g-g)<theta) && (fabs(v.b-b)<theta)); }
74 
75  // r,g,b values are from 0 to 1
76  // h = [0,360], s = [0,1], v = [0,1]
77  // if s == 0, then h = 0 //changed from -1 (undefined)
78  inline void toHSV(float *h, float *s, float *v ) {
79  float min, max, delta;
80  min = (r<g)?r:g;
81  min = (min<b)?min:b;//MIN( r, g, b );
82  max = (r>g)?r:g;
83  max = (max>b)?max:b;//MAX( r, g, b );
84  *v = max; // v
85  delta = max - min;
86  if ( max != 0 )
87  *s = delta / max; // s
88  else {
89  *s = 0;
90  *h = 0; //-1;
91  return;
92  }
93  if ( r == max )
94  *h = ( g - b ) / delta; // between yellow & magenta
95  else if ( g == max )
96  *h = 2 + ( b - r ) / delta; // between cyan & yellow
97  else
98  *h = 4 + ( r - g ) / delta; // between magenta & cyan
99  *h *= 60; // degrees
100  if ( *h < 0 )
101  *h += 360;
102  }
103 
104  static inline Color fromHSV(float h, float s, float v) {
105  Color c;
106  int i;
107  float f, p, q, t;
108 
109  if ( s == 0 ) {
110  // achromatic (grey)
111  c.r = c.g = c.b = v;
112  return c;
113  }
114 
115  h /= 60; // sector 0 to 5
116  i = (int)floor(h);
117  f = h - i; // factorial part of h
118  p = v * ( 1 - s );
119  q = v * ( 1 - s * f );
120  t = v * ( 1 - s * ( 1 - f ) );
121 
122  switch ( i ) {
123  case 0:
124  c.r = v;
125  c.g = t;
126  c.b = p;
127  break;
128  case 1:
129  c.r = q;
130  c.g = v;
131  c.b = p;
132  break;
133  case 2:
134  c.r = p;
135  c.g = v;
136  c.b = t;
137  break;
138  case 3:
139  c.r = p;
140  c.g = q;
141  c.b = v;
142  break;
143  case 4:
144  c.r = t;
145  c.g = p;
146  c.b = v;
147  break;
148  case 5: // case 5:
149  default:
150  c.r = v;
151  c.g = p;
152  c.b = q;
153  break;
154  // default:
155  // ASSERT(0);
156  }
157  return c;
158  }
159 };
160 
161 #endif
Color & operator+=(const Color &v)
Definition: color.h:65
float g
Definition: color.h:31
float avg() const
Definition: color.h:42
Color()
Definition: color.h:34
float length() const
Definition: color.h:39
Color & operator*=(float f)
Definition: color.h:61
void clampBottom()
Definition: color.h:50
Definition: color.h:29
Color compProduct(const Color &cl)
Definition: color.h:48
static Color fromHSV(float h, float s, float v)
Definition: color.h:104
bool equalsTheta(const Color &v, float theta)
Definition: color.h:73
friend Color operator*(const Color &v, float f)
Definition: color.h:57
Color & operator/=(float f)
Definition: color.h:62
bool operator==(const Color &v)
Definition: color.h:71
float b
Definition: color.h:31
bool operator!=(const Color &v)
Definition: color.h:72
void clamp()
Definition: color.h:51
friend Color operator+(const Color &v1, const Color &v2)
Definition: color.h:67
Color & operator-=(const Color &v)
Definition: color.h:66
Color(float _x, float _y, float _z)
Definition: color.h:35
friend Color operator/(const Color &v, float f)
Definition: color.h:59
void toHSV(float *h, float *s, float *v)
Definition: color.h:78
void clampTop()
Definition: color.h:49
Color(const Color &v)
Definition: color.h:36
Color(const unsigned char *c)
Definition: color.h:37
float r
Definition: color.h:31
friend Color operator-(const Color &v)
Definition: color.h:54