nux-1.14.0
Color.h
00001 /*
00002  * Copyright 2010 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 namespace nux
00027 {
00028 namespace color
00029 {
00030   // DirectX D3DFormat
00031   //
00032   // All formats are listed from left to right, most significant bit (MSB) to
00033   // least significant bit (LSB). For example, D3DFORMAT_ARGB is ordered from
00034   // the MSB channel A (alpha), to the LSB channel B (blue). When traversing
00035   // surface data, the data is stored in memory from LSB to MSB, which means
00036   // that the channel order in memory is from LSB (blue) to MSB (alpha).
00037   //
00038   // The default value for formats that contain undefined channels (G16R16,
00039   // A8, and so on) is 1. The only exception is the A8 format, which is
00040   // initialized to 000 for the three color channels.
00041   //
00042   // The order of the bits is from the most significant byte first, so
00043   // D3DFMT_A8L8 indicates that the high byte of this 2-byte format is
00044   // alpha. D3DFMT_D16 indicates a 16-bit integer value and an
00045   // application-lockable surface.
00046   //
00047   // Pixel formats have been chosen to enable the expression of
00048   // hardware-vendor-defined extension formats, as well as to include the
00049   // well-established four-character code (FOURCC) method. The set of formats
00050   // understood by the Microsoft Direct3D runtime is defined by D3DFORMAT.
00051 
00052   //Format of RGBA colors is
00053   //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
00054   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00055   //|    alpha        |      red    |     green      |     blue     |
00056   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00057   //MSB 31                                                             0 LSB
00058 
00059 
00060   //Format of RGB colors is
00061   //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
00062   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00063   //|   ignored     |      red      |     green     |     blue      |
00064   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00065 
00066   //Format of BGR colors is
00067   //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
00068   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00069   //|   ignored     |      blue     |     green     |      red      |
00070   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00071 
00072   //Format of RGBA colors is
00073   //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
00074   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00075   //|      red      |      green    |     blue      |     alpha     |
00076   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00077 
00078   //Format of BGRA colors is
00079   //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
00080   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00081   //|      blue      |      green    |     red      |     alpha     |
00082   //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00083 
00084 enum Model {
00085     RGB,
00086     HSV,
00087     HLS,
00088     YUV
00089 };
00090 
00091 enum Channel {
00092     RED,
00093     GREEN,
00094     BLUE,
00095     HUE,
00096     SATURATION,
00097     LIGHT,
00098     VALUE
00099 };
00100 
00101 enum Format {
00102     FLOAT,
00103     HEX,
00104     INT
00105 };
00106 
00107 struct RedGreenBlue;
00108 
00109 struct Color
00110 {
00111     Color();
00112     explicit Color (unsigned int c);
00113     Color(int r, int g, int b);
00114     Color(float r, float g, float b, float a = 1.0f);
00115     Color(RedGreenBlue const& rgb, float a = 1.0f);
00116 
00117     float red;
00118     float green;
00119     float blue;
00120     float alpha;
00121 };
00122 
00123 bool operator == (Color const& lhs, Color const& rhs);
00124 bool operator != (Color const& lhs, Color const& rhs);
00125 
00126 Color ClampVal(Color const&);
00127 Color Saturate(Color const&);
00128 Color Complement(Color const&);
00129 Color Luminance(Color const&);
00130 Color OneMinusLuminance(Color const&);
00131 Color MakeOpaque(Color const&);
00132 
00133 Color operator + (Color const&, Color const&);
00134 Color operator + (float, Color const&);
00135 Color operator + (Color const&, float);
00136 
00137 Color operator - (Color const&, Color const&);
00138 Color operator - (float, Color const&);
00139 Color operator - (Color const&, float);
00140 
00141 Color operator * (float, Color const&);
00142 Color operator * (Color const&, float);
00143 
00144 Color RandomColor();
00145 unsigned int RandomColorINT();
00146 
00147 struct HueSaturationValue;
00148 struct HueLightnessSaturation;
00149 
00150 struct RedGreenBlue
00151 {
00152   RedGreenBlue(float r, float g, float b);
00153   RedGreenBlue(HueSaturationValue const&);
00154   RedGreenBlue(HueLightnessSaturation const&);
00155 
00156   float red;
00157   float green;
00158   float blue;
00159 };
00160 
00161 struct HueSaturationValue
00162 {
00163   HueSaturationValue(float h, float s, float v);
00164   HueSaturationValue(Color const&);
00165   HueSaturationValue(RedGreenBlue const&);
00166 
00167   float hue;
00168   float saturation;
00169   float value;
00170 };
00171 
00172 struct HueLightnessSaturation
00173 {
00174   HueLightnessSaturation(float h, float l, float s);
00175   HueLightnessSaturation(Color const&);
00176   HueLightnessSaturation(RedGreenBlue const&);
00177 
00178   float hue;
00179   float lightness;
00180   float saturation;
00181 };
00182 
00183 //  void RGBtoHSV ( float r, float g, float b, float &h, float &s, float &v );
00184 //  void HSVtoRGB ( float &r, float &g, float &b, float h, float s, float v );
00185 //  void HLStoRGB (float &r, float &g, float &b, float hue, float light, float satur);
00186 //  void RGBtoHLS (float rr, float gg, float bb, float &hue, float &light, float &satur);
00187 
00188 }
00189 using color::Color;
00190 }
00191 
00192 #endif // COLOR_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends