File indexing completed on 2025-11-03 09:54:31
0001 
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 #ifndef _Quantity_ColorRGBA_HeaderFile
0015 #define _Quantity_ColorRGBA_HeaderFile
0016 
0017 #include <Quantity_Color.hxx>
0018 #include <Standard_Assert.hxx>
0019 
0020 
0021 class Quantity_ColorRGBA
0022 {
0023 public:
0024 
0025   
0026   Quantity_ColorRGBA() : myAlpha (1.0f) {}
0027 
0028   
0029   explicit Quantity_ColorRGBA (const Quantity_Color& theRgb)
0030   : myRgb (theRgb), myAlpha (1.0f)
0031   {}
0032 
0033   
0034   Quantity_ColorRGBA (const Quantity_Color& theRgb, float theAlpha)
0035   : myRgb (theRgb), myAlpha (theAlpha)
0036   {}
0037 
0038   
0039   explicit Quantity_ColorRGBA (const NCollection_Vec4<float>& theRgba) 
0040   : myRgb (theRgba.rgb()), myAlpha (theRgba.a())
0041   {}
0042 
0043   
0044   Quantity_ColorRGBA (float theRed, float theGreen, float theBlue, float theAlpha)
0045   : myRgb (theRed, theGreen, theBlue, Quantity_TOC_RGB),
0046     myAlpha (theAlpha)
0047   {}
0048 
0049   
0050   void SetValues (float theRed, float theGreen, float theBlue, float theAlpha)
0051   {
0052     myRgb.SetValues (theRed, theGreen, theBlue, Quantity_TOC_RGB);
0053     myAlpha = theAlpha;
0054   }
0055 
0056   
0057   const Quantity_Color& GetRGB() const { return myRgb; }
0058 
0059   
0060   Quantity_Color& ChangeRGB() { return myRgb; }
0061 
0062   
0063   void SetRGB (const Quantity_Color& theRgb) { myRgb = theRgb; }
0064 
0065   
0066   Standard_ShortReal Alpha() const { return myAlpha; }
0067 
0068   
0069   void SetAlpha (const Standard_ShortReal theAlpha) { myAlpha = theAlpha; }
0070 
0071   
0072   operator const NCollection_Vec4<float>&() const { return *(const NCollection_Vec4<float>* )this; }
0073 
0074   
0075   bool IsDifferent (const Quantity_ColorRGBA& theOther) const
0076   {
0077     return myRgb.IsDifferent (theOther.GetRGB())
0078         || Abs(myAlpha - theOther.myAlpha) > (float )Quantity_Color::Epsilon();
0079   }
0080 
0081   
0082   bool operator!= (const Quantity_ColorRGBA& theOther) const { return IsDifferent (theOther); }
0083 
0084   
0085   bool IsEqual (const Quantity_ColorRGBA& theOther) const
0086   {
0087     return myRgb.IsEqual (theOther.GetRGB())
0088         && Abs(myAlpha - theOther.myAlpha) <= (float )Quantity_Color::Epsilon();
0089   }
0090 
0091   
0092   bool operator== (const Quantity_ColorRGBA& theOther) const { return IsEqual (theOther); }
0093 
0094 public:
0095 
0096   
0097   
0098   
0099   
0100   
0101   
0102   static Standard_Boolean ColorFromName (const Standard_CString theColorNameString, Quantity_ColorRGBA& theColor)
0103   {
0104     Quantity_ColorRGBA aColor;
0105     if (!Quantity_Color::ColorFromName (theColorNameString, aColor.ChangeRGB()))
0106     {
0107       return false;
0108     }
0109     theColor = aColor;
0110     return true;
0111   }
0112 
0113   
0114   
0115   
0116   
0117   
0118   
0119   
0120   Standard_EXPORT static bool ColorFromHex (const char* const   theHexColorString,
0121                                             Quantity_ColorRGBA& theColor,
0122                                             const bool          theAlphaComponentIsOff = false);
0123 
0124   
0125   static TCollection_AsciiString ColorToHex (const Quantity_ColorRGBA& theColor,
0126                                              const bool theToPrefixHash = true)
0127   {
0128     NCollection_Vec4<Standard_ShortReal> anSRgb = Convert_LinearRGB_To_sRGB ((NCollection_Vec4<Standard_ShortReal> )theColor);
0129     NCollection_Vec4<Standard_Integer> anSRgbInt (anSRgb * 255.0f + NCollection_Vec4<Standard_ShortReal> (0.5f));
0130     char aBuff[12];
0131     Sprintf (aBuff, theToPrefixHash ? "#%02X%02X%02X%02X" : "%02X%02X%02X%02X",
0132              anSRgbInt.r(), anSRgbInt.g(), anSRgbInt.b(), anSRgbInt.a());
0133     return aBuff;
0134   }
0135 
0136 public:
0137 
0138   
0139   static NCollection_Vec4<float> Convert_LinearRGB_To_sRGB (const NCollection_Vec4<float>& theRGB)
0140   {
0141     return NCollection_Vec4<float> (Quantity_Color::Convert_LinearRGB_To_sRGB (theRGB.r()),
0142                                     Quantity_Color::Convert_LinearRGB_To_sRGB (theRGB.g()),
0143                                     Quantity_Color::Convert_LinearRGB_To_sRGB (theRGB.b()),
0144                                     theRGB.a());
0145   }
0146 
0147   
0148   static NCollection_Vec4<float> Convert_sRGB_To_LinearRGB (const NCollection_Vec4<float>& theRGB)
0149   {
0150     return NCollection_Vec4<float> (Quantity_Color::Convert_sRGB_To_LinearRGB (theRGB.r()),
0151                                     Quantity_Color::Convert_sRGB_To_LinearRGB (theRGB.g()),
0152                                     Quantity_Color::Convert_sRGB_To_LinearRGB (theRGB.b()),
0153                                     theRGB.a());
0154   }
0155 
0156 public:
0157 
0158   
0159   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0160 
0161   
0162   Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
0163 
0164 private:
0165 
0166   static void myTestSize3() { Standard_STATIC_ASSERT (sizeof(float) * 3 == sizeof(Quantity_Color)); }
0167   static void myTestSize4() { Standard_STATIC_ASSERT (sizeof(float) * 4 == sizeof(Quantity_ColorRGBA)); }
0168 
0169 private:
0170 
0171   Quantity_Color     myRgb;
0172   Standard_ShortReal myAlpha;
0173 
0174 };
0175 
0176 namespace std
0177 {
0178   template <>
0179   struct hash<Quantity_ColorRGBA>
0180   {
0181     std::size_t operator()(const Quantity_ColorRGBA& theColor) const noexcept
0182     {
0183       const Quantity_Color& anRGB = theColor.GetRGB();
0184       unsigned char aByteArr[4] = { static_cast<unsigned char>(100 * theColor.Alpha()),
0185                                     static_cast<unsigned char>(255 * anRGB.Red()),
0186                                     static_cast<unsigned char>(255 * anRGB.Green()),
0187                                     static_cast<unsigned char>(255 * anRGB.Blue()) };
0188       return opencascade::hashBytes(aByteArr, sizeof(aByteArr));
0189     }
0190   };
0191 }
0192 #endif