Gel2D - The free/open source game creation suite

gelColor.h
Go to the documentation of this file.
00001 /*
00002 Gel2D Game Engine - Cross-platform 2D gaming middleware
00003 Copyright (C) 2010-2011 Mark D. Procarione
00004 
00005 Gel2D is free software: you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation, either version 3 of the License, or
00008 (at your option) any later version.
00009 
00010 Gel2D 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 __GELCOLOR_H__
00017 #define __GELCOLOR_H__
00018 
00019 #include "gelNamespace.h"
00020 
00021 #define GEL_WHITE   GelColor(1.0f, 1.0f, 1.0f, 1.0f)
00022 #define GEL_BLACK   GelColor(0.0f, 0.0f, 0.0f, 1.0f)
00023 #define GEL_RED     GelColor(1.0f, 0.0f, 0.0f, 1.0f)
00024 #define GEL_GREEN   GelColor(0.0f, 1.0f, 0.0f, 1.0f)
00025 #define GEL_BLUE    GelColor(0.0f, 0.0f, 1.0f, 1.0f)
00026 #define GEL_PURPLE  GelColor(0.75f, 0.25f, 1.0f, 1.0f)
00027 #define GEL_YELLOW  GelColor(1.0f, 1.0f, 0.0f, 1.0f)
00028 #define GEL_ORANGE  GelColor(1.0f, 0.5f, 0.0f, 1.0f)
00029 #define GEL_PINK    GelColor(1.0f, 0.5f, 0.75f, 1.0f)
00030 #define GEL_BROWN   GelColor(0.3f, 0.2f, 0.1f, 1.0f)
00031 #define GEL_GREY    GelColor(0.5f, 0.5f, 0.5f, 1.0f)
00032 #define GEL_SKYBLUE GelColor(0.0f, 1.0f, 1.0f, 1.0f)
00033 
00034 namespace gel
00035 {
00036     // Define these ahead of time so all color classes
00037     // can interact with each other.
00038     class GelHWColor;
00039     class GelRGBA;
00040     class GelHSLA;
00041     class GelColor;
00042 
00046 
00047 
00048     class GelHWColor
00049     {
00050         public:
00052             GelHWColor() : c(0xffffffff) {}
00053 
00055 
00059             GelHWColor( int red, int green, int blue, int alpha );
00060 
00062 
00063             GelHWColor( GELCOLOR col ) : c(col) {}
00064 
00066 
00067             GelHWColor( const GelRGBA &col );
00068 
00070 
00071             GelHWColor( const GelHSLA &col );
00072 
00074 
00075             GelHWColor( const GelColor &col );
00076 
00077         public:
00079             GELCOLOR c;
00080     };
00081 
00083 
00084     class GelRGBA
00085     {
00086         public:
00088             GelRGBA() { r = g = b = a = 255; }
00089 
00091 
00095             GelRGBA( int red, int green, int blue, int alpha ) { r = red; g = green; b = blue; a = alpha; }
00096 
00098 
00099             GelRGBA( int value ) { r = g = b = a = value; }
00100 
00102 
00103             GelRGBA( const GelHWColor &col );
00104 
00106 
00107             GelRGBA( const GelColor &col );
00108 
00110 
00111             GelRGBA( const GelHSLA &col );
00112 
00113             GelRGBA operator+ ( const GelRGBA &c ) const { return GelRGBA( r+c.r, g+c.g, b+c.b, a+c.a ); }
00114             GelRGBA operator- ( const GelRGBA &c ) const { return GelRGBA( r-c.r, g-c.g, b-c.b, a-c.a ); }
00115             GelRGBA operator* ( const GelRGBA &c ) const { return GelRGBA( r*c.r, g*c.g, b*c.b, a*c.a ); }
00116             GelRGBA operator/ ( const GelRGBA &c ) const { return GelRGBA( r/c.r, g/c.g, b/c.b, a/c.a ); }
00117             GelRGBA operator= ( const GelRGBA &c )  { r=c.r; g=c.g; b=c.b; a=c.a; return *this; }
00118             GelRGBA operator+=( const GelRGBA &c ) { r+=c.r; g+=c.g; b+=c.b; a+=c.a; return *this; }
00119             GelRGBA operator-=( const GelRGBA &c ) { r-=c.r; g-=c.g; b-=c.b; a-=c.a; return *this; }
00120             GelRGBA operator*=( const GelRGBA &c ) { r*=c.r; g*=c.g; b*=c.b; a*=c.a; return *this; }
00121             GelRGBA operator/=( const GelRGBA &c ) { r/=c.r; g/=c.g; b/=c.b; a/=c.a; return *this; }
00122 
00123             bool operator==( const GelRGBA &c ) const { return (r==c.r && g==c.g && b==c.b && a==c.a); }
00124             bool operator!=( const GelRGBA &c ) const { return (r!=c.r || g!=c.g || b!=c.b || a!=c.a); }
00125 
00126         public:
00128             int r;
00130             int g;
00132             int b;
00134             int a;
00135     };
00136 
00138 
00140     class GelHSLA
00141     {
00142         public:
00144             GelHSLA() { h = s = l = a = 255; }
00145 
00147 
00151             GelHSLA( int hue, int sat, int lum, int alpha ) { h = hue; s = sat; l = lum; a = alpha; }
00152 
00154 
00155             GelHSLA( int value ) { h = s = l = a = value; }
00156 
00158 
00159             GelHSLA( const GelRGBA &col );
00160 
00161             GelHSLA operator+ ( const GelHSLA &c ) const { return GelHSLA( h+c.h, s+c.s, l+c.l, a+c.a ); }
00162             GelHSLA operator- ( const GelHSLA &c ) const { return GelHSLA( h-c.h, s-c.s, l-c.l, a-c.a ); }
00163             GelHSLA operator* ( const GelHSLA &c ) const { return GelHSLA( h*c.h, s*c.s, l*c.l, a*c.a ); }
00164             GelHSLA operator/ ( const GelHSLA &c ) const { return GelHSLA( h/c.h, s/c.s, l/c.l, a/c.a ); }
00165             GelHSLA operator= ( const GelHSLA &c ) { h=c.h; s=c.s; l=c.l; a=c.a; return *this; }
00166             GelHSLA operator+=( const GelHSLA &c ) { h+=c.h; s+=c.s; l+=c.l; a+=c.a; return *this; }
00167             GelHSLA operator-=( const GelHSLA &c ) { h-=c.h; s-=c.s; l-=c.l; a-=c.a; return *this; }
00168             GelHSLA operator*=( const GelHSLA &c ) { h*=c.h; s*=c.s; l*=c.l; a*=c.a; return *this; }
00169             GelHSLA operator/=( const GelHSLA &c ) { h/=c.h; s/=c.s; l/=c.l; a/=c.a; return *this; }
00170 
00171             bool operator==( const GelHSLA &c ) const { return (h==c.h && s==c.s && l==c.l && a==c.a); }
00172             bool operator!=( const GelHSLA &c ) const { return (h!=c.h || s!=c.s || l!=c.l || a!=c.a); }
00173 
00174         public:
00176             int h;
00178             int s;
00180             int l;
00182             int a;
00183     };
00184 
00186 
00188     class GelColor
00189     {
00190         public:
00192             GelColor() { r = g = b = a = 1.0f; }
00193 
00195 
00199             GelColor( float red, float green, float blue, float alpha ) { r = red; g = green; b = blue; a = alpha; }
00200 
00202 
00203             GelColor( float value ) { r = g = b = a = value; }
00204 
00206 
00207             GelColor( const GelHWColor &col );
00208 
00210 
00211             GelColor( const GelRGBA &col );
00212 
00214 
00215             GelColor( const GelHSLA &col );
00216 
00217             void clamp() { cClamp(r); cClamp(g); cClamp(b); cClamp(a); }
00218 
00220 
00223             void setRGB( float red, float green, float blue ) { r = red; g = green; b = blue; }
00224 
00225             GelColor operator+ ( const GelColor &c ) const { return GelColor( r+c.r, g+c.g, b+c.b, a+c.a ); }
00226             GelColor operator- ( const GelColor &c ) const { return GelColor( r-c.r, g-c.g, b-c.b, a-c.a ); }
00227             GelColor operator* ( const GelColor &c ) const { return GelColor( r*c.r, g*c.g, b*c.b, a*c.a ); }
00228             GelColor operator/ ( const GelColor &c ) const { return GelColor( r/c.r, g/c.g, b/c.b, a/c.a ); }
00229             GelColor operator= ( const GelColor &c ) { r=c.r; g=c.g; b=c.b; a=c.a; return *this; }
00230             GelColor operator+=( const GelColor &c ) { r+=c.r; g+=c.g; b+=c.b; a+=c.a; return *this; }
00231             GelColor operator-=( const GelColor &c ) { r-=c.r; g-=c.g; b-=c.b; a-=c.a; return *this; }
00232             GelColor operator*=( const GelColor &c ) { r*=c.r; g*=c.g; b*=c.b; a*=c.a; return *this; }
00233             GelColor operator/=( const GelColor &c ) { r/=c.r; g/=c.g; b/=c.b; a/=c.a; return *this; }
00234 
00235             bool operator==( const GelColor &c ) const { return (r==c.r && g==c.g && b==c.b && a==c.a); }
00236             bool operator!=( const GelColor &c ) const { return (r!=c.r || g!=c.g || b!=c.b || a!=c.a); }
00237 
00238         private:
00239             void cClamp(float &x) { if(x<0.0f) x=0.0f; if(x>1.0f) x=1.0f; }
00240 
00241         public:
00243             float r;
00245             float g;
00247             float b;
00249             float a;
00250     };
00251 
00253     class GelColorTool
00254     {
00255         public:
00256             static GelHWColor RGBAtoHW( const GelRGBA &col );
00257             static GelHWColor HSLAtoHW( const GelHSLA &col );
00258             static GelHWColor ScalarToHW( const GelColor &col );
00259 
00260             static GelRGBA HSLAtoRGBA( const GelHSLA &col );
00261             static GelRGBA HWtoRGBA( const GelHWColor &col );
00262             static GelRGBA ScalarToRGBA( const GelColor &col );
00263 
00264             static GelHSLA HWtoHSLA( const GelHWColor &col );
00265             static GelHSLA RGBAtoHSLA( const GelRGBA &col );
00266             static GelHSLA ScalarToHSLA( const GelColor &col );
00267 
00268             static GelColor HWtoScalar( const GelHWColor &col );
00269             static GelColor RGBAtoScalar( const GelRGBA &col );
00270             static GelColor HSLAtoScalar( const GelHSLA &col );
00271     };
00272 
00274 }
00275 
00276 #endif // __GELCOLOR_H__