iColor.h
Go to the documentation of this file.
00001 /*
00002 imagen - Simple object-oriented imaging library
00003 Copyright (C) 2011-2012 Mark D. Procarione
00004 
00005 imagen is free software: you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation, either
00008 version 3 of the License, or (at your option) any later version.
00009 
00010 imagen is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014 */
00015 
00016 #ifndef __ICOLOR_H__
00017 #define __ICOLOR_H__
00018 
00019 #include "imagen.h"
00020 
00021 #include <cmath>
00022 
00023 namespace imagen
00024 {
00026 
00027     class iColor
00028     {
00029         public:
00031             iColor() { r = g = b = a = 255; }
00032 
00034 
00038             iColor( int red, int green, int blue, int alpha = 255 ) { r = red; g = green; b = blue; a = alpha; }
00039 
00041 
00042             iColor( int value ) { r = g = b = a = value; }
00043 
00045             int length() const
00046             {
00047                 return (int)sqrt(r*r + g*g + b*b);
00048             }
00049 
00051             int sqrLength() const
00052             {
00053                 return r*r + g*g + b*b;
00054             }
00055 
00057 
00059             int distanceTo( const iColor &col ) const
00060             {
00061                 return (int)sqrt((r - col.r)*(r - col.r) +
00062                                  (g - col.g)*(g - col.g) +
00063                                  (b - col.b)*(b - col.b));
00064             }
00065 
00067 
00069             int dot( const iColor &col ) const
00070             {
00071                 return r*col.r + g*col.g + b*col.b;
00072             }
00073 
00075 
00077             iColor cross( const iColor &col ) const
00078             {
00079                 return iColor((g*col.b) - (b*col.g),
00080                                 (b*col.r) - (r*col.b),
00081                                 (r*col.g) - (g*col.r));
00082             }
00083 
00085             void normalize()
00086             {
00087                 int len = length();
00088                 r /= len;
00089                 g /= len;
00090                 b /= len;
00091             }
00092 
00094             int average() const
00095             {
00096                  return (r+g+b)/3;
00097             }
00098 
00100             void clamp() { cClamp(r); cClamp(g); cClamp(b); cClamp(a); }
00101 
00102             iColor operator+ ( const iColor &c ) const { return iColor( r+c.r, g+c.g, b+c.b, a+c.a ); }
00103             iColor operator- ( const iColor &c ) const { return iColor( r-c.r, g-c.g, b-c.b, a-c.a ); }
00104             iColor operator* ( const iColor &c ) const { return iColor( r*c.r, g*c.g, b*c.b, a*c.a ); }
00105             iColor operator/ ( const iColor &c ) const { return iColor( r/c.r, g/c.g, b/c.b, a/c.a ); }
00106             iColor operator= ( const iColor &c )  { r=c.r; g=c.g; b=c.b; a=c.a; return *this; }
00107             iColor operator+=( const iColor &c ) { r+=c.r; g+=c.g; b+=c.b; a+=c.a; return *this; }
00108             iColor operator-=( const iColor &c ) { r-=c.r; g-=c.g; b-=c.b; a-=c.a; return *this; }
00109             iColor operator*=( const iColor &c ) { r*=c.r; g*=c.g; b*=c.b; a*=c.a; return *this; }
00110             iColor operator/=( const iColor &c ) { r/=c.r; g/=c.g; b/=c.b; a/=c.a; return *this; }
00111 
00112             bool operator==( const iColor &c ) const { return (r==c.r && g==c.g && b==c.b && a==c.a); }
00113             bool operator!=( const iColor &c ) const { return (r!=c.r || g!=c.g || b!=c.b || a!=c.a); }
00114 
00115             iColor operator*( float i ) const { return iColor( (int)((float)i*r), (int)((float)i*g), (int)((float)i*b), (int)((float)i*a)); }
00116 
00117             private:
00118                 void cClamp( int &x ) { if(x<0) x=0; if(x>255) x=255; }
00119 
00120         public:
00122             int r;
00124             int g;
00126             int b;
00128             int a;
00129     };
00130 }
00131 
00132 #endif // __ICOLOR_H__
00133