color.h

Go to the documentation of this file.
00001 /*
00002  * color.h
00003  *
00004  * Copyright 2010 Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * colors -- used just about everywhere
00032  */
00033 
00034 #ifndef WAVEPACKET_COLOR_H__
00035 #define WAVEPACKET_COLOR_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "common/common.h"
00039 
00040 
00041 ////////////////////////////////////////////////////////////////////////////////
00042 //
00043 // \ingroup geometric
00044 // \defgroup color Basic Color Definitions
00045 //
00046 // These are basic color definitions used by a variety of libraries.  Includes
00047 // 24-bit (8-bit per channel) and floating point (each channel is 0.0 - 1.0).
00048 //
00049 ////////////////////////////////////////////////////////////////////////////////
00050 /*@{*/
00051 
00052 /// standard 4-byte color value for images.  Byte ranges 0-255
00053 struct img_color_t {
00054         // constants
00055         enum eConstants {
00056                 eMaxComponent   = 255   ///< max byte value for a color
00057         };
00058 
00059         // constructors
00060         img_color_t(void) throw() { }
00061         /// see caveats about the int-based setter
00062         img_color_t(IN int r, IN int g, IN int b, IN int a = eMaxComponent) throw()
00063                 { this->set(r, g, b, a); }
00064         img_color_t(IN const img_color_t& c) throw() { *this = c; }
00065 
00066         // getters and setters
00067         const img_color_t& operator=(IN const img_color_t& c) throw() {
00068                         red = c.red;
00069                         green = c.green;
00070                         blue = c.blue;
00071                         alpha = c.alpha;
00072                         return *this;
00073                 }
00074 
00075         /// could be inefficient!  If you are calling this a lot, consider
00076         ///     setting the byte values directly and doing your own validation.
00077         void set(IN int r, IN int g, IN int b, IN int a = eMaxComponent) throw() {
00078                         ASSERT(0 <= r && r <= eMaxComponent, "bad red: %d", r);
00079                         ASSERT(0 <= g && g <= eMaxComponent, "bad green: %d", g);
00080                         ASSERT(0 <= b && b <= eMaxComponent, "bad blue: %d", b);
00081                         ASSERT(0 <= a && a <= eMaxComponent, "bad alpha: %d", a);
00082                         red = r;
00083                         green = g;
00084                         blue = b;
00085                         alpha = a;
00086                 }
00087 
00088         /// this will set the color to solid (opaque) black
00089         void clear(void) throw() {
00090                         red = green = blue = 0;
00091                         alpha = eMaxComponent;
00092                 }
00093 
00094         /// for debugging
00095         void dump(IN const char * title) const throw() {
00096                         DPRINTF("%s: r=%d, g=%d, b=%d, a=%d",
00097                             title, red, green, blue, alpha);
00098                 }
00099 
00100         // data
00101         byte_t          red;
00102         byte_t          green;
00103         byte_t          blue;
00104         byte_t          alpha;
00105 };
00106 
00107 
00108 
00109 /// colors used for GLUT (OpenGL utility library).  0-127 max component values
00110 struct glut_color_t {
00111         // constructors
00112         glut_color_t(void) throw() { }
00113 
00114         /// see caveats about the setter
00115         glut_color_t(IN float r, IN float g, IN float b, IN float a = 1.0) throw()
00116                 { this->set(r, g, b, a); }
00117         glut_color_t(IN const glut_color_t& c) throw() { *this = c; }
00118 
00119         // getters and setters
00120         const glut_color_t& operator=(IN const glut_color_t& c) throw() {
00121                         red = c.red;
00122                         green = c.green;
00123                         blue = c.blue;
00124                         alpha = c.alpha;
00125                         return *this;
00126                 }
00127 
00128         /// could be inefficient!  If you are calling this a lot, consider
00129         ///     setting the byte values directly and doing your own validation.
00130         void set(IN float r, IN float g, IN float b, IN float a = 1.0) throw() {
00131                         ASSERT(0 <= r && r <= 1.0, "Bad red: %f", r);
00132                         ASSERT(0 <= g && g <= 1.0, "Bad green: %f", g);
00133                         ASSERT(0 <= b && b <= 1.0, "Bad blue: %f", b);
00134                         ASSERT(0 <= a && a <= 1.0, "Bad alpha: %f", a);
00135                         red = r;
00136                         green = g;
00137                         blue = b;
00138                         alpha = a;
00139                 }
00140 
00141         /// this will set the color to solid (opaque) black
00142         void clear(void) throw() {
00143                         red = green = blue = 0;
00144                         alpha = 1.0;
00145                 }
00146 
00147         /// for debugging
00148         void dump(IN const char * title) const throw() {
00149                         DPRINTF("%s: r=%5.3f, g=%5.3f, b=%5.3f, a=%5.3f",
00150                             title, red, green, blue, alpha);
00151                 }
00152 
00153         // data
00154         float   red;
00155         float   green;
00156         float   blue;
00157         float   alpha;
00158 };
00159 
00160 
00161 
00162 ////////////////////////////////////////////////////////////////////////////////
00163 //
00164 //      public API
00165 //
00166 ////////////////////////////////////////////////////////////////////////////////
00167 
00168 void getImgColorFromString(IN const char * text,
00169                         OUT img_color_t& color);
00170 
00171 
00172 
00173 #endif  // WAVEPACKET_COLOR_H__
00174