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