itom 1.3.0
D:/git-itom/sources/itom/common/color.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2013, Institut für Technische Optik (ITO),
00005     Universität Stuttgart, Germany
00006 
00007     This file is part of itom and its software development toolkit (SDK).
00008 
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013    
00014     In addition, as a special exception, the Institut für Technische
00015     Optik (ITO) gives you certain additional rights.
00016     These rights are described in the ITO LGPL Exception version 1.0,
00017     which can be found in the file LGPL_EXCEPTION.txt in this package.
00018 
00019     itom is distributed in the hope that it will be useful, but
00020     WITHOUT ANY WARRANTY; without even the implied warranty of
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00022     General Public Licence for more details.
00023 
00024     You should have received a copy of the GNU Library General Public License
00025     along with itom. If not, see <http://www.gnu.org/licenses/>.
00026 *********************************************************************** */
00027 
00028 #ifndef COLOR_H
00029 #define COLOR_H
00030 
00031 #include "typeDefs.h"
00032 
00033 namespace ito
00034 {
00035 
00037 
00046         class Rgba32 : public RgbaBase32
00047         {
00048         public:
00049 
00050             enum RGBSelectionFlags
00051             {
00052                 RGBA_B    = 0,
00053                 RGBA_G    = 1,
00054                 RGBA_R    = 2,
00055                 RGBA_A    = 3,
00056                 RGBA_Y    = 4,
00057                 RGBA_RGB  = 5
00058             };
00059 
00060             Rgba32()  
00061             {
00062                 //memset(m_value, 0, 4*sizeof(ito::uint8));
00063             }
00064 
00065             static Rgba32 zeros()
00066             {
00067                 Rgba32 temp;
00068                 temp.rgba = 0;
00069                 return temp;
00070             }
00071 
00072             static Rgba32 black()
00073             {
00074                 Rgba32 temp(255, 0,0,0);
00075                 return temp;
00076             }
00077 
00078             /*static Rgba32 red()
00079             {
00080                 Rgba32 temp(255, 255, 0, 0);
00081                 return temp;
00082             }
00083 
00084             static Rgba32 green()
00085             {
00086                 Rgba32 temp(255, 0, 255, 0);
00087                 return temp;
00088             }
00089 
00090             static Rgba32 blue()
00091             {
00092                 Rgba32 temp(255, 0, 0, 255);
00093                 return temp;
00094             }*/
00095 
00096             static Rgba32 fromUnsignedLong(const uint32 val)
00097             {
00098                 Rgba32 temp;
00099                 memcpy(temp.u8ptr(), &val, 4*sizeof(ito::uint8));
00100                 return temp;
00101             }
00102 
00103             Rgba32(const uint8 &a, const uint8 &r, const uint8 &g, const uint8 &b) 
00104             {
00105                 this->b = b;
00106                 this->g = g;
00107                 this->r = r;
00108                 this->a = a;
00109             }
00110 
00111             explicit Rgba32(const uint8 gray) 
00112             {
00113                 a = 0xFF;
00114                 r = gray;
00115                 b = gray;
00116                 g = gray;
00117                 //memset(value, gray, 3*sizeof(uint8));
00118             }
00119 
00120             Rgba32(const Rgba32 &rhs)
00121             {
00122                 rgba = rhs.rgba;
00123             };
00124 
00125             Rgba32& operator +=(const Rgba32 &rhs)
00126             {
00127                 b = static_cast<uint8>(std::min<int16>(b + rhs.b, 255));
00128                 g = static_cast<uint8>(std::min<int16>(g + rhs.g, 255));
00129                 r = static_cast<uint8>(std::min<int16>(r + rhs.r, 255));
00130                 a = static_cast<uint8>(std::min<int16>(a + rhs.a, 255));
00131                 return *this;
00132             }
00133 
00134             Rgba32& operator =(const Rgba32 &rhs)
00135             {
00136                 rgba = rhs.rgba;
00137                 return *this;
00138             }
00139         
00140             Rgba32& operator =(const uint32 &rhs)
00141             {
00142                 rgba = rhs;
00143                 return *this;
00144             }
00145 
00146             Rgba32& operator -=(const Rgba32 &rhs)
00147             {
00148                 b = static_cast<uint8>(std::max<int16>(b - rhs.b, 0));
00149                 g = static_cast<uint8>(std::max<int16>(g - rhs.g, 0));
00150                 r = static_cast<uint8>(std::max<int16>(r - rhs.r, 0));
00151                 a = static_cast<uint8>(std::max<int16>(a - rhs.a, 0));
00152                 return *this;
00153             }
00154 
00155             Rgba32& operator *=(const Rgba32 &rhs)
00156             {
00157                 b = static_cast<uint8>(b * rhs.b / 255);
00158                 g = static_cast<uint8>(g * rhs.g / 255);
00159                 r = static_cast<uint8>(r * rhs.r / 255);
00160                 a = static_cast<uint8>(a * rhs.a / 255);
00161                 return *this;
00162             }
00163 
00164             Rgba32& operator /=(const Rgba32 &rhs)
00165             {
00166                 if(rhs.b == 0 || rhs.g == 0 || rhs.r == 0 || rhs.a == 0)
00167                 {
00168                     throw std::runtime_error("Division by zero not allowed for rgba32-values");
00169                 }
00170                 b = static_cast<uint8>(std::min<int16>((b * (ito::int16)255) / rhs.b, 255));
00171                 g = static_cast<uint8>(std::min<int16>((g * (ito::int16)255) / rhs.g, 255));
00172                 r = static_cast<uint8>(std::min<int16>((r * (ito::int16)255) / rhs.r, 255));
00173                 a = static_cast<uint8>(std::min<int16>((a * (ito::int16)255) / rhs.a, 255));
00174                 return *this;
00175             }
00176 
00177             Rgba32 operator +(const Rgba32 &second) const 
00178             {
00179                 Rgba32 first(*this);
00180                 first += second;
00181                 return first;
00182             }
00183 
00184             Rgba32 operator -(const Rgba32 &second) const 
00185             {
00186                 Rgba32 first(*this);
00187                 first -= second;
00188                 return first;
00189             }
00190 
00191             Rgba32 operator *(const Rgba32 &second) const 
00192             {
00193                 Rgba32 first(*this);
00194                 first *= second;
00195                 return first;
00196             }
00197 
00198             Rgba32 operator /(const Rgba32 &second) const 
00199             {
00200                 Rgba32 first(*this);
00201                 first /= second;
00202                 return first;
00203             }
00204 
00205             bool operator ==(const Rgba32 &rhs) const 
00206             {
00207                 return (b == rhs.b) && (g == rhs.g) && (r == rhs.r && (a == rhs.a));
00208             }
00209 
00210             bool operator !=(const Rgba32 &rhs) const 
00211             {
00212                 return (b != rhs.b) || (g != rhs.g) || (r != rhs.r || (a != rhs.a));
00213             }
00214 
00215             inline float32 gray() const {return static_cast<float32>(0.299 * r + 0.587 * g + 0.114 * b);} 
00217             uint8& alpha() {return a;}; 
00218             uint8& red()   {return r;}; 
00219             uint8& green() {return g;}; 
00220             uint8& blue()  {return b;}; 
00222             uint8 alpha() const {return a;}; 
00223             uint8 red()   const {return r;}; 
00224             uint8 green() const {return g;}; 
00225             uint8 blue()  const {return b;}; 
00227             uint32& argb() {return rgba;}; 
00228             uint32 argb() const {return rgba;}; 
00230             uint32* u32ptr() {return (&rgba);}
00231             uint8* u8ptr() {return ((uint8*)(&rgba));}
00232         };
00233 
00234 
00235         template<uint8 _COLOR> class RGBChannel : Rgba32
00236         {
00237         public:
00238             RGBChannel()  
00239             {
00240                 //memset(m_value, 0, 4*sizeof(ito::uint8));
00241             };
00242 
00243             explicit RGBChannel(const uint8 gray) 
00244             {
00245                 r = g = b = 0;
00246                 a = 0xFF;
00247                 items[_COLOR] = gray;
00248             }
00249 
00250             static RGBChannel<_COLOR> zeros()
00251             {
00252                 RGBChannel<_COLOR> temp;
00253                 temp.rgba = 0;
00254                 return temp;
00255             }
00256 
00257             static RGBChannel<_COLOR> black()
00258             {
00259                 RGBChannel<_COLOR> temp;
00260                 temp.rgba = 0;
00261                 temp.a = 0xFF;
00262                 return temp;
00263             }
00264 
00265             RGBChannel(const RGBChannel &rhs)
00266             {
00267                 rgba = 0;
00268                 a = 0xFF;
00269                 items[_COLOR] = rhs.items[_COLOR];
00270             }
00271 
00272             RGBChannel& operator +=(const RGBChannel &rhs)
00273             {
00274                 items[_COLOR] = static_cast<uint8>(std::min<int16>(items[_COLOR] + rhs.items[_COLOR], 255));
00275                 return *this;
00276             }
00277 
00278             RGBChannel& operator =(const RGBChannel &rhs)
00279             {
00280                 items[_COLOR] = rhs.items[_COLOR];
00281                 return *this;
00282             }
00283 
00284             RGBChannel& operator =(const uint32 &rhs)
00285             {
00286                 items[_COLOR] = ((unsigned char*)&rhs)[_COLOR]; 
00287                 return *this;
00288             }
00289 
00290             RGBChannel& operator -=(const RGBChannel &rhs)
00291             {
00292                 items[_COLOR] = static_cast<uint8>(std::max<int16>(items[_COLOR] - rhs.items[_COLOR], 0));
00293                 return *this;
00294             }
00295 
00296             RGBChannel& operator *=(const RGBChannel &rhs)
00297             {
00298                 items[_COLOR] = static_cast<uint8>(items[_COLOR] * rhs.items[_COLOR] / 255);
00299                 return *this;
00300             }
00301 
00302             RGBChannel& operator /=(const RGBChannel &rhs)
00303             {
00304                 if(rhs.items[_COLOR] == 0)
00305                 {
00306                     throw std::runtime_error("Division by zero not allowed for rgba32-values");
00307                 }
00308                 items[_COLOR] = static_cast<uint8>(std::min<int16>((items[_COLOR] * (ito::int16)255)/ rhs.items[_COLOR], 255));
00309                 return *this;
00310             }
00311 
00312             RGBChannel operator +(const RGBChannel &second) const 
00313             {
00314                 RGBChannel<_COLOR> first(*this);
00315                 first += second;
00316                 return first;
00317             }
00318 
00319             RGBChannel operator -(const RGBChannel &second) const 
00320             {
00321                 RGBChannel<_COLOR> first(*this);
00322                 first -= second;
00323                 return first;
00324             }
00325 
00326             RGBChannel operator *(const RGBChannel &second) const 
00327             {
00328                 RGBChannel<_COLOR> first(*this);
00329                 first *= second;
00330                 return first;
00331             }
00332 
00333             RGBChannel operator /(const RGBChannel &second) const 
00334             {
00335                 RGBChannel<_COLOR> first(*this);
00336                 first /= second;
00337                 return first;
00338             }
00339 
00340             bool operator ==(const RGBChannel &rhs) const 
00341             {
00342                 return items[_COLOR] == rhs.items[_COLOR];
00343             }
00344 
00345             bool operator !=(const RGBChannel &rhs) const 
00346             {
00347                 return items[_COLOR] != rhs.items[_COLOR];
00348             }
00349 
00350             bool operator <(const RGBChannel &rhs) const 
00351             {
00352                 return (items[_COLOR] < rhs.items[_COLOR]);
00353             }
00354 
00355             bool operator >(const RGBChannel &rhs) const 
00356             {
00357                 return (items[_COLOR] < rhs.items[_COLOR]);
00358             }
00359 
00360             inline float32 gray() const {return static_cast<float32> (items[_COLOR]);}; 
00362             uint8& value() {return items[_COLOR];}; 
00363             uint8 value() const {return items[_COLOR];}; 
00364         };
00365 
00366         typedef RGBChannel<Rgba32::RGBA_A> AlphaChannel;
00367         typedef RGBChannel<Rgba32::RGBA_R> RedChannel;
00368         typedef RGBChannel<Rgba32::RGBA_G> GreenChannel;
00369         typedef RGBChannel<Rgba32::RGBA_B> BlueChannel;
00371 
00372 } //end namespace ito
00373 
00374 #endif //COLOR_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends