// gColor.h #ifndef gColorh #define gColorh #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "../../Global.h" // For MinMax, modf and M_PI class gColor { double R,G,B,A; static double FromByte(int B); static int sgn(double c); // to Compare a double value with zero using a tolerance of 1/256.0 int Compare(double a, double b) const; public: enum {NoColor=0xFFFFFFFF}; // Used to indicate an unset color (Full Alpha==transparent) explicit gColor(bool Grey); // Random Color explicit gColor(BYTE R, BYTE G, BYTE B, BYTE A=0); // 0-255 explicit gColor(int R, int G, int B, int A=0); // 0-255 explicit gColor(float R, float G, float B, float A=0); // 0-1 explicit gColor(const double& R, const double& G, const double& B, const double& A=0); // 0-1 explicit gColor(COLORREF Color=NoColor); explicit gColor(const double& t); // t in the Interval [0,1] Sets from Hue (pick a color from a rainbow spectrum) virtual ~gColor(); void SetRGB (BYTE R, BYTE G, BYTE B, BYTE A=0); // 0-255 void SetRGB (int R, int G, int B, int A=0); // 0-255 void SetRGB (float R, float G, float B, float A=0); // 0-1 void SetRGB (const double& R, const double& G, const double& B, const double& A=0); // 0-1 void SetBGR (DWORD Color=NoColor); void SetBGRA(DWORD Color=NoColor); void SetRGB (DWORD Color=NoColor); void SetRGBA(DWORD Color=NoColor); void SetCOLORREF(COLORREF Color=NoColor); // Obvious one for MFC users void Set(DWORD Color=NoColor); void SetLevel(BYTE Level); // Grey Scale Level void SetR(BYTE c); void SetG(BYTE c); void SetB(BYTE c); void SetA(BYTE c); void SetR(float c); void SetG(float c); void SetB(float c); void SetA(float c); void SetR(const double& c); void SetG(const double& c); void SetB(const double& c); void SetA(const double& c); void SetFromHue(const double& H); // Interval [0,1] void SetFromHSL(const double& H, const double& S, const double& L); // Hue, Saturation, Lightness, all in the Interval [0,1] void SetFromHSV(const double& H, const double& S, const double& V); // Hue, Saturation, Value, all in the Interval [0,1] COLORREF GetCOLORREF() const; // Obvious one for MFC users DWORD GetRGB () const; DWORD GetRGBA() const; DWORD GetARGB() const; DWORD GetBGR () const; DWORD GetBGRA() const; BYTE GetRb() const; // Red 0-255 BYTE GetGb() const; // Green 0-255 BYTE GetBb() const; // Blue 0-255 BYTE GetAb() const; // Alpha 0-255 int GetRi() const; // Red 0-255 int GetGi() const; // Green 0-255 int GetBi() const; // Blue 0-255 int GetAi() const; // Alpha 0-255 float GetRf() const; // Red 0-1 float GetGf() const; // Green 0-1 float GetBf() const; // Blue 0-1 float GetAf() const; // Alpha 0-1 double GetRd() const; // Red 0-1 double GetGd() const; // Green 0-1 double GetBd() const; // Blue 0-1 double GetAd() const; // Alpha 0-1 void GetHSL(double& H, double& S, double& L) const ; // Hue, Saturation, Lightness, all in the Interval [0,1] void GetHSV(double& H, double& S, double& V) const ; // Hue, Saturation, Value, all in the Interval [0,1] void ToRGBAByteArray (BYTE (&Array)[4]) const; void ToRGBAIntArray (int (&Array)[4]) const; void ToRGBAFloatArray (float (&Array)[4]) const; void ToRGBADoubleArray(double (&Array)[4]) const; bool IsNoColor() const; int Compare(const gColor& c) const; bool operator==(COLORREF c) const; // Alpha is ignored! bool operator!=(COLORREF c) const; // Alpha is ignored! bool operator==(const gColor& c) const; // Alpha is ignored! bool operator!=(const gColor& c) const; // Alpha is ignored! gColor& operator*=(const double& d); // d is expected to be in the range [0,1] Alpha is ignored! gColor operator* (const double& d) const; // d is expected to be in the range [0,1] Alpha is ignored! bool IsGrey(BYTE Tolerance=1/256.0); // Tolerance specifies how close to Gray, 0 asks if R,G and B are identical (unlikely with doubles), 0.25 means that there may be a difference of at most 0.25 between two components BYTE GetGreyLevel() const; // Returns 0 to 1,Standard NTSC weights for eye response double GetAverageBrightness() const; // Avarage double GetStandardPerceivedLuminance() const; // Photometric/digital ITU-R double GetPerceivedLuminance() const; // Digital CCIR601 (Use to get Greyscale Levels) Standard NTSC weights for eye response double GetSlowPerceivedLuminance() const; double GetFastPerceivedLuminance() const; void SetSaturation(const double& t); // t in the Interval [0,1] void SetLightness(const double& t); // t is in the Interval [0,1] (Use above Exposure(...) to adjust from raw light levels) void SetValue(const double& t); // t is in the Interval [0,1] (Use above Exposure(...) to adjust from raw light levels) void SetBrightness(const double& t); // t in the Interval [0,1] (Use above Exposure(...) to adjust from raw light levels) void Normalise(); private: // Just for Normalise to use: void Distribute(double& A, double& B, double& C) const; public: //____________________________ Shaders ________________________________________ gColor GetPearlColor(const double& x, const double& y, const double& z) const; // x,y,z vary over the interval [0,1] gColor GetGlassColor(const double& x, const double& y, const double& z) const; // x,y,z vary over the interval [0,1] //____________________________ Mixers ________________________________________ enum MixType {BlendMix,AddMix,MultiplyMix,ScreenMix,OverlayMix,SubtractMix,DivideMix,DifferenceMix,DarkerMix,LighterMix,DodgeMix,BurnMix,HueMix,SaturationMix,ValueMix,ColorMix}; void Mix(MixType Type, const double& t, const gColor& C, bool GreyScale=false); void MixBlend (const double& t, const gColor& C, bool GreyScale=false); void MixMultiply (const double& t, const gColor& C, bool GreyScale=false); void MixScreen (const double& t, const gColor& C, bool GreyScale=false); void MixOverlay (const double& t, const gColor& C, bool GreyScale=false); void MixDivide (const double& t, const gColor& C, bool GreyScale=false); void MixDifference(const double& t, const gColor& C, bool GreyScale=false); void MixSubtract (const double& t, const gColor& C, bool GreyScale=false); void MixAdd (const double& t, const gColor& C, bool GreyScale=false); void MixDarker (const double& t, const gColor& C, bool GreyScale=false); void MixLighter (const double& t, const gColor& C, bool GreyScale=false); void MixDodge (const double& t, const gColor& C, bool GreyScale=false); void MixBurn (const double& t, const gColor& C, bool GreyScale=false); void MixHue (const double& t, const gColor& C); // Slow ones only meaningful for Colour: void MixSaturation(const double& t, const gColor& C); void MixColor (const double& t, const gColor& C); void MixValue (const double& t, const gColor& C); //____________________________ statics ________________________________________ static bool IsNoColor(DWORD Color); static DWORD SwapRedBlue(DWORD Color); // BGR <-> RGB static DWORD Invert(COLORREF Color); static DWORD Mix(COLORREF Color1, COLORREF Color2); // Fast averaging ignoring Alphas static DWORD AlphaBlend(COLORREF Paper, COLORREF Paint, BYTE Transparency); static double GammaCorrect(const double& Brightness, const double& Gamma); static double Expose(const double& LightIn, const double& Exposure); // Auto-adjust brightness static double GetLightPower(const double& WaveLength, const double& Temperature); // Temperature in Kelvin static double Blend (const double& B, const double& t, const double& S, const double& r); static double Multiply (const double& B, const double& t, const double& S, const double& r); static double Screen (const double& B, const double& t, const double& S, const double& r); static double Overlay (const double& B, const double& t, const double& S, const double& r); static double Divide (const double& B, const double& t, const double& S, const double& r); static double Difference(const double& B, const double& t, const double& S, const double& r); static double Subtract (const double& B, const double& t, const double& S ); static double Add (const double& B, const double& t, const double& S ); static double Darker (const double& B, const double& t, const double& S ); static double Lighter (const double& B, const double& t, const double& S ); static double Dodge (const double& B, const double& t, const double& S ); static double Burn (const double& B, const double& t, const double& S, const double& r); }; bool operator==(const gColor& c1, const gColor& c2); bool operator!=(const gColor& c1, const gColor& c2); // Color Spline primitive. Interpolates an array of Colors to return a single color. If not equally spaced, there can be a parallel array holding positions in the color bar for each color in the Color array. struct gColorSpline { int Count; gColor* Colors; double* Positions; enum SplineInterpolationType {LinearCSI,HermiteCSI,bSplineCSI,CatmullRomCSI} InterpolationType; gColorSpline() : Count(0) ,Colors(0), Positions(0), InterpolationType(CatmullRomCSI) {} gColorSpline(int Count, gColor* Colors, double* Positions=0, SplineInterpolationType InterpolationType=CatmullRomCSI) : Count(Count), Colors(Colors), Positions(Positions), InterpolationType(InterpolationType) {} gColor Get(double t) const {return Get(t, Count, Colors, Positions, InterpolationType);} static gColor Get(double t, int Count, gColor* Colors, double* Positions=0, SplineInterpolationType InterpolationType=CatmullRomCSI); }; #endif // ndef gColorh