Templates -- Meow  1.1.2
不能,也不應該先編譯成obj-file的templates
HSV_Space.h
Go to the documentation of this file.
1 #ifndef colors_HSV_Space_H__
2 #define colors_HSV_Space_H__
3 
4 #include "Color3_Space.h"
5 #include "../geo/Vectors.h"
6 
7 #include "RGB_Space.h"
8 #include "YUV_Space.h"
9 #include "HSL_Space.h"
10 #include "../math/utility.h"
11 
12 #include <cstdlib>
13 
14 namespace meow {
15 
23 class HSVf_Space: public Color3_Space<double> {
24 public:
26  Vector3D<double>(PI*2.0, 1.0, 1.0),
27  Vector3D<double>( 0.0, 0.0, 0.0)) {
28  }
29  HSVf_Space(double c): Color3_Space<double>(Vector3D<double>( 0.0, 0.0, 0.0),
30  Vector3D<double>(PI*2.0, 1.0, 1.0),
31  Vector3D<double>( c, c, c))
32  { }
34  Color3_Space<double>(Vector3D<double>(0.0, 0.0, 0.0),
35  Vector3D<double>(1.0, 1.0, 1.0),
36  Vector3D<double>(v)) {
37  }
38  HSVf_Space(HSV_Space const& b): Color3_Space<double>(b) {
39  }
41  }
42  double const& hsvMin(size_t i) const { return min(i); }
43  double const& hMin( ) const { return min(0); }
44  double const& sMin( ) const { return min(1); }
45  double const& vMin( ) const { return min(2); }
46  double const& hsvMax(size_t i) const { return max(i); }
47  double const& hMax( ) const { return max(0); }
48  double const& sMax( ) const { return max(1); }
49  double const& vMax( ) const { return max(2); }
50  double const& hsv(size_t i) const { return val(i); }
51  double const& h( ) const { return hsv(0); }
52  double const& s( ) const { return hsv(1); }
53  double const& v( ) const { return hsv(2); }
54  double const& hsv(size_t i, double c) { return val(i, c); }
55  double const& h( double c) { return hsv(0, c); }
56  double const& s( double c) { return hsv(1, c); }
57  double const& v( double c) { return hsv(2, c); }
58  double& hsvGet(size_t i) { return valGet(i); }
59  double& hGet( ) { return hsvGet(0); }
60  double& sGet( ) { return hsvGet(1); }
61  double& vGet( ) { return hsvGet(2); }
63  copyFrom(b);
64  return *this;
65  }
66  HSVf_Space operator+(HSVf_Space const& b) const {
67  return HSVf_Space(val_ + b.val_);
68  }
69  HSVf_Space operator-(HSVf_Space const& b) const {
70  return HSVf_Space(val_ - b.val_);
71  }
72  HSVf_Space operator*(double const& c) const {
73  return HSVf_Space(val_ * c);
74  }
75  HSVf_Space operator/(double const& c) const {
76  return HSVf_Space(val_ / c);
77  }
78  double operator*(HSVf_Space const& b) const {
79  return val_ * b.val_;
80  }
81 };
82 
86 inline void colorTransformate(RGBf_Space const& rgb, HSVf_Space* hsv) {
87  double r = normalize(rgb.rMin(), rgb.rMax(), rgb.r());
88  double g = normalize(rgb.gMin(), rgb.gMax(), rgb.g());
89  double b = normalize(rgb.bMin(), rgb.bMax(), rgb.b());
90  double mx = std::max(std::max(r, g), b);
91  double mn = std::min(std::min(r, g), b);
92  double h, s, v;
93  if (mx == mn ) h = 0;
94  else if(mx == r && g >= b) h = PI/3.0 * (g-b) / (mx-mn);
95  else if(mx == r && g < b) h = PI/3.0 * (g-b) / (mx-mn) + PI * 2.0;
96  else if(mx == g ) h = PI/3.0 * (b-r) / (mx-mn) + PI/3.0*2.0;
97  else h = PI/3.0 * (r-g) / (mx-mn) + PI/3.0*4.0;
98  if(mx == 0) s = 0;
99  else s = 1 - mn / mx;
100  v = mx;
101  hsv->h(h);
102  hsv->s(s);
103  hsv->v(v);
104 }
105 
109 inline void colorTransformate(YUVf_Space const& yuv, HSVf_Space* hsv) {
110  RGBf_Space tmp;
111  colorTransformate( yuv, &tmp);
112  colorTransformate(*tmp, hsv);
113 }
114 
118 inline void colorTransformate(HSLf_Space const& hsl, HSVf_Space* hsv) {
119  RGBf_Space tmp;
120  colorTransformate( hsl, &tmp);
121  colorTransformate(*tmp, hsv);
122 }
123 
127 inline void colorTransformate(HSVf_Space const& hsv, RGBf_Space* rgb) {
128  double h = normalize(hsv.hMin(), hsv.hMax(), hsv.h()) * 360;
129  double s = normalize(hsv.sMin(), hsv.sMax(), hsv.s());
130  double v = normalize(hsv.vMin(), hsv.vMax(), hsv.v());
131  int hi = (int)h / 60 % 6;
132  double f = h / 60.0 - hi;
133  double p = v * (1 - s);
134  double q = v * (1 - f * s);
135  double t = v * (1 - (1 - f) * s);
136  double r, g, b;
137  if (hi == 0){ r = v; g = t; b = p; }
138  else if(hi == 1){ r = q; g = v; b = p; }
139  else if(hi == 2){ r = p; g = v; b = t; }
140  else if(hi == 3){ r = p; g = q; b = v; }
141  else if(hi == 4){ r = t; g = p; b = v; }
142  else { r = v; g = p; b = q; }
143  rgb->r(denormalize(rgb->rMin(), rgb->rMax(), r));
144  rgb->g(denormalize(rgb->gMin(), rgb->gMax(), g));
145  rgb->b(denormalize(rgb->bMin(), rgb->bMax(), b));
146 }
147 
151 inline void colorTransformate(HSVf_Space const& hsv, YUVf_Space* yuv) {
152  RGBf_Space tmp;
153  colorTransformate( hsv, &tmp);
154  colorTransformate(*tmp, yuv);
155 }
156 
160 inline void colorTransformate(HSVf_Space const& hsv, HSLf_Space* hsl) {
161  RGBf_Space tmp;
162  colorTransformate( hsv, &tmp);
163  colorTransformate(*tmp, hsl);
164 }
165 
169 inline void colorTransformate(HSVf_Space const& hsv, RGBi_Space* rgb) {
170  RGBf_Space tmp;
171  colorTransformate(hsv, &tmp);
172  rgb->copyFrom(tmp);
173 }
174 
175 
179 inline void colorTransformate(RGBi_Space const& rgb, HSVf_Space* hsv) {
180  RGBf_Space tmp;
181  tmp.copyFrom(rgb);
182  colorTransformate(rgb, hsv);
183 }
184 
185 } // meow
186 
187 
188 #endif // colors_HSV_Space_H__