Back to index

nux  3.0.0
Color.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010-2012 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef COLOR_H
00024 #define COLOR_H
00025 
00026 #include <string>
00027 
00028 namespace nux
00029 {
00030 namespace color
00031 {
00032   // DirectX D3DFormat
00033   //
00034   // All formats are listed from left to right, most significant bit (MSB) to
00035   // least significant bit (LSB). For example, D3DFORMAT_ARGB is ordered from
00036   // the MSB channel A (alpha), to the LSB channel B (blue). When traversing
00037   // surface data, the data is stored in memory from LSB to MSB, which means
00038   // that the channel order in memory is from LSB (blue) to MSB (alpha).
00039   //
00040   // The default value for formats that contain undefined channels (G16R16,
00041   // A8, and so on) is 1. The only exception is the A8 format, which is
00042   // initialized to 000 for the three color channels.
00043   //
00044   // The order of the bits is from the most significant byte first, so
00045   // D3DFMT_A8L8 indicates that the high byte of this 2-byte format is
00046   // alpha. D3DFMT_D16 indicates a 16-bit integer value and an
00047   // application-lockable surface.
00048   //
00049   // Pixel formats have been chosen to enable the expression of
00050   // hardware-vendor-defined extension formats, as well as to include the
00051   // well-established four-character code (FOURCC) method. The set of formats
00052   // understood by the Microsoft Direct3D runtime is defined by D3DFORMAT.
00053 
00054   //Format of RGBA colors is
00055   //7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
00056   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00057   //|    alpha        |      red    |     green      |     blue     |
00058   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00059   //MSB 31                                                             0 LSB
00060 
00061 
00062   //Format of RGB colors is
00063   //7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
00064   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00065   //|   ignored     |      red      |     green     |     blue      |
00066   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00067 
00068   //Format of BGR colors is
00069   //7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
00070   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00071   //|   ignored     |      blue     |     green     |      red      |
00072   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00073 
00074   //Format of RGBA colors is
00075   //7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
00076   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00077   //|      red      |      green    |     blue      |     alpha     |
00078   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00079 
00080   //Format of BGRA colors is
00081   //7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
00082   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00083   //|      blue      |      green    |     red      |     alpha     |
00084   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00085 
00086   enum Model
00087   {
00088     RGB,
00089     HSV,
00090     HLS,
00091     YUV
00092   };
00093 
00094   enum Channel
00095   {
00096     RED,
00097     GREEN,
00098     BLUE,
00099     HUE,
00100     SATURATION,
00101     LIGHT,
00102     VALUE
00103   };
00104 
00105   enum Format
00106   {
00107     FLOAT,
00108     HEX,
00109     INT
00110   };
00111 
00112   class RedGreenBlue;
00113 
00114   class Color
00115   {
00116   public:
00117     Color();
00118     explicit Color(unsigned int c);
00119     Color(int r, int g, int b);
00120     Color(float r, float g, float b, float a = 1.0f);
00121     Color(RedGreenBlue const& rgb, float a = 1.0f);
00122 
00124 
00134     Color(std::string const& hex);
00135     
00137 
00144     Color GetPremultiplied();
00145 
00147 
00155     void SetPremultiplied(float r, float g, float b, float a);
00156 
00158 
00163     bool IsPremultiplied();
00164 
00165     float red;
00166     float green;
00167     float blue;
00168     float alpha;
00169 
00170   protected:
00171     bool premultiplied_; 
00172 
00173     friend bool operator == (Color const& lhs, Color const& rhs);
00174   };
00175 
00176   bool operator == (Color const& lhs, Color const& rhs);
00177   bool operator != (Color const& lhs, Color const& rhs);
00178 
00179   Color operator + (Color const&, Color const&);
00180   Color operator + (float, Color const&);
00181   Color operator + (Color const&, float);
00182 
00183   Color operator - (Color const&, Color const&);
00184   Color operator - (float, Color const&);
00185   Color operator - (Color const&, float);
00186 
00187   Color operator * (float, Color const&);
00188   Color operator * (Color const&, float);
00189 
00190   Color RandomColor();
00191   unsigned int RandomColorINT();
00192 
00193   class HueSaturationValue;
00194   class HueLightnessSaturation;
00195 
00196   class RedGreenBlue
00197   {
00198   public:
00199     RedGreenBlue(float r, float g, float b);
00200     RedGreenBlue(HueSaturationValue const&);
00201     RedGreenBlue(HueLightnessSaturation const&);
00202 
00203     float red;
00204     float green;
00205     float blue;
00206   };
00207 
00208   class HueSaturationValue
00209   {
00210   public:
00211     HueSaturationValue(float h, float s, float v);
00212     HueSaturationValue(Color const&);
00213     HueSaturationValue(RedGreenBlue const&);
00214 
00215     float hue;
00216     float saturation;
00217     float value;
00218   };
00219 
00220   class HueLightnessSaturation
00221   {
00222   public:
00223     HueLightnessSaturation(float h, float l, float s);
00224     HueLightnessSaturation(Color const&);
00225     HueLightnessSaturation(RedGreenBlue const&);
00226 
00227     float hue;
00228     float lightness;
00229     float saturation;
00230   };
00231 
00232 }
00233 using color::Color;
00234 }
00235 
00236 #endif // COLOR_H