synfig-core  1.0.3
color/color.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
24 /* ========================================================================= */
25 
26 #ifndef __SYNFIG_COLOR_COLOR_H
27 #define __SYNFIG_COLOR_COLOR_H
28 
29 #include <synfig/color/common.h>
30 
31 namespace synfig {
32 
33 class CairoColor;
34 
39 class Color
40 {
41 public:
43 
44 private:
45  value_type a_, r_, g_, b_;
46 
47 public:
48 
49  static const value_type ceil;
50  static const value_type floor;
51 
52  const String get_string(void) const;
53 
54  inline Color & operator+= (const Color &rhs);
55  inline Color & operator-= (const Color &rhs);
56  inline Color & operator*= (const float &rhs);
57  inline Color & operator/= (const float &rhs);
58 
59  inline Color operator+ (const Color &rhs) const;
60  inline Color operator- (const Color &rhs) const;
61  inline Color operator* (const float &rhs) const;
62  inline Color operator/ (const float &rhs) const;
63  inline bool operator== (const Color &rhs) const;
64  inline bool operator!= (const Color &rhs) const;
65  inline Color operator- () const;
67  inline Color operator~() const;
68 
69  inline bool is_valid() const;
70 
71  inline Color premult_alpha() const;
72  inline Color demult_alpha() const;
73 
74 public:
75  // ETL/trunk/ETL/_gaussian.h does:
76  // SR1=SR2=SR3=typename T::value_type();
77  // and expects that to give it initialized colors
78  // Otherwise the 'gaussian' blur type is random.
79  inline Color();
80  inline Color(const value_type &f);
81  inline Color(int f);
82 
87  inline Color(const value_type& R, const value_type& G,
88  const value_type& B, const value_type& A=1);
89 
92  inline Color(const Color& c, const value_type& A);
93 
95  inline Color(const Color& c);
96 
98  inline Color(const CairoColor& c);
99 
100 #ifdef USE_HALF_TYPE
101  friend class ColorAccumulator;
103  inline Color(const ColorAccumulator& c);
104 #endif
105 
107  //Color(const Color &c) { memcpy((void*)this, (const void*)&c, sizeof(Color)); }
108 
109  /*const Color &operator=(const value_type &i)
110  {
111  r_ = g_ = b_ = a_ = i;
112  return *this;
113  }*/
114  //Color& operator=(const Color &c) { memcpy((void*)this, (const void*)&c, sizeof(Color)); return *this; }
115 
117  const value_type& get_r()const { return r_; }
118 
120  const value_type& get_g()const { return g_; }
121 
123  const value_type& get_b()const { return b_; }
124 
126  const value_type& get_a()const { return a_; }
127 
129  const value_type& get_alpha()const { return get_a(); }
130 
132  static ColorReal hex2real(String s);
133 
135  static const String real2hex(ColorReal c);
136 
138  inline const String get_hex()const;
139 
141  void set_hex(String& hex);
142 
144  Color& set_r(const value_type& x) { r_ = x; return *this; }
145 
147  Color& set_g(const value_type& x) { g_ = x; return *this; }
148 
150  Color& set_b(const value_type& x) { b_ = x; return *this; }
151 
153  Color& set_a(const value_type& x) { a_ = x; return *this; }
154 
156  Color& set_alpha(const value_type& x) { return set_a(x); }
157 
159  inline float get_y() const;
160 
162  inline float get_u() const;
163 
165  inline float get_v() const;
166 
168 
170  inline float get_s() const;
171 
173  inline Color& set_yuv(const float &y, const float &u, const float &v);
174 
176  inline Color& set_y(const float &y);
177 
179  inline Color& set_u(const float &u);
180 
182  inline Color& set_v(const float &v);
183 
185  inline Color& set_uv(const float& u, const float& v);
186 
188 
189  inline Color& set_s(const float &x);
190 
192  inline static Color YUV(const float& y, const float& u,
193  const float& v, const value_type& a=1);
194 
196 
198  inline Angle get_hue() const;
199 
201  inline Angle get_uv_angle() const;
202 
204 
205  inline Color& set_hue(const Angle& theta);
206 
208  inline Color& set_uv_angle(const Angle& theta);
209 
211  inline Color& rotate_uv(const Angle& theta);
212 
214 
217  inline Color& set_yuv(const float& y, const float& s, const Angle& theta);
218 
220 
224  inline static Color YUV(const float& y,
225  const float& s,
226  const Angle& theta,
227  const value_type& a=1);
228 
229 
231  Color clamped() const;
232 
234  Color clamped_negative() const;
235 
236  /* Preset Colors */
237 
239 
240 #ifdef HAS_VIMAGE
241  static inline Color alpha() { return Color(0,0,0,0.0000001f); }
242 #else
243  static inline Color alpha() { return Color(0,0,0,0); }
244 #endif
245  static inline Color black() { return Color(0,0,0); }
246  static inline Color white() { return Color(1,1,1); }
247  static inline Color gray() { return Color(0.5f,0.5f,0.5f); }
248  static inline Color magenta() { return Color(1,0,1); }
249  static inline Color red() { return Color(1,0,0); }
250  static inline Color green() { return Color(0,1,0); }
251  static inline Color blue() { return Color(0,0,1); }
252  static inline Color cyan() { return Color(0,1,1); }
253  static inline Color yellow() { return Color(1,1,0); }
255 
258  {
278 
282 
285 
286  };
287 
288  /* Other */
289  static Color blend(Color a, Color b,float amount,BlendMethod type=BLEND_COMPOSITE);
290 
291  static bool is_onto(BlendMethod x)
292  {
293  return x==BLEND_BRIGHTEN
294  || x==BLEND_DARKEN
295  || x==BLEND_ADD
296  || x==BLEND_SUBTRACT
297  || x==BLEND_MULTIPLY
298  || x==BLEND_DIVIDE
299  || x==BLEND_COLOR
300  || x==BLEND_HUE
301  || x==BLEND_SATURATION
302  || x==BLEND_LUMINANCE
303  || x==BLEND_ONTO
304  || x==BLEND_STRAIGHT_ONTO
305  || x==BLEND_SCREEN
306  || x==BLEND_OVERLAY
307  || x==BLEND_DIFFERENCE
308  || x==BLEND_HARD_LIGHT
309  ;
310  }
311 
313  static bool is_straight(BlendMethod x)
314  {
315  return x==BLEND_STRAIGHT
316  || x==BLEND_STRAIGHT_ONTO
318  ;
319  }
320 /*protected:
321 
322  value_type& operator[](const int i)
323  {
324  assert(i>=0);
325  assert(i<(signed)(sizeof(Color)/sizeof(value_type)));
326  return (&r_)[i];
327  }
328 
329  const value_type& operator[](const int i)const
330  {
331  assert(i>=0);
332  assert(i<(signed)(sizeof(Color)/sizeof(value_type)));
333  return (&r_)[i];
334  }
335 */
336 }; // END of class Color
337 
338 } // synfig namespace
339 
340 #include "color.hpp"
341 
342 #endif // __SYNFIG_COLOR_COLOR_H
343