00001 #ifndef COLOR_HPP
00002 #define COLOR_HPP
00003
00004 #include <cmath>
00005 #include <algorithm>
00006
00007 class Color {
00008 public:
00009 float R, G, B, A;
00010 public:
00011 Color(int r, int g, int b, int a = 255) : R(r/255), G(g/255), B(b/255), A(a/255) { };
00012 Color(float r, float g, float b, float a = 1.0f) : R(r), G(g), B(b), A(a) { };
00013
00014 Color *toHSV(void) {
00015 float delta, min, max;
00016 float h, s, v;
00017
00018 min = std::min(R, std::min(G, B));
00019 max = std::max(R, std::max(G, B));
00020 delta = max - min;
00021
00022
00023 if(delta == 0.0f)
00024 h = 0.0f;
00025 else
00026 if(R == max)
00027 h = 0.0f + (G - B) / delta;
00028 else if(G == max)
00029 h = 2.0f + (B - R) / delta;
00030 else if(B == max)
00031 h = 4.0f + (R - G) / delta;
00032
00033 h *= 1.0f/6.0f;
00034
00035
00036 if(max == 0.0f)
00037 s = 0.0f;
00038 else
00039 s = delta / max;
00040
00041
00042 v = max;
00043
00044 return new Color(h, s, v);
00045 };
00046
00047 static Color* fromHSV(float h, float s, float v) {
00048 float f, hTemp, p, q, t;
00049 int i;
00050 Color *result;
00051
00052 if(s == 0.0) {
00053
00054 result = new Color(v, v, v);
00055 return result;
00056 }
00057
00058 if(h < 0.0)
00059 h += 1.0;
00060 if(h >= 1.0)
00061 h -= 1.0;
00062
00063
00064 hTemp = h * 6.0;
00065 i = (int)std::floor(hTemp);
00066 f = hTemp - (float)i;
00067
00068 p = v * (1.0 - s);
00069 q = v * (1.0 - (s * f));
00070 t = v * (1.0 - (s * (1.0 - f)));
00071
00072 switch(i) {
00073 case 0: result = new Color(v, t, p); break;
00074 case 1: result = new Color(q, v, p); break;
00075 case 2: result = new Color(p, v, t); break;
00076 case 3: result = new Color(p, q, v); break;
00077 case 4: result = new Color(t, p, v); break;
00078 case 5: result = new Color(v, p, q); break;
00079 }
00080
00081 return result;
00082 }
00083
00084 virtual ~Color(void) { }
00085 };
00086
00087 #endif