My Project
 All Classes Files Functions Variables Enumerations Pages
rgb.h
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Copyright 2009 Tim Day */
3 /* */
4 /* This file is part of Fracplanet */
5 /* */
6 /* Fracplanet 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 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* Fracplanet is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with Fracplanet. If not, see <http://www.gnu.org/licenses/>. */
18 /**************************************************************************/
19 
20 /* Copyright (C) 1998,1999 T Day */
21 
26 #ifndef _image_rgb_h_
27 #define _image_rgb_h_
28 
34 template <typename T> class RGBA
35 {
36  public:
38 
39  T r;
40  T g;
41  T b;
42  T a;
44 
46  /* NB There are no default values.
47  */
48  RGBA()
49  {}
50 
52  RGBA(const RGBA& c)
53  :r(c.r)
54  ,g(c.g)
55  ,b(c.b)
56  ,a(c.a)
57  {}
58 
60  RGBA(T vr,T vg,T vb,T va)
61  :r(vr)
62  ,g(vg)
63  ,b(vb)
64  ,a(va)
65  {}
66 
68  void operator+=(const RGBA& v)
69  {
70  r+=v.r;
71  g+=v.g;
72  b+=v.b;
73  a+=v.a;
74  }
75 
77  void operator-=(const RGBA& v)
78  {
79  r-=v.r;
80  g-=v.g;
81  b-=v.b;
82  a-=v.a;
83  }
84 };
85 
87 template <typename T> inline bool operator==(const RGBA<T>& a,const RGBA<T>& b)
88 {
89  return (a.r==b.r && a.g==b.g && a.b==b.b && a.a==b.a);
90 }
91 
93 template <typename T> inline bool operator!=(const RGBA<T>& a,const RGBA<T>& b)
94 {
95  return (a.r!=b.r || a.g!=b.g || a.b!=b.b || a.a!=b.a);
96 }
97 
99 template <typename T> inline RGBA<T> operator+(const RGBA<T>& a,const RGBA<T>& b)
100 {
101  return RGBA<T>
102  (
103  a.r+b.r,
104  a.g+b.g,
105  a.b+b.b,
106  a.a+b.a
107  );
108 }
109 
111 template <typename T> inline RGBA<T> operator-(const RGBA<T>& c)
112 {
113  return RGBA<T>
114  (
115  -c.r,
116  -c.g,
117  -c.b,
118  -c.a
119  );
120 }
121 
123 template <typename T> inline RGBA<T> operator-(const RGBA<T>& a,const RGBA<T>& b)
124 {
125  return RGBA<T>
126  (
127  a.r-b.r,
128  a.g-b.g,
129  a.b-b.b,
130  a.a-b.a
131  );
132 }
133 
134 
136 class ByteRGBA : public RGBA<uchar>
137 {
138  public:
139 
142  :RGBA<uchar>()
143  {}
144 
147  :RGBA<uchar>(c)
148  {}
149 
151  ByteRGBA(uchar vr,uchar vg,uchar vb,uchar va)
152  :RGBA<uchar>(vr,vg,vb,va)
153  {}
154 
156 
158  explicit ByteRGBA(const RGBA<float>& c)
159  :RGBA<uchar>
160  (
161  static_cast<uchar>(255.0*clamped(c.r,0.0f,1.0f)),
162  static_cast<uchar>(255.0*clamped(c.g,0.0f,1.0f)),
163  static_cast<uchar>(255.0*clamped(c.b,0.0f,1.0f)),
164  static_cast<uchar>(255.0*clamped(c.a,0.0f,1.0f))
165  )
166  {}
167 
168  std::ostream& write(std::ostream&) const;
169  const std::string format_comma() const;
170 };
171 
173 class FloatRGBA : public RGBA<float>
174 {
175 public:
176 
178  /* NB There are no default values.
179  */
181  {}
182 
185  :RGBA<float>(c)
186  {}
187 
189  FloatRGBA(float vr,float vg,float vb,float va)
190  :RGBA<float>(vr,vg,vb,va)
191  {}
192 
194 
196  explicit FloatRGBA(const RGBA<uchar>& c)
197  :RGBA<float>(c.r/255.0f,c.g/255.0f,c.b/255.0,c.a/255.0)
198  {}
199 
201  std::ostream& write(std::ostream&) const;
202 
203  const std::string format_pov_rgb() const;
204 
205  const std::string format_pov_rgbf() const;
206 };
207 
209 inline FloatRGBA operator*(float k,const FloatRGBA& c)
210 {
211  return FloatRGBA
212  (
213  k*c.r,
214  k*c.g,
215  k*c.b,
216  k*c.a
217  );
218 }
219 
221 inline FloatRGBA operator*(const FloatRGBA& c,float k)
222 {
223  return FloatRGBA
224  (
225  k*c.r,
226  k*c.g,
227  k*c.b,
228  k*c.a
229  );
230 }
231 
233 inline FloatRGBA operator/(const FloatRGBA& c,float k)
234 {
235  return FloatRGBA
236  (
237  c.r/k,
238  c.g/k,
239  c.b/k,
240  c.a/k
241  );
242 }
243 
244 
246 
248 inline FloatRGBA operator*(const FloatRGBA& a,const FloatRGBA& b)
249 {
250  return FloatRGBA
251  (
252  a.r*b.r,
253  a.g*b.g,
254  a.b*b.b,
255  a.a*b.a
256  );
257 }
258 
259 inline std::ostream& operator<<(std::ostream& out,const FloatRGBA& c)
260 {
261  return c.write(out);
262 }
263 
264 #endif