1 /* === S Y N F I G ========================================================= */
3 ** \brief Color Class Implementation
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === S T A R T =========================================================== */
25 #ifndef __SYNFIG_COLOR_H
26 #define __SYNFIG_COLOR_H
28 /* === H E A D E R S ======================================================= */
35 #include <synfig/string.h>
38 #include <OpenEXR/half.h>
41 #ifndef SYNFIG_NO_ANGLE
45 /* === M A C R O S ========================================================= */
50 extern "C" { int _isnan(double x); }
55 // For some reason isnan() isn't working on macosx any more.
56 // This is a quick fix.
57 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
61 inline bool isnan(double x) { return x != x; }
62 inline bool isnan(float x) { return x != x; }
63 #define SYNFIG_ISNAN_FIX 1
69 typedef half ColorReal;
71 typedef float ColorReal;
74 static const float EncodeYUV[3][3]=
76 { 0.299f, 0.587f, 0.114f },
77 { -0.168736f, -0.331264f, 0.5f },
78 { 0.5f, -0.418688f, -0.081312f }
81 static const float DecodeYUV[3][3]=
83 { 1.0f, 0.0f, 1.402f },
84 { 1.0f, -0.344136f, -0.714136f },
85 { 1.0f, 1.772f, 0.0f }
88 /* === T Y P E D E F S ===================================================== */
90 /* === C L A S S E S & S T R U C T S ======================================= */
93 class ColorAccumulator;
101 ** Future optimizations: lookup table for sqrt()?
106 typedef ColorReal value_type;
109 value_type a_, r_, g_, b_;
113 const String get_string(void)const;
116 operator+=(const Color &rhs)
126 operator-=(const Color &rhs)
136 operator*=(const float &rhs)
146 operator/=(const float &rhs)
148 const float temp(value_type(1)/rhs);
157 operator+(const Color &rhs)const
158 { return Color(*this)+=rhs; }
161 operator-(const Color &rhs)const
162 { return Color(*this)-=rhs; }
165 operator*(const float &rhs)const
166 { return Color(*this)*=rhs; }
169 operator/(const float &rhs)const
170 { return Color(*this)/=rhs; }
173 operator==(const Color &rhs)const
174 { return r_==rhs.r_ && g_==rhs.g_ && b_==rhs.b_ && a_==rhs.a_; }
177 operator!=(const Color &rhs)const
178 { return r_!=rhs.r_ || g_!=rhs.g_ || b_!=rhs.b_ || a_!=rhs.a_; }
182 { return Color(-r_,-g_,-b_,-a_); }
184 //! Effectively 1.0-color
187 { return Color(1.0f-r_,1.0f-g_,1.0f-b_,a_); }
190 { return !isnan(r_) && !isnan(g_) && !isnan(b_) && !isnan(a_); }
192 Color premult_alpha() const
194 return Color (r_*a_, g_*a_, b_*a_, a_);
197 Color demult_alpha() const
201 const value_type inva = 1/a_;
202 return Color (r_*inva, g_*inva, b_*inva, a_);
203 }else return alpha();
207 Color() /*:r_(0), g_(0), b_(0), a_(0)*/ { }
208 Color(const value_type &f) :a_(f),r_(f), g_(f), b_(f) { }
209 Color(int f) :a_(f),r_(f), g_(f), b_(f) { }
214 ** \param A Opacity(alpha) */
215 Color(const value_type& R, const value_type& G, const value_type& B, const value_type& A=1):
221 /*! \param c Source for color components
222 ** \param A Opacity(alpha) */
223 Color(const Color& c, const value_type& A):
231 Color(const Color& c):
238 friend class ColorAccumulator;
239 //! Convert constructor
240 Color(const ColorAccumulator& c);
244 //Color(const Color &c) { memcpy((void*)this, (const void*)&c, sizeof(Color)); }
246 /*const Color &operator=(const value_type &i)
248 r_ = g_ = b_ = a_ = i;
251 //Color& operator=(const Color &c) { memcpy((void*)this, (const void*)&c, sizeof(Color)); return *this; }
253 //! Returns the RED component
254 const value_type& get_r()const { return r_; }
256 //! Returns the GREEN component
257 const value_type& get_g()const { return g_; }
259 //! Returns the BLUE component
260 const value_type& get_b()const { return b_; }
262 //! Returns the amount of opacity (alpha)
263 const value_type& get_a()const { return a_; }
265 //! Synonym for get_a(). \see get_a()
266 const value_type& get_alpha()const { return get_a(); }
268 //! Converts a 2 character hex string \a s (00-ff) into a ColorReal (0.0-1.0)
269 static ColorReal hex2real(String s);
271 //! Converts a ColorReal \a c (0.0-1.0) into a 2 character hex string (00-ff)
272 static const String real2hex(ColorReal c);
274 //! Returns the color as a 6 character hex sting
275 const String get_hex()const { return String(real2hex(r_)+real2hex(g_)+real2hex(b_)); }
277 //! Sets the color's R, G, and B from a 3 or 6 character hex string
278 void set_hex(String& hex);
280 //! Sets the RED component to \a x
281 Color& set_r(const value_type& x) { r_ = x; return *this; }
283 //! Sets the GREEN component to \a x
284 Color& set_g(const value_type& x) { g_ = x; return *this; }
286 //! Sets the BLUE component to \a x
287 Color& set_b(const value_type& x) { b_ = x; return *this; }
289 //! Sets the opacity (alpha) to \a x
290 Color& set_a(const value_type& x) { a_ = x; return *this; }
292 //! Synonym for set_a(). \see set_a()
293 Color& set_alpha(const value_type& x) { return set_a(x); }
295 //! Returns color's luminance
300 (float)get_r()*EncodeYUV[0][0]+
301 (float)get_g()*EncodeYUV[0][1]+
302 (float)get_b()*EncodeYUV[0][2];
306 //! Returns U component of chromanance
311 (float)get_r()*EncodeYUV[1][0]+
312 (float)get_g()*EncodeYUV[1][1]+
313 (float)get_b()*EncodeYUV[1][2];
317 //! Returns V component of chromanance
322 (float)get_r()*EncodeYUV[2][0]+
323 (float)get_g()*EncodeYUV[2][1]+
324 (float)get_b()*EncodeYUV[2][2];
327 //! Returns the color's saturation
328 /*! This is is the magnitude of the U and V components.
333 const float u(get_u()), v(get_v());
334 return sqrt(u*u+v*v);
337 //! Sets the luminance (\a y) and chromanance (\a u and \a v)
339 set_yuv(const float &y, const float &u, const float &v)
341 set_r(y*DecodeYUV[0][0]+u*DecodeYUV[0][1]+v*DecodeYUV[0][2]);
342 set_g(y*DecodeYUV[1][0]+u*DecodeYUV[1][1]+v*DecodeYUV[1][2]);
343 set_b(y*DecodeYUV[2][0]+u*DecodeYUV[2][1]+v*DecodeYUV[2][2]);
347 //! Sets color luminance
348 Color& set_y(const float &y) { return set_yuv(y,get_u(),get_v()); }
350 //! Set U component of chromanance
351 Color& set_u(const float &u) { return set_yuv(get_y(),u,get_v()); }
353 //! Set V component of chromanance
354 Color& set_v(const float &v) { return set_yuv(get_y(),get_u(),v); }
356 //! Set the U and V components of chromanance
357 Color& set_uv(const float& u, const float& v) { return set_yuv(get_y(),u,v); }
359 //! Sets the color's saturation
362 set_s(const float &x)
364 float u(get_u()), v(get_v());
365 const float s(sqrt(u*u+v*v));
375 //! YUV Color constructor
376 static Color YUV(const float& y, const float& u, const float& v, const value_type& a=1)
377 { return Color().set_yuv(y,u,v).set_a(a); }
379 #ifndef SYNFIG_NO_ANGLE
380 //! Returns the hue of the chromanance
381 /*! This is the angle of the U and V components.
385 { return Angle::tan(get_u(),get_v()); }
387 //! Synonym for get_hue(). \see get_hue()
388 Angle get_uv_angle() const { return get_hue(); }
390 //! Sets the color's hue
391 /*! \see get_hue() */
393 set_hue(const Angle& theta)
395 const float s(get_s());
397 u(s*(float)Angle::sin(theta).get()),
398 v(s*(float)Angle::cos(theta).get());
402 //! Synonym for set_hue(). \see set_hue()
403 Color& set_uv_angle(const Angle& theta) { return set_hue(theta); }
405 //! Rotates the chromanance vector by amount specified by \a theta
406 Color& rotate_uv(const Angle& theta)
408 const float a(Angle::sin(theta).get()), b(Angle::cos(theta).get());
409 const float u(get_u()), v(get_v());
411 return set_uv(b*u-a*v,a*u+b*v);
414 //! Sets the luminance (\a y) and chromanance (\a s and \a theta).
415 /*! \param y Luminance
416 ** \param s Saturation
417 ** \param theta Hue */
418 Color& set_yuv(const float& y, const float& s, const Angle& theta)
423 s*(float)Angle::sin(theta).get(),
424 s*(float)Angle::cos(theta).get()
428 //! YUV color constructor where the chroma is in the saturation/hue form.
429 /*! \param y Luminance
430 ** \param s Saturation
432 ** \param a Opacity (alpha) */
433 static Color YUV(const float& y, const float& s, const Angle& theta, const value_type& a=1)
434 { return Color().set_yuv(y,s,theta).set_a(a); }
438 //! Clamps a color so that its values are in range. Ignores attempting to visualize negative colors.
439 Color clamped()const;
441 //! Clamps a color so that its values are in range.
442 Color clamped_negative()const;
446 //! Preset Color Constructors
449 static inline Color alpha() { return Color(0,0,0,0.0000001f); }
451 static inline Color alpha() { return Color(0,0,0,0); }
453 static inline Color black() { return Color(0,0,0); }
454 static inline Color white() { return Color(1,1,1); }
455 static inline Color gray() { return Color(0.5f,0.5f,0.5f); }
456 static inline Color magenta() { return Color(1,0,1); }
457 static inline Color red() { return Color(1,0,0); }
458 static inline Color green() { return Color(0,1,0); }
459 static inline Color blue() { return Color(0,0,1); }
460 static inline Color cyan() { return Color(0,1,1); }
461 static inline Color yellow() { return Color(1,1,0); }
467 BLEND_COMPOSITE=0, //!< Color A is composited onto B (Taking A's alpha into account)
468 BLEND_STRAIGHT=1, //!< Straight linear interpolation from A->B (Alpha ignored)
469 BLEND_ONTO=13, //!< Similar to BLEND_COMPOSITE, except that B's alpha is maintained
470 BLEND_STRAIGHT_ONTO=21, //!< <deprecated> \writeme
471 BLEND_BEHIND=12, //!< Similar to BLEND_COMPOSITE, except that B is composited onto A.
472 BLEND_SCREEN=16, //!< \writeme
473 BLEND_OVERLAY=20, //!< \writeme
474 BLEND_HARD_LIGHT=17, //!< \writeme
475 BLEND_MULTIPLY=6, //!< Simple A*B.
476 BLEND_DIVIDE=7, //!< Simple B/A
477 BLEND_ADD=4, //!< Simple A+B.
478 BLEND_SUBTRACT=5, //!< Simple A-B.
479 BLEND_DIFFERENCE=18, //!< Simple |A-B|.
480 BLEND_BRIGHTEN=2, //!< If composite is brighter than B, use composite. B otherwise.
481 BLEND_DARKEN=3, //!< If composite is darker than B, use composite. B otherwise.
482 BLEND_COLOR=8, //!< Preserves the U and V channels of color A
483 BLEND_HUE=9, //!< Preserves the angle of the UV vector of color A
484 BLEND_SATURATION=10, //!< Preserves the magnitude of the UV Vector of color A
485 BLEND_LUMINANCE=11, //!< Preserves the Y channel of color A
487 BLEND_ALPHA_BRIGHTEN=14, //!< <deprecated> If A is less opaque than B, use A
488 BLEND_ALPHA_DARKEN=15, //!< <deprecated> If A is more opaque than B, use B
489 BLEND_ALPHA_OVER=19, //!< <deprecated> multiply alphas and then straight blends using the amount
491 BLEND_END=22 //!< \internal
495 static Color blend(Color a, Color b,float amount,BlendMethod type=BLEND_COMPOSITE);
497 static bool is_onto(BlendMethod x)
499 return x==BLEND_BRIGHTEN
507 || x==BLEND_SATURATION
508 || x==BLEND_LUMINANCE
510 || x==BLEND_STRAIGHT_ONTO
513 || x==BLEND_DIFFERENCE
514 || x==BLEND_HARD_LIGHT
519 value_type& operator[](const int i)
522 assert(i<(signed)(sizeof(Color)/sizeof(value_type)));
526 const value_type& operator[](const int i)const
529 assert(i<(signed)(sizeof(Color)/sizeof(value_type)));
533 }; // END of class Color
535 #ifndef USE_HALF_TYPE
536 typedef Color ColorAccumulator;
538 class ColorAccumulator
542 typedef float value_type;
545 value_type a_, r_, g_, b_;
550 operator+=(const ColorAccumulator &rhs)
560 operator-=(const ColorAccumulator &rhs)
570 operator*=(const float &rhs)
580 operator/=(const float &rhs)
582 const float temp(value_type(1)/rhs);
591 operator+(const ColorAccumulator &rhs)const
592 { return Color(*this)+=rhs; }
595 operator-(const ColorAccumulator &rhs)const
596 { return Color(*this)-=rhs; }
599 operator*(const float &rhs)const
600 { return Color(*this)*=rhs; }
603 operator/(const float &rhs)const
604 { return Color(*this)/=rhs; }
607 operator==(const ColorAccumulator &rhs)const
608 { return r_==rhs.r_ && g_==rhs.g_ && b_==rhs.b_ && a_!=rhs.a_; }
611 operator!=(const ColorAccumulator &rhs)const
612 { return r_!=rhs.r_ || g_!=rhs.g_ || b_!=rhs.b_ || a_!=rhs.a_; }
616 { return ColorAccumulator(-r_,-g_,-b_,-a_); }
619 { return !isnan(r_) && !isnan(g_) && !isnan(b_) && !isnan(a_); }
622 ColorAccumulator() { }
627 ** \param A Opacity(alpha) */
628 ColorAccumulator(const value_type& R, const value_type& G, const value_type& B, const value_type& A=1):
635 ColorAccumulator(const ColorAccumulator& c):
642 ColorAccumulator(const Color& c):
649 ColorAccumulator(int c): a_(c),r_(c), g_(c), b_(c) { }
651 //! Returns the RED component
652 const value_type& get_r()const { return r_; }
654 //! Returns the GREEN component
655 const value_type& get_g()const { return g_; }
657 //! Returns the BLUE component
658 const value_type& get_b()const { return b_; }
660 //! Returns the amount of opacity (alpha)
661 const value_type& get_a()const { return a_; }
663 //! Synonym for get_a(). \see get_a()
664 const value_type& get_alpha()const { return get_a(); }
666 //! Sets the RED component to \a x
667 ColorAccumulator& set_r(const value_type& x) { r_ = x; return *this; }
669 //! Sets the GREEN component to \a x
670 ColorAccumulator& set_g(const value_type& x) { g_ = x; return *this; }
672 //! Sets the BLUE component to \a x
673 ColorAccumulator& set_b(const value_type& x) { b_ = x; return *this; }
675 //! Sets the opacity (alpha) to \a x
676 ColorAccumulator& set_a(const value_type& x) { a_ = x; return *this; }
678 //! Synonym for set_a(). \see set_a()
679 ColorAccumulator& set_alpha(const value_type& x) { return set_a(x); }
683 Color::Color(const ColorAccumulator& c):
697 /* Bit Descriptions (ON/OFF)
698 ** ----+-------------
699 ** 0 Color Channels (Gray/RGB)
700 ** 1 Alpha Channel (WITH/WITHOUT)
701 ** 2 ZDepth (WITH/WITHOUT)
702 ** 3 Endian (BGR/RGB)
703 ** 4 Alpha Location (Start/End)
704 ** 5 ZDepth Location (Start/End)
705 ** 6 Alpha/ZDepth Arangement (ZA,AZ)
706 ** 7 Alpha Range (Inverted,Normal)
707 ** 8 Z Range (Inverted,Normal)
710 PF_GRAY=(1<<0), //!< If set, use one grayscale channel. If clear, use three channels for RGB
711 PF_A=(1<<1), //!< If set, include alpha channel
712 PF_Z=(1<<2), //!< If set, include ZDepth channel
713 PF_BGR=(1<<3), //!< If set, reverse the order of the RGB channels
714 PF_A_START=(1<<4), //!< If set, alpha channel is before the color data. If clear, it is after.
715 PF_Z_START=(1<<5), //!< If set, ZDepth channel is before the color data. If clear, it is after.
716 PF_ZA=(1<<6), //!< If set, the ZDepth channel will be infront of the alpha channel. If clear, they are reversed.
718 PF_A_INV=(1<<7), //!< If set, the alpha channel is stored as 1.0-a
719 PF_Z_INV=(1<<8), //!< If set, the ZDepth channel is stored as 1.0-z
720 PF_RAW_COLOR=(1<<9)+(1<<1) //!< If set, the data represents a raw Color datastructure, and all other bits are ignored.
723 inline PixelFormat operator|(PixelFormat lhs, PixelFormat rhs)
724 { return static_cast<PixelFormat>((int)lhs|(int)rhs); }
726 inline PixelFormat operator&(PixelFormat lhs, PixelFormat rhs)
727 { return static_cast<PixelFormat>((int)lhs&(int)rhs); }
728 #define FLAGS(x,y) (((x)&(y))==(y))
730 //! Returns the number of channels that the given PixelFormat calls for
732 channels(PixelFormat x)
743 if(FLAGS(x,PF_RAW_COLOR))
749 inline unsigned char *
750 Color2PixelFormat(const Color &color, const PixelFormat &pf, unsigned char *out, const Gamma &gamma)
752 if(FLAGS(pf,PF_RAW_COLOR))
754 Color *outcol=reinterpret_cast<Color *>(out);
760 int alpha=(int)((FLAGS(pf,PF_A_INV)?(-(float)color.get_a()+1):(float)color.get_a())*255);
762 if(alpha>255)alpha=255;
764 if(FLAGS(pf,PF_ZA|PF_A_START|PF_Z_START))
766 if(FLAGS(pf,PF_Z_START))
767 *out++/*=(unsigned char)(color.GetZ()*255.0f)*/;
768 if(FLAGS(pf,PF_A_START))
769 *out++=static_cast<unsigned char>(alpha);
773 if(FLAGS(pf,PF_A_START))
774 *out++=static_cast<unsigned char>(alpha);
775 if(FLAGS(pf,PF_Z_START))
776 *out++/*=(unsigned char)(color.GetZ()*255.0f)*/;
780 if(FLAGS(pf,PF_GRAY))
781 *out++=static_cast<unsigned char>(gamma.g_F32_to_U8(color.get_y()));
786 *out++=static_cast<unsigned char>(gamma.r_F32_to_U8(color.get_b()));
787 *out++=static_cast<unsigned char>(gamma.g_F32_to_U8(color.get_g()));
788 *out++=static_cast<unsigned char>(gamma.b_F32_to_U8(color.get_r()));
792 *out++=static_cast<unsigned char>(gamma.r_F32_to_U8(color.get_r()));
793 *out++=static_cast<unsigned char>(gamma.g_F32_to_U8(color.get_g()));
794 *out++=static_cast<unsigned char>(gamma.b_F32_to_U8(color.get_b()));
800 if(!FLAGS(pf,PF_Z_START) && FLAGS(pf,PF_Z))
801 out++;//*out++=(unsigned char)(color.GetZ()*255.0f);
802 if(!FLAGS(pf,PF_A_START) && FLAGS(pf,PF_A))
803 *out++=static_cast<unsigned char>(alpha);
807 if(!FLAGS(pf,PF_Z_START) && FLAGS(pf,PF_Z))
808 out++;//*out++=(unsigned char)(color.GetZ()*255.0f);
809 if(!FLAGS(pf,PF_A_START) && FLAGS(pf,PF_A))
810 *out++=static_cast<unsigned char>(alpha);
816 convert_color_format(unsigned char *dest, const Color *src, int w, PixelFormat pf,const Gamma &gamma)
820 dest=Color2PixelFormat((*(src++)).clamped(),pf,dest,gamma);
823 inline const unsigned char *
824 PixelFormat2Color(Color &color, const PixelFormat &pf,const unsigned char *out)
826 if(FLAGS(pf,PF_ZA|PF_A_START|PF_Z_START))
828 if(FLAGS(pf,PF_Z_START))
829 out++;//color.SetZ((Color::value_type)*out++/255.0f);
830 if(FLAGS(pf,PF_A_START))
831 color.set_a((float)*out++/255);
835 if(FLAGS(pf,PF_A_START))
836 color.set_a((float)*out++/255);
837 if(FLAGS(pf,PF_Z_START))
838 out++;//color.SetZ((Color::value_type)*out++/255.0f);
841 if(FLAGS(pf,PF_GRAY))
842 color.set_yuv((float)*out++/255,0,0);
847 color.set_b((float)*out++/255);
848 color.set_g((float)*out++/255);
849 color.set_r((float)*out++/255);
853 color.set_r((float)*out++/255);
854 color.set_g((float)*out++/255);
855 color.set_b((float)*out++/255);
861 if(!FLAGS(pf,PF_Z_START) && FLAGS(pf,PF_Z))
862 out++;//color.SetZ((Color::value_type)*out++/255.0f);
863 if(!FLAGS(pf,PF_A_START) && FLAGS(pf,PF_A))
864 color.set_a((float)*out++/255);
868 if(!FLAGS(pf,PF_A_START) && FLAGS(pf,PF_A))
869 color.set_a((float)*out++/255);
870 if(!FLAGS(pf,PF_Z_START) && FLAGS(pf,PF_Z))
871 out++;//color.SetZ((Color::value_type)*out++/255.0f);
878 }; // END of namespace synfig
880 /* === E N D =============================================================== */