PixelLightAPI  .
Color4.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Color4.h                                       *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLGRAPHICS_COLOR4_H__
00024 #define __PLGRAPHICS_COLOR4_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLCore/String/String.h>
00032 #include "PLGraphics/PLGraphics.h"
00033 
00034 
00035 //[-------------------------------------------------------]
00036 //[ Namespace                                             ]
00037 //[-------------------------------------------------------]
00038 namespace PLGraphics {
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Forward declarations                                  ]
00043 //[-------------------------------------------------------]
00044 class Color3;
00045 
00046 
00047 //[-------------------------------------------------------]
00048 //[ Classes                                               ]
00049 //[-------------------------------------------------------]
00050 /**
00051 *  @brief
00052 *    RGBA color
00053 */
00054 class Color4 {
00055 
00056 
00057     //[-------------------------------------------------------]
00058     //[ Public static data                                    ]
00059     //[-------------------------------------------------------]
00060     public:
00061         // Special
00062         static PLGRAPHICS_API const Color4 Null;        /**< -1.0, -1.0, -1.0, -1.0 */
00063         static PLGRAPHICS_API const Color4 Transparent; /**<  0.0,  0.0,  0.0,  0.0 */
00064         // HTML 4/VGA (alpha channel is always sA="255", Hex="#FF")
00065         static PLGRAPHICS_API const Color4 Black;   /**< Black (sRGB="0 0 0", Hex="#000000") */
00066         static PLGRAPHICS_API const Color4 White;   /**< White (sRGB="255 255 255", Hex="#FFFFFF") */
00067         static PLGRAPHICS_API const Color4 Red;     /**< Red (sRGB="255 0 0", Hex="#FF0000") */
00068         static PLGRAPHICS_API const Color4 Yellow;  /**< Yellow (sRGB="255 255 0", Hex="#FFFF00") */
00069         static PLGRAPHICS_API const Color4 Green;   /**< Green (sRGB="0 128 0", Hex="#008000") */
00070         static PLGRAPHICS_API const Color4 Aqua;    /**< Aqua, also called cyan or blue-green (sRGB="0 255 255", Hex="#00FFFF") */
00071         static PLGRAPHICS_API const Color4 Blue;    /**< Blue (sRGB="0 0 255", Hex="#0000FF") */
00072         static PLGRAPHICS_API const Color4 Fuchsia; /**< Fuchsia, also called full magenta, bright pink or vivid pink (sRGB="255 0 255", Hex="#FF00FF") */
00073         static PLGRAPHICS_API const Color4 Maroon;  /**< Maroon, dark brownish-red color (sRGB="128 0 0", Hex="#800000") */
00074         static PLGRAPHICS_API const Color4 Olive;   /**< Olive, dark green/brown color (sRGB="128 128 0", Hex="#808000") */
00075         static PLGRAPHICS_API const Color4 Navy;    /**< Navy, also called navy blue, very dark shade of the color blue (sRGB="0 0 128", Hex="#000080") */
00076         static PLGRAPHICS_API const Color4 Purple;  /**< Purple, color intermediate to red and blue (sRGB="128 0 128", Hex="#800080") */
00077         static PLGRAPHICS_API const Color4 Teal;    /**< Teal, also called teal blue, greenish dark-blue color (sRGB="0 128 128", Hex="#008080") */
00078         static PLGRAPHICS_API const Color4 Gray;    /**< Gray (sRGB="128 128 128", Hex="#808080") */
00079         static PLGRAPHICS_API const Color4 Silver;  /**< Silver (sRGB="192 192 192", Hex="#C0C0C0") */
00080         static PLGRAPHICS_API const Color4 Lime;    /**< Lime, actually corresponds to the green primary of an RGB display (sRGB="0 255 0", Hex="#00FF00") */
00081 
00082 
00083     //[-------------------------------------------------------]
00084     //[ Public data                                           ]
00085     //[-------------------------------------------------------]
00086     public:
00087         union {
00088             float fColor[4];
00089             struct {
00090                 float r, g, b, a;
00091             };
00092         };
00093 
00094 
00095     //[-------------------------------------------------------]
00096     //[ Public static functions                               ]
00097     //[-------------------------------------------------------]
00098     public:
00099         /**
00100         *  @brief
00101         *    Maps a RGBA floating point channels (0.0f to 1.0f range) to PLCore::uint32
00102         *
00103         *  @param[in] fR
00104         *    Red color component (0.0-1.0)
00105         *  @param[in] fG
00106         *    Green color component (0.0-1.0)
00107         *  @param[in] fB
00108         *    Blue color component (0.0-1.0)
00109         *  @param[in] fA
00110         *    Alpha color component (0.0-1.0)
00111         *
00112         *  @return
00113         *    The color as PLCore::uint32
00114         */
00115         static inline PLCore::uint32 ToUInt32(float fR, float fG, float fB, float fA);
00116 
00117         /**
00118         *  @brief
00119         *    Returns the red color component (float, 0.0-1.0) from a PLCore::uint32 RGBA color
00120         *
00121         *  @param[in] nColor
00122         *    Color as PLCore::uint32
00123         *
00124         *  @return
00125         *    The red color component (0.0-1.0)
00126         */
00127         static inline float RedFromUInt32(PLCore::uint32 nColor);
00128 
00129         /**
00130         *  @brief
00131         *    Returns the green color component (float, 0.0-1.0) from a PLCore::uint32 RGBA color
00132         *
00133         *  @param[in] nColor
00134         *    Color as PLCore::uint32
00135         *
00136         *  @return
00137         *    The green color component (0.0-1.0)
00138         */
00139         static inline float GreenFromUInt32(PLCore::uint32 nColor);
00140 
00141         /**
00142         *  @brief
00143         *    Returns the blue color component (float, 0.0-1.0) from a PLCore::uint32 RGBA color
00144         *
00145         *  @param[in] nColor
00146         *    Color as PLCore::uint32
00147         *
00148         *  @return
00149         *    The blue color component (0.0-1.0)
00150         */
00151         static inline float BlueFromUInt32(PLCore::uint32 nColor);
00152 
00153         /**
00154         *  @brief
00155         *    Returns the alpha color component (float, 0.0-1.0) from a PLCore::uint32 RGBA color
00156         *
00157         *  @param[in] nColor
00158         *    Color as PLCore::uint32
00159         *
00160         *  @return
00161         *    The alpha color component (0.0-1.0)
00162         */
00163         static inline float AlphaFromUInt32(PLCore::uint32 nColor);
00164 
00165         /**
00166         *  @brief
00167         *    Swaps the RB components in the given buffer with 4 components (BGRA <-> RGBA)
00168         *
00169         *  @param[in, out] nBuffer
00170         *    Reference to the buffer (RGBA or BGRA)
00171         *  @param[in]      nNumOfPixels
00172         *    Buffer size (width*height => number of pixels)
00173         *
00174         *  @note
00175         *    - This function is internally assembler optimizes for maximum performance
00176         */
00177         static PLGRAPHICS_API void SwapRB(PLCore::uint8 nBuffer[], PLCore::uint32 nNumOfPixels);
00178 
00179 
00180     //[-------------------------------------------------------]
00181     //[ Public functions                                      ]
00182     //[-------------------------------------------------------]
00183     public:
00184         /**
00185         *  @brief
00186         *    Constructor (all components are set to 0.0)
00187         */
00188         inline Color4();
00189 
00190         /**
00191         *  @brief
00192         *    Constructor
00193         *
00194         *  @param[in] fR
00195         *    Red color component (0.0-1.0)
00196         *  @param[in] fG
00197         *    Green color component (0.0-1.0)
00198         *  @param[in] fB
00199         *    Blue color component (0.0-1.0)
00200         *  @param[in] fA
00201         *    Alpha color component (0.0-1.0)
00202         */
00203         inline Color4(float fR, float fG, float fB, float fA);
00204 
00205         /**
00206         *  @brief
00207         *    Constructor
00208         *
00209         *  @param[in] nR
00210         *    Red color component (0-255)
00211         *  @param[in] nG
00212         *    Green color component (0-255)
00213         *  @param[in] nB
00214         *    Blue color component (0-255)
00215         *  @param[in] nA
00216         *    Alpha color component (0-255)
00217         */
00218         inline Color4(PLCore::uint8 nR, PLCore::uint8 nG, PLCore::uint8 nB, PLCore::uint8 nA);
00219 
00220         /**
00221         *  @brief
00222         *    Constructor
00223         *
00224         *  @param[in] fColor
00225         *    Float array holding the color, MUST have 4 elements! (0.0-1.0)
00226         */
00227         inline Color4(const float fColor[]);
00228 
00229         /**
00230         *  @brief
00231         *    Constructor
00232         *
00233         *  @param[in] nColor
00234         *    PLCore::uint8 array holding the color, MUST have 4 elements! (0-255)
00235         */
00236         inline Color4(const PLCore::uint8 nColor[]);
00237 
00238         /**
00239         *  @brief
00240         *    Constructor
00241         *
00242         *  @param[in] fColor
00243         *    Value for all four color components (0.0-1.0)
00244         */
00245         inline Color4(float fColor);
00246 
00247         /**
00248         *  @brief
00249         *    Constructor
00250         *
00251         *  @param[in] nColor
00252         *    Value for all four color components (0-255)
00253         */
00254         inline Color4(PLCore::uint8 nColor);
00255 
00256         /**
00257         *  @brief
00258         *    Copy constructor
00259         *
00260         *  @param[in] cSource
00261         *    Source color
00262         *  @param[in] fA
00263         *    Alpha color component (0.0-1.0)
00264         */
00265         inline Color4(const Color3 &cSource, float fA = 1.0f);
00266 
00267         /**
00268         *  @brief
00269         *    Copy constructor
00270         *
00271         *  @param[in] cSource
00272         *    Source color
00273         */
00274         inline Color4(const Color4 &cSource);
00275 
00276         /**
00277         *  @brief
00278         *    Destructor
00279         */
00280         inline ~Color4();
00281 
00282         /**
00283         *  @brief
00284         *    Maps this RGBA floating point channels (0.0f to 1.0f range) to PLCore::uint32
00285         *
00286         *  @return
00287         *    This color as PLCore::uint32
00288         */
00289         inline PLCore::uint32 ToUInt32() const;
00290 
00291         /**
00292         *  @brief
00293         *    Maps from a PLCore::uint32 to RGBA floating point channels (0.0f to 1.0f range)
00294         *
00295         *  @param[in] nColor
00296         *    Color as PLCore::uint32
00297         */
00298         inline void FromUInt32(PLCore::uint32 nColor);
00299 
00300         /**
00301         *  @brief
00302         *    Get the color component as float values
00303         *
00304         *  @param[out] fR
00305         *    Will receive the red color component (0.0-1.0)
00306         *  @param[out] fG
00307         *    Will receive the green color component (0.0-1.0)
00308         *  @param[out] fB
00309         *    Will receive the blue color component (0.0-1.0)
00310         *  @param[out] fA
00311         *    Will receive the alpha color component (0.0-1.0)
00312         */
00313         inline void GetRGBA(float &fR, float &fG, float &fB, float &fA) const;
00314 
00315         /**
00316         *  @brief
00317         *    Get the color component as integer values
00318         *
00319         *  @param[out] nR
00320         *    Will receive the red color component (0-255)
00321         *  @param[out] nG
00322         *    Will receive the green color component (0-255)
00323         *  @param[out] nB
00324         *    Will receive the blue color component (0-255)
00325         *  @param[out] nA
00326         *    Will receive the alpha color component (0-255)
00327         */
00328         inline void GetRGBA(PLCore::uint8 &nR, PLCore::uint8 &nG, PLCore::uint8 &nB, PLCore::uint8 &nA) const;
00329 
00330         /**
00331         *  @brief
00332         *    Set the color components by using float values
00333         *
00334         *  @param[in] fR
00335         *    Red color component (0.0-1.0)
00336         *  @param[in] fG
00337         *    Green color component (0.0-1.0)
00338         *  @param[in] fB
00339         *    Blue color component (0.0-1.0)
00340         *  @param[in] fA
00341         *    Alpha color component (0.0-1.0)
00342         */
00343         inline void SetRGBA(float fR, float fG, float fB, float fA);
00344 
00345         /**
00346         *  @brief
00347         *    Set the color components by using integer values
00348         *
00349         *  @param[in] nR
00350         *    Red color component (0-255)
00351         *  @param[in] nG
00352         *    Green color component (0-255)
00353         *  @param[in] nB
00354         *    Blue color component (0-255)
00355         *  @param[in] nA
00356         *    Alpha color component (0-255)
00357         */
00358         inline void SetRGBA(PLCore::uint8 nR, PLCore::uint8 nG, PLCore::uint8 nB, PLCore::uint8 nA);
00359 
00360         /**
00361         *  @brief
00362         *    Get the color component as array of float values
00363         *
00364         *  @param[out] fColor
00365         *    Reference to float array receiving the color values, MUST have 4 elements! (0.0-1.0)
00366         */
00367         inline void GetRGBA(float fColor[]) const;
00368 
00369         /**
00370         *  @brief
00371         *    Get the color component as array of integer values
00372         *
00373         *  @param[out] nColor
00374         *    Reference to PLCore::uint8 array receiving the color values, MUST have 4 elements! (0-255)
00375         */
00376         inline void GetRGBA(PLCore::uint8 nColor[]) const;
00377 
00378         /**
00379         *  @brief
00380         *    Set the color components by using a reference to float values
00381         *
00382         *  @param[in] fColor
00383         *    Reference to float array containing the color value, MUST have 4 elements! (0.0-1.0)
00384         */
00385         inline void SetRGBA(const float fColor[]);
00386 
00387         /**
00388         *  @brief
00389         *    Set the color components by using a reference to integer values
00390         *
00391         *  @param[in] nColor
00392         *    Reference to PLCore::uint8 array containing the color value, MUST have 4 elements! (0-255)
00393         */
00394         inline void SetRGBA(const PLCore::uint8 nColor[]);
00395 
00396         /**
00397         *  @brief
00398         *    Set all color component to the same float value
00399         *
00400         *  @param[in] fValue
00401         *    Value for all four color component (0.0-1.0)
00402         */
00403         inline void SetRGBA(float fValue);
00404 
00405         /**
00406         *  @brief
00407         *    Set all color component to the same integer value
00408         *
00409         *  @param[in] nColor
00410         *    Value for all four color component (0-255)
00411         */
00412         inline void SetRGBA(PLCore::uint8 nColor);
00413 
00414         /**
00415         *  @brief
00416         *    Get the red color value as float
00417         *
00418         *  @return
00419         *    Red value (0.0-1.0)
00420         */
00421         inline float GetR() const;
00422 
00423         /**
00424         *  @brief
00425         *    Get the red color value as integer
00426         *
00427         *  @return
00428         *    Red value (0-255)
00429         */
00430         inline PLCore::uint8 GetRInt() const;
00431 
00432         /**
00433         *  @brief
00434         *    Set the red color component using a float value
00435         *
00436         *  @param[in] fR
00437         *    Red value (0.0-1.0)
00438         */
00439         inline void SetR(float fR);
00440 
00441         /**
00442         *  @brief
00443         *    Set the red color component using an integer value
00444         *
00445         *  @param[in] nR
00446         *    Red value (0-255)
00447         */
00448         inline void SetR(PLCore::uint8 nR);
00449 
00450         /**
00451         *  @brief
00452         *    Get the green color value as float
00453         *
00454         *  @return
00455         *    Green value (0.0-1.0)
00456         */
00457         inline float GetG() const;
00458 
00459         /**
00460         *  @brief
00461         *    Get the green color value as integer
00462         *
00463         *  @return
00464         *    Green value (0-255)
00465         */
00466         inline PLCore::uint8 GetGInt() const;
00467 
00468         /**
00469         *  @brief
00470         *    Set the green color component using a float value
00471         *
00472         *  @param[in] fG
00473         *    Green value (0.0-1.0)
00474         */
00475         inline void SetG(float fG);
00476 
00477         /**
00478         *  @brief
00479         *    Set the green color component using an integer value
00480         *
00481         *  @param[in] nG
00482         *    Green value (0-255)
00483         */
00484         inline void SetG(PLCore::uint8 nG);
00485 
00486         /**
00487         *  @brief
00488         *    Get the blue color value as float
00489         *
00490         *  @return
00491         *    Blue value (0.0-1.0)
00492         */
00493         inline float GetB() const;
00494 
00495         /**
00496         *  @brief
00497         *    Get the blue color value as integer
00498         *
00499         *  @return
00500         *    Blue value (0-255)
00501         */
00502         inline PLCore::uint8 GetBInt() const;
00503 
00504         /**
00505         *  @brief
00506         *    Set the blue color component using a float value
00507         *
00508         *  @param[in] fB
00509         *    Blue value (0.0-1.0)
00510         */
00511         inline void SetB(float fB);
00512 
00513         /**
00514         *  @brief
00515         *    Set the blue color component using an integer value
00516         *
00517         *  @param[in] nB
00518         *    Blue value (0-255)
00519         */
00520         inline void SetB(PLCore::uint8 nB);
00521 
00522         /**
00523         *  @brief
00524         *    Get the alpha color value as float
00525         *
00526         *  @return
00527         *    Alpha value (0.0-1.0)
00528         */
00529         inline float GetA() const;
00530 
00531         /**
00532         *  @brief
00533         *    Get the alpha color value as integer
00534         *
00535         *  @return
00536         *    Alpha value (0-255)
00537         */
00538         inline PLCore::uint8 GetAInt() const;
00539 
00540         /**
00541         *  @brief
00542         *    Set the alpha color component using a float value
00543         *
00544         *  @param[in] fA
00545         *    Alpha value (0.0-1.0)
00546         */
00547         inline void SetA(float fA);
00548 
00549         /**
00550         *  @brief
00551         *    Set the alpha color component using an integer value
00552         *
00553         *  @param[in] nA
00554         *    Alpha value (0-255)
00555         */
00556         inline void SetA(PLCore::uint8 nA);
00557 
00558         /**
00559         *  @brief
00560         *    Returns if the color is valid
00561         *
00562         *  @return
00563         *    'true' if the color is valid, else 'false'
00564         */
00565         inline bool IsValid() const;
00566 
00567         /**
00568         *  @brief
00569         *    Returns if the color is transparent
00570         *
00571         *  @return
00572         *    'true', if the color is transparent, else 'false'
00573         */
00574         inline bool IsTransparent() const;
00575 
00576         /**
00577         *  @brief
00578         *    Set the color to transparent
00579         *
00580         *  @note
00581         *    - Sets all components to 0.0
00582         */
00583         inline void SetTransparent();
00584 
00585         /**
00586         *  @brief
00587         *    Clamps the color values between 0.0 and 1.0
00588         */
00589         inline void Saturate();
00590 
00591         /**
00592         *  @brief
00593         *    Get luminance of color as float
00594         *
00595         *  @return
00596         *    Luminance value (0.0-1.0)
00597         *
00598         *  @note
00599         *    - "human eye formula" (red*0.299 + green*0.587 + blue*0.114) is used
00600         */
00601         inline float GetLuminance() const;
00602 
00603         /**
00604         *  @brief
00605         *    Get luminance of color as integer
00606         *
00607         *  @return
00608         *    Luminance (0-255)
00609         *
00610         *  @see
00611         *    - GetLuminance()
00612         */
00613         inline PLCore::uint8 GetLuminanceInt() const;
00614 
00615         /**
00616         *  @brief
00617         *    Get luminance as color (results in a grayscale color)
00618         *
00619         *  @return
00620         *    Luminance color (grayscale)
00621         *
00622         *  @see
00623         *    - GetLuminance()
00624         */
00625         inline Color4 GetLuminanceColor() const;
00626 
00627         //[-------------------------------------------------------]
00628         //[ Assignment operators                                  ]
00629         //[-------------------------------------------------------]
00630         inline Color4 &operator =(const Color4 &cC);
00631         inline Color4 &operator =(const Color3 &cC);
00632         inline Color4 &operator =(const float fC[]);
00633         inline Color4 &operator =(float fD);
00634         inline operator float *();
00635         inline operator const float *() const;
00636 
00637         //[-------------------------------------------------------]
00638         //[ Comparison                                            ]
00639         //[-------------------------------------------------------]
00640         inline bool operator ==(const Color4 &cC) const;
00641         inline bool operator !=(const Color4 &cC) const;
00642         inline bool operator ==(const Color3 &cC) const;
00643         inline bool operator !=(const Color3 &cC) const;
00644         inline bool operator ==(float f) const;
00645         inline bool operator !=(float f) const;
00646 
00647         //[-------------------------------------------------------]
00648         //[ Misc                                                  ]
00649         //[-------------------------------------------------------]
00650         inline Color4  operator +(const Color4 &cC) const;
00651         inline Color4  operator +(float fN) const;
00652         inline Color4 &operator +=(const Color4 &cC);
00653         inline Color4 &operator +=(float fN);
00654         inline Color4  operator -() const;
00655         inline Color4  operator -(const Color4 &cC) const;
00656         inline Color4  operator -(float fN) const;
00657         inline Color4 &operator -=(const Color4 &cC);
00658         inline Color4 &operator -=(float fN);
00659         inline Color4  operator *(const Color4 &cC) const;
00660         inline Color4  operator *(float fS) const;
00661         inline Color4 &operator *=(const Color4 &cC);
00662         inline Color4 &operator *=(float fS);
00663         inline Color4  operator /(const Color4 &cC) const;
00664         inline Color4  operator /(float fS) const;
00665         inline Color4 &operator /=(const Color4 &cC);
00666         inline Color4 &operator /=(float fS);
00667         inline float  &operator [](int nIndex);
00668 
00669         /**
00670         *  @brief
00671         *    To string
00672         *
00673         *  @return
00674         *    String with the data
00675         */
00676         inline PLCore::String ToString() const;
00677 
00678         /**
00679         *  @brief
00680         *    From string
00681         *
00682         *  @param[in] sString
00683         *    String with the data
00684         */
00685         PLGRAPHICS_API bool FromString(const PLCore::String &sString);
00686 
00687 
00688 };
00689 
00690 
00691 //[-------------------------------------------------------]
00692 //[ Namespace                                             ]
00693 //[-------------------------------------------------------]
00694 } // PLGraphics
00695 
00696 
00697 //[-------------------------------------------------------]
00698 //[ Implementation                                        ]
00699 //[-------------------------------------------------------]
00700 #include "PLGraphics/Color/Color4.inl"
00701 #include "PLGraphics/Color/TypeColor4.inl"
00702 
00703 
00704 #endif // __PLGRAPHICS_COLOR4_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:52
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported