Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages

color.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                          color.h  -  Color data classes
00003                              -------------------
00004     begin                : Mon Sep 16 2002
00005     copyright            : (C) 2002 by Reality Rift Studios
00006     email                : mattias@realityrift.com
00007  ***************************************************************************
00008 
00009  The contents of this file are subject to the Mozilla Public License Version
00010  1.1 (the "License"); you may not use this file except in compliance with
00011  the License. You may obtain a copy of the License at 
00012  http://www.mozilla.org/MPL/
00013 
00014  Software distributed under the License is distributed on an "AS IS" basis,
00015  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  for the specific language governing rights and limitations under the
00017  License.
00018 
00019  The Original Code is the NeoEngine, color.h
00020 
00021  The Initial Developer of the Original Code is Mattias Jansson.
00022  Portions created by Mattias Jansson are Copyright (C) 2002
00023  Reality Rift Studios. All Rights Reserved.
00024 
00025  ***************************************************************************/
00026 
00027 #ifndef __NECOLOR_H
00028 #define __NECOLOR_H
00029 
00030 
00031 #include "base.h"
00032 #include "util.h"
00033 
00034 
00041 namespace NeoEngine
00042 {
00043 
00044 
00050 class NEOENGINE_API Color
00051 {
00052     public:
00053 
00055         float                           r,g,b,a;
00056 
00057 
00061                                         Color() : r(0.0f), g(0.0f), b(0.0f), a(1.0f) {}
00062 
00067                                         Color( unsigned int uiColor ) : r( float( uiColor & 0xFF ) * 0.0039215686f ), g( float( ( uiColor >> 8 ) & 0xFF ) * 0.0039215686f ), b( float( ( uiColor >> 16 ) & 0xFF ) * 0.0039215686f ), a( float( ( uiColor >> 24 ) & 0xFF ) * 0.0039215686f ) {}
00068 
00076                                         Color( float fR, float fG, float fB, float fA = 1.0f ) : r(fR), g(fG), b(fB), a(fA) {}
00077 
00085         inline void                     Set( float fR, float fG, float fB, float fA = 1.0f ) { r = fR; g = fG; b = fB; a = fA; }
00086 
00091         inline void                     Set( unsigned int uiColor ) { r = float( uiColor & 0xFF ) * 0.0039215686f; g = float( ( uiColor >> 8 ) & 0xFF ) * 0.0039215686f; b = float( ( uiColor >> 16 ) & 0xFF ) * 0.0039215686f; a = float( ( uiColor >> 24 ) & 0xFF ) * 0.0039215686f; }
00092 
00097         inline void                     Set( const Color &rkColor ) { fmemcpy( &r, &rkColor.r, sizeof( float ) * 4 ); }
00098 
00102         inline                          operator float* () { return &r; }
00103 
00107         inline                          operator const float* () const { return &r; }
00108 
00112         inline float                   &operator [] ( int iComponent ) { return ((float*)&r)[ iComponent ]; }
00113 
00117         inline const float             &operator [] ( int iComponent ) const { return ((const float*)&r)[ iComponent ]; }
00118 
00124         inline Color                   &operator = ( const Color &rkColor ) { fmemcpy( &r, &rkColor.r, sizeof( float ) * 4 ); return( *this ); }
00125 
00131         inline bool                     operator == ( const Color &rkColor ) const { return( ( r == rkColor.r ) && ( g == rkColor.g ) && ( b == rkColor.b ) && ( a == rkColor.a ) ); }
00132 
00138         inline bool                     operator != ( const Color &rkColor ) const { return( !( *this == rkColor ) ); }
00139 
00140 //#if !defined( __MINGW32__ ) || defined( BUILD_STATIC )
00142         static const NE_STATIC Color    BLACK;
00143 
00145         static const NE_STATIC Color    WHITE;
00146 
00148         static const NE_STATIC Color    RED;
00149 
00151         static const NE_STATIC Color    GREEN;
00152 
00154         static const NE_STATIC Color    BLUE;
00155 //#else
00156 #if 0
00157 
00158         static NEOENGINE_API Color      BLACK;
00159 
00161         static NEOENGINE_API Color      WHITE;
00162 
00164         static NEOENGINE_API Color      RED;
00165 
00167         static NEOENGINE_API Color      GREEN;
00168 
00170         static NEOENGINE_API Color      BLUE;
00171 #endif
00172 };
00173 
00174 
00181 class NEOENGINE_API Color32
00182 {
00183     private:
00184 
00188         union Color32Union
00189         {
00190           unsigned int                  m_uiValue;
00191           unsigned char                 m_aucComponent[4];
00192         }                               m_uiColor;
00193 
00194 
00195     public:
00196 
00198         static unsigned int             s_uiShift[4];
00199 
00201         static unsigned int             s_uiComponent[4];
00202 
00203 
00207                                         Color32() { m_uiColor.m_uiValue = 0xFF << s_uiShift[3]; }
00208 
00216                                         Color32( unsigned char ucR, unsigned char ucG, unsigned char ucB, unsigned char ucA = 0xFF ) { m_uiColor.m_uiValue = ( ucR << s_uiShift[0] ) | ( ucG << s_uiShift[1] ) | ( ucB << s_uiShift[2] ) | ( ucA << s_uiShift[3] ); }
00217 
00222                                         Color32( unsigned char *pucComponents ) { m_uiColor.m_uiValue = ( pucComponents[0] << s_uiShift[0] ) | ( pucComponents[1] << s_uiShift[1] ) | ( pucComponents[2] << s_uiShift[2] ) | ( pucComponents[3] << s_uiShift[3] ); }
00223 
00228                                         Color32( const Color &rkColor ) { m_uiColor.m_uiValue = ( ( int( rkColor.r * 255.0f ) & 0xFF ) << s_uiShift[0] ) | ( ( int( rkColor.g * 255.0f ) & 0xFF ) << s_uiShift[1] ) | ( ( int( rkColor.b * 255.0f ) & 0xFF ) << s_uiShift[2] ) | ( ( int( rkColor.a * 255.0f ) & 0xFF ) << s_uiShift[3] ); }
00229 
00234                                         Color32( float *pfComponents ) { m_uiColor.m_uiValue = ( ( int( pfComponents[0] * 255.0f ) & 0xFF ) << s_uiShift[0] ) | ( ( int( pfComponents[1] * 255.0f ) & 0xFF ) << s_uiShift[1] ) | ( ( int( pfComponents[2] * 255.0f ) & 0xFF ) << s_uiShift[2] ) | ( ( int( pfComponents[3] * 255.0f ) & 0xFF ) << s_uiShift[3] ); }
00235 
00243                                         Color32( float fR, float fG, float fB, float fA = 1.0f ) { m_uiColor.m_uiValue = ( ( int( fR * 255.0f ) & 0xFF ) << s_uiShift[0] ) | ( ( int( fG * 255.0f ) & 0xFF ) << s_uiShift[1] ) | ( ( int( fB * 255.0f ) & 0xFF ) << s_uiShift[2] ) | ( ( int( fA * 255.0f ) & 0xFF ) << s_uiShift[3] ); }
00244 
00252         inline void                     Set( unsigned char ucR, unsigned char ucG, unsigned char ucB, unsigned char ucA = 0xFF ) { m_uiColor.m_uiValue = ( ucR << s_uiShift[0] ) | ( ucG << s_uiShift[1] ) | ( ucB << s_uiShift[2] ) | ( ucA << s_uiShift[3] ); }
00253 
00258         inline void                     Set( unsigned char *pucComponents ) { m_uiColor.m_uiValue = ( pucComponents[0] << s_uiShift[0] ) | ( pucComponents[1] << s_uiShift[1] ) | ( pucComponents[2] << s_uiShift[2] ) | ( pucComponents[3] << s_uiShift[3] ); }
00259 
00264         inline void                     Set( const Color &rkColor ) { m_uiColor.m_uiValue = ( ( int( rkColor.r * 255.0f ) & 0xFF ) | ( ( int( rkColor.g * 255.0f ) & 0xFF ) << 8 ) | ( ( int( rkColor.b * 255.0f ) & 0xFF ) << 16 ) | ( ( int( rkColor.a * 255.0f ) & 0xFF ) << 24 ) ); }
00265 
00273         inline void                     Set( float fR, float fG, float fB, float fA = 1.0f ) { m_uiColor.m_uiValue = ( ( int( fR * 255.0f ) & 0xFF ) << s_uiShift[0] ) | ( ( int( fG * 255.0f ) & 0xFF ) << s_uiShift[1] ) | ( ( int( fB * 255.0f ) & 0xFF ) << s_uiShift[2] ) | ( ( int( fA * 255.0f ) & 0xFF ) << s_uiShift[3] ); }
00274 
00278                                         operator const unsigned int& () const { return m_uiColor.m_uiValue; }
00279 
00280 
00284                                         operator unsigned char* () { return m_uiColor.m_aucComponent; }
00285 
00289         unsigned char                  &operator [] ( int iComponent ) { return m_uiColor.m_aucComponent[ s_uiComponent[ iComponent ] ]; }
00290 
00294         const unsigned char            &operator [] ( int iComponent ) const { return m_uiColor.m_aucComponent[ s_uiComponent[ iComponent ] ]; }
00295 
00301         Color32                        &operator = ( const Color32 &rkColor ) { m_uiColor.m_uiValue = rkColor.m_uiColor.m_uiValue; return( *this ); }
00302 
00308         Color32                        &operator = ( const Color &rkColor ) { Set( rkColor.r, rkColor.g, rkColor.b, rkColor.a ); return( *this ); }
00309 
00315         Color32                        &operator = ( unsigned int uiColor ) { Set( (unsigned char)( uiColor & 0xFF ), (unsigned char)( ( uiColor >> 8 ) & 0xFF ), (unsigned char)( ( uiColor >> 16 ) & 0xFF ), (unsigned char)( ( uiColor >> 24 ) & 0xFF ) ); return( *this ); }
00316 };
00317 
00318 
00319 };
00320 
00321 
00322 #endif
00323 

Generated on Wed Jan 21 14:21:06 2004 for NeoEngine by doxygen 1.3.5