CVD 0.8
cvd/rgba.h
00001 /*                       
00002     This file is part of the CVD Library.
00003 
00004     Copyright (C) 2005 The Authors
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to the Free Software
00018     Foundation, Inc., 
00019     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 #ifndef CVD_RGBA_H
00022 #define CVD_RGBA_H
00023 
00024 #include <iostream>
00025 #include <cvd/internal/is_pod.h>
00026 
00027 namespace CVD {
00028 
00029 
00031 // CVD::Rgba
00032 // Template class to represent red, green, blue and alpha components
00033 //
00037 template <typename T>
00038 class Rgba
00039 {
00040 public:
00042     explicit Rgba() : red(0), green(0), blue(0), alpha(0) {}
00043 
00049     inline explicit Rgba(T r, T g, T b, T a) : red(r), green(g), blue(b), alpha(a) {}
00050     template <class S> inline explicit Rgba(const Rgba<S>& rgba) : red(static_cast<T>(rgba.red)), green(static_cast<T>(rgba.green)), blue(static_cast<T>(rgba.blue)), alpha(static_cast<T>(rgba.alpha)) {}
00051 
00052    T red; 
00053    T green; 
00054    T blue; 
00055    T alpha; 
00056 
00059    template <typename T2>
00060      Rgba<T>& operator=(const Rgba<T2>& c){
00061      red = static_cast<T>(c.red);
00062      green = static_cast<T>(c.green); 
00063      blue = static_cast<T>(c.blue); 
00064      alpha = static_cast<T>(c.alpha); 
00065      return *this;
00066    }
00067 
00070     bool operator==(const Rgba<T>& c) const
00071       {return red == c.red && green == c.green && blue == c.blue && alpha == c.alpha;}
00072 
00075     bool operator!=(const Rgba<T>& c) const
00076       {return red != c.red || green != c.green || blue != c.blue || alpha != c.alpha;}
00077 
00078 //   T to_grey() const {return 0.3*red + 0.6*green + 0.1*blue;}
00079 };
00080 
00085 template <typename T>
00086 std::ostream& operator <<(std::ostream& os, const Rgba<T>& x)
00087 {
00088    return os << "(" << x.red << "," << x.green << ","
00089              << x.blue << "," << x.alpha << ")";
00090 }
00091 
00096 inline std::ostream& operator <<(std::ostream& os, const Rgba<unsigned char>& x)
00097 {
00098    return os << "(" << static_cast<unsigned int>(x.red) << ","
00099              << static_cast<unsigned int>(x.green) << ","
00100              << static_cast<unsigned int>(x.blue) << ","
00101              << static_cast<unsigned int>(x.alpha) << ")";
00102 }
00103 
00104 #ifndef DOXYGEN_IGNORE_INTERNAL
00105 namespace Internal
00106 {
00107   template<class C> struct is_POD<Rgba<C> >
00108   {
00109     enum { is_pod = is_POD<C>::is_pod };
00110   };
00111 }
00112 #endif
00113 
00114 
00115 
00116 } // end namespace 
00117 #endif
00118