1 // Copyright (c) 2016 OPEN CASCADE SAS 2 // 3 // This file is part of Open CASCADE Technology software library. 4 // 5 // This library is free software; you can redistribute it and/or modify it under 6 // the terms of the GNU Lesser General Public License version 2.1 as published 7 // by the Free Software Foundation, with special exception defined in the file 8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 9 // distribution for complete text of the license and disclaimer of any warranty. 10 // 11 // Alternatively, this file may be used under the terms of Open CASCADE 12 // commercial license or contractual agreement. 13 14 #ifndef _Quantity_ColorRGBA_HeaderFile 15 #define _Quantity_ColorRGBA_HeaderFile 16 17 #include <Quantity_Color.hxx> 18 #include <Standard_Assert.hxx> 19 20 //! The pair of Quantity_Color and Alpha component (1.0 opaque, 0.0 transparent). 21 class Quantity_ColorRGBA 22 { 23 public: 24 25 //! Creates a color with the default value. Quantity_ColorRGBA()26 Quantity_ColorRGBA() : myAlpha (1.0f) {} 27 28 //! Creates the color with specified RGB value. Quantity_ColorRGBA(const Quantity_Color & theRgb)29 explicit Quantity_ColorRGBA (const Quantity_Color& theRgb) 30 : myRgb (theRgb), myAlpha (1.0f) 31 {} 32 33 //! Creates the color with specified RGBA values. Quantity_ColorRGBA(const Quantity_Color & theRgb,float theAlpha)34 Quantity_ColorRGBA (const Quantity_Color& theRgb, float theAlpha) 35 : myRgb (theRgb), myAlpha (theAlpha) 36 {} 37 38 //! Creates the color from RGBA vector. Quantity_ColorRGBA(const NCollection_Vec4<float> & theRgba)39 explicit Quantity_ColorRGBA (const NCollection_Vec4<float>& theRgba) 40 : myRgb (theRgba.rgb()), myAlpha (theRgba.a()) 41 {} 42 43 //! Creates the color from RGBA values. Quantity_ColorRGBA(float theRed,float theGreen,float theBlue,float theAlpha)44 Quantity_ColorRGBA (float theRed, float theGreen, float theBlue, float theAlpha) 45 : myRgb (theRed, theGreen, theBlue, Quantity_TOC_RGB), 46 myAlpha (theAlpha) 47 {} 48 49 //! Assign new values to the color. SetValues(float theRed,float theGreen,float theBlue,float theAlpha)50 void SetValues (float theRed, float theGreen, float theBlue, float theAlpha) 51 { 52 myRgb.SetValues (theRed, theGreen, theBlue, Quantity_TOC_RGB); 53 myAlpha = theAlpha; 54 } 55 56 //! Return RGB color value. GetRGB() const57 const Quantity_Color& GetRGB() const { return myRgb; } 58 59 //! Modify RGB color components without affecting alpha value. ChangeRGB()60 Quantity_Color& ChangeRGB() { return myRgb; } 61 62 //! Assign RGB color components without affecting alpha value. SetRGB(const Quantity_Color & theRgb)63 void SetRGB (const Quantity_Color& theRgb) { myRgb = theRgb; } 64 65 //! Return alpha value (1.0 means opaque, 0.0 means fully transparent). Alpha() const66 Standard_ShortReal Alpha() const { return myAlpha; } 67 68 //! Assign the alpha value. SetAlpha(const Standard_ShortReal theAlpha)69 void SetAlpha (const Standard_ShortReal theAlpha) { myAlpha = theAlpha; } 70 71 //! Return the color as vector of 4 float elements. operator const NCollection_Vec4<float>&() const72 operator const NCollection_Vec4<float>&() const { return *(const NCollection_Vec4<float>* )this; } 73 74 //! Returns true if the distance between colors is greater than Epsilon(). IsDifferent(const Quantity_ColorRGBA & theOther) const75 bool IsDifferent (const Quantity_ColorRGBA& theOther) const 76 { 77 return myRgb.IsDifferent (theOther.GetRGB()) 78 || Abs(myAlpha - theOther.myAlpha) > (float )Quantity_Color::Epsilon(); 79 } 80 81 //! Returns true if the distance between colors is greater than Epsilon(). operator !=(const Quantity_ColorRGBA & theOther) const82 bool operator!= (const Quantity_ColorRGBA& theOther) const { return IsDifferent (theOther); } 83 84 //! Two colors are considered to be equal if their distance is no greater than Epsilon(). IsEqual(const Quantity_ColorRGBA & theOther) const85 bool IsEqual (const Quantity_ColorRGBA& theOther) const 86 { 87 return myRgb.IsEqual (theOther.GetRGB()) 88 && Abs(myAlpha - theOther.myAlpha) <= (float )Quantity_Color::Epsilon(); 89 } 90 91 //! Two colors are considered to be equal if their distance is no greater than Epsilon(). operator ==(const Quantity_ColorRGBA & theOther) const92 bool operator== (const Quantity_ColorRGBA& theOther) const { return IsEqual (theOther); } 93 94 public: 95 96 //! Finds color from predefined names. 97 //! For example, the name of the color which corresponds to "BLACK" is Quantity_NOC_BLACK. 98 //! An alpha component is set to 1.0. 99 //! @param theColorNameString the color name 100 //! @param theColor a found color 101 //! @return false if the color name is unknown, or true if the search by color name was successful ColorFromName(const Standard_CString theColorNameString,Quantity_ColorRGBA & theColor)102 static Standard_Boolean ColorFromName (const Standard_CString theColorNameString, Quantity_ColorRGBA& theColor) 103 { 104 Quantity_ColorRGBA aColor; 105 if (!Quantity_Color::ColorFromName (theColorNameString, aColor.ChangeRGB())) 106 { 107 return false; 108 } 109 theColor = aColor; 110 return true; 111 } 112 113 //! Parses the string as a hex color (like "#FF0" for short sRGB color, "#FF0F" for short sRGBA color, 114 //! "#FFFF00" for RGB color, or "#FFFF00FF" for RGBA color) 115 //! @param theHexColorString the string to be parsed 116 //! @param theColor a color that is a result of parsing 117 //! @param theAlphaComponentIsOff the flag that indicates if a color alpha component is presented 118 //! in the input string (false) or not (true) 119 //! @return true if parsing was successful, or false otherwise 120 Standard_EXPORT static bool ColorFromHex (const char* const theHexColorString, 121 Quantity_ColorRGBA& theColor, 122 const bool theAlphaComponentIsOff = false); 123 124 //! Returns hex sRGBA string in format "#RRGGBBAA". ColorToHex(const Quantity_ColorRGBA & theColor,const bool theToPrefixHash=true)125 static TCollection_AsciiString ColorToHex (const Quantity_ColorRGBA& theColor, 126 const bool theToPrefixHash = true) 127 { 128 NCollection_Vec4<Standard_ShortReal> anSRgb = Convert_LinearRGB_To_sRGB ((NCollection_Vec4<Standard_ShortReal> )theColor); 129 NCollection_Vec4<Standard_Integer> anSRgbInt (anSRgb * 255.0f + NCollection_Vec4<Standard_ShortReal> (0.5f)); 130 char aBuff[12]; 131 Sprintf (aBuff, theToPrefixHash ? "#%02X%02X%02X%02X" : "%02X%02X%02X%02X", 132 anSRgbInt.r(), anSRgbInt.g(), anSRgbInt.b(), anSRgbInt.a()); 133 return aBuff; 134 } 135 136 public: 137 138 //! Convert linear RGB components into sRGB using OpenGL specs formula. Convert_LinearRGB_To_sRGB(const NCollection_Vec4<float> & theRGB)139 static NCollection_Vec4<float> Convert_LinearRGB_To_sRGB (const NCollection_Vec4<float>& theRGB) 140 { 141 return NCollection_Vec4<float> (Quantity_Color::Convert_LinearRGB_To_sRGB (theRGB.r()), 142 Quantity_Color::Convert_LinearRGB_To_sRGB (theRGB.g()), 143 Quantity_Color::Convert_LinearRGB_To_sRGB (theRGB.b()), 144 theRGB.a()); 145 } 146 147 //! Convert sRGB components into linear RGB using OpenGL specs formula. Convert_sRGB_To_LinearRGB(const NCollection_Vec4<float> & theRGB)148 static NCollection_Vec4<float> Convert_sRGB_To_LinearRGB (const NCollection_Vec4<float>& theRGB) 149 { 150 return NCollection_Vec4<float> (Quantity_Color::Convert_sRGB_To_LinearRGB (theRGB.r()), 151 Quantity_Color::Convert_sRGB_To_LinearRGB (theRGB.g()), 152 Quantity_Color::Convert_sRGB_To_LinearRGB (theRGB.b()), 153 theRGB.a()); 154 } 155 156 public: 157 158 //! Dumps the content of me into the stream 159 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const; 160 161 //! Inits the content of me from the stream 162 Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos); 163 164 private: 165 myTestSize3()166 static void myTestSize3() { Standard_STATIC_ASSERT (sizeof(float) * 3 == sizeof(Quantity_Color)); } myTestSize4()167 static void myTestSize4() { Standard_STATIC_ASSERT (sizeof(float) * 4 == sizeof(Quantity_ColorRGBA)); } 168 169 private: 170 171 Quantity_Color myRgb; 172 Standard_ShortReal myAlpha; 173 174 }; 175 176 #endif // _Quantity_ColorRGBA_HeaderFile 177