Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:18:52

0001 // Created by: NW,JPB,CAL
0002 // Copyright (c) 1991-1999 Matra Datavision
0003 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Quantity_Color_HeaderFile
0017 #define _Quantity_Color_HeaderFile
0018 
0019 #include <Standard.hxx>
0020 #include <Standard_DefineAlloc.hxx>
0021 #include <Standard_Handle.hxx>
0022 #include <Standard_HashUtils.hxx>
0023 #include <Standard_ShortReal.hxx>
0024 
0025 #include <Quantity_NameOfColor.hxx>
0026 #include <Quantity_TypeOfColor.hxx>
0027 #include <NCollection_Vec4.hxx>
0028 
0029 //! This class allows the definition of an RGB color as triplet of 3 normalized floating point
0030 //! values (red, green, blue).
0031 //!
0032 //! Although Quantity_Color can be technically used for pass-through storage of RGB triplet in any
0033 //! color space, other OCCT interfaces taking/returning Quantity_Color would expect them in linear
0034 //! space. Therefore, take a look into methods converting to and from non-linear sRGB color space,
0035 //! if needed; for instance, application usually providing color picking within 0..255 range in sRGB
0036 //! color space.
0037 class Quantity_Color
0038 {
0039 public:
0040   DEFINE_STANDARD_ALLOC
0041 
0042   //! Creates Quantity_NOC_YELLOW color (for historical reasons).
0043   Quantity_Color()
0044       : myRgb(valuesOf(Quantity_NOC_YELLOW, Quantity_TOC_RGB))
0045   {
0046   }
0047 
0048   //! Creates the color from enumeration value.
0049   Quantity_Color(const Quantity_NameOfColor theName)
0050       : myRgb(valuesOf(theName, Quantity_TOC_RGB))
0051   {
0052   }
0053 
0054   //! Creates a color according to the definition system theType.
0055   //! Throws exception if values are out of range.
0056   Standard_EXPORT Quantity_Color(const double               theC1,
0057                                  const double               theC2,
0058                                  const double               theC3,
0059                                  const Quantity_TypeOfColor theType);
0060 
0061   //! Define color from linear RGB values.
0062   Standard_EXPORT explicit Quantity_Color(const NCollection_Vec3<float>& theRgb);
0063 
0064   //! Returns the name of the nearest color from the Quantity_NameOfColor enumeration.
0065   Standard_EXPORT Quantity_NameOfColor Name() const;
0066 
0067   //! Updates the color from specified named color.
0068   void SetValues(const Quantity_NameOfColor theName) noexcept
0069   {
0070     myRgb = valuesOf(theName, Quantity_TOC_RGB);
0071   }
0072 
0073   //! Return the color as vector of 3 float elements.
0074   constexpr const NCollection_Vec3<float>& Rgb() const noexcept { return myRgb; }
0075 
0076   //! Return the color as vector of 3 float elements.
0077   constexpr operator const NCollection_Vec3<float>&() const noexcept { return myRgb; }
0078 
0079   //! Returns in theC1, theC2 and theC3 the components of this color
0080   //! according to the color system definition theType.
0081   Standard_EXPORT void Values(double&                    theC1,
0082                               double&                    theC2,
0083                               double&                    theC3,
0084                               const Quantity_TypeOfColor theType) const;
0085 
0086   //! Updates a color according to the mode specified by theType.
0087   //! Throws exception if values are out of range.
0088   Standard_EXPORT void SetValues(const double               theC1,
0089                                  const double               theC2,
0090                                  const double               theC3,
0091                                  const Quantity_TypeOfColor theType);
0092 
0093   //! Returns the Red component (quantity of red) of the color within range [0.0; 1.0].
0094   constexpr double Red() const noexcept { return myRgb.r(); }
0095 
0096   //! Returns the Green component (quantity of green) of the color within range [0.0; 1.0].
0097   constexpr double Green() const noexcept { return myRgb.g(); }
0098 
0099   //! Returns the Blue component (quantity of blue) of the color within range [0.0; 1.0].
0100   constexpr double Blue() const noexcept { return myRgb.b(); }
0101 
0102   //! Returns the Hue component (hue angle) of the color
0103   //! in degrees within range [0.0; 360.0], 0.0 being Red.
0104   //! -1.0 is a special value reserved for grayscale color (S should be 0.0)
0105   double Hue() const noexcept { return Convert_LinearRGB_To_HLS(myRgb)[0]; }
0106 
0107   //! Returns the Light component (value of the lightness) of the color within range [0.0; 1.0].
0108   double Light() const noexcept { return Convert_LinearRGB_To_HLS(myRgb)[1]; }
0109 
0110   //! Increases or decreases the intensity (variation of the lightness).
0111   //! The delta is a percentage. Any value greater than zero will increase the intensity.
0112   //! The variation is expressed as a percentage of the current value.
0113   Standard_EXPORT void ChangeIntensity(const double theDelta);
0114 
0115   //! Returns the Saturation component (value of the saturation) of the color within range
0116   //! [0.0; 1.0].
0117   double Saturation() const noexcept { return Convert_LinearRGB_To_HLS(myRgb)[2]; }
0118 
0119   //! Increases or decreases the contrast (variation of the saturation).
0120   //! The delta is a percentage. Any value greater than zero will increase the contrast.
0121   //! The variation is expressed as a percentage of the current value.
0122   Standard_EXPORT void ChangeContrast(const double theDelta);
0123 
0124   //! Returns TRUE if the distance between two colors is greater than Epsilon().
0125   bool IsDifferent(const Quantity_Color& theOther) const noexcept
0126   {
0127     return (SquareDistance(theOther) > Epsilon() * Epsilon());
0128   }
0129 
0130   //! Alias to IsDifferent().
0131   bool operator!=(const Quantity_Color& theOther) const noexcept { return IsDifferent(theOther); }
0132 
0133   //! Returns TRUE if the distance between two colors is no greater than Epsilon().
0134   bool IsEqual(const Quantity_Color& theOther) const noexcept
0135   {
0136     return (SquareDistance(theOther) <= Epsilon() * Epsilon());
0137   }
0138 
0139   //! Alias to IsEqual().
0140   bool operator==(const Quantity_Color& theOther) const noexcept { return IsEqual(theOther); }
0141 
0142   //! Returns the distance between two colors. It's a value between 0 and the square root of 3 (the
0143   //! black/white distance).
0144   double Distance(const Quantity_Color& theColor) const noexcept
0145   {
0146     return (NCollection_Vec3<double>(myRgb) - NCollection_Vec3<double>(theColor.myRgb)).Modulus();
0147   }
0148 
0149   //! Returns the square of distance between two colors.
0150   double SquareDistance(const Quantity_Color& theColor) const noexcept
0151   {
0152     return (NCollection_Vec3<double>(myRgb) - NCollection_Vec3<double>(theColor.myRgb))
0153       .SquareModulus();
0154   }
0155 
0156   //! Returns the percentage change of contrast and intensity between this and another color.
0157   //! <DC> and <DI> are percentages, either positive or negative.
0158   //! The calculation is with respect to this color.
0159   //! If <DC> is positive then <me> is more contrasty.
0160   //! If <DI> is positive then <me> is more intense.
0161   Standard_EXPORT void Delta(const Quantity_Color& theColor, double& DC, double& DI) const;
0162 
0163   //! Returns the value of the perceptual difference between this color
0164   //! and @p theOther, computed using the CIEDE2000 formula.
0165   //! The difference is in range [0, 100.], with 1 approximately corresponding
0166   //! to the minimal perceivable difference (usually difference 5 or greater is
0167   //! needed for the difference to be recognizable in practice).
0168   Standard_EXPORT double DeltaE2000(const Quantity_Color& theOther) const;
0169 
0170 public:
0171   //! Returns the color from Quantity_NameOfColor enumeration nearest to specified RGB values.
0172   static Quantity_NameOfColor Name(const double theR, const double theG, const double theB)
0173   {
0174     const Quantity_Color aColor(theR, theG, theB, Quantity_TOC_RGB);
0175     return aColor.Name();
0176   }
0177 
0178   //! Returns the name of the color identified by the given Quantity_NameOfColor enumeration value.
0179   Standard_EXPORT static const char* StringName(const Quantity_NameOfColor theColor) noexcept;
0180 
0181   //! Finds color from predefined names.
0182   //! For example, the name of the color which corresponds to "BLACK" is Quantity_NOC_BLACK.
0183   //! Returns FALSE if name is unknown.
0184   Standard_EXPORT static bool ColorFromName(const char* const     theName,
0185                                             Quantity_NameOfColor& theColor) noexcept;
0186 
0187   //! Finds color from predefined names.
0188   //! @param theColorNameString the color name
0189   //! @param theColor a found color
0190   //! @return false if the color name is unknown, or true if the search by color name was successful
0191   static bool ColorFromName(const char* const theColorNameString, Quantity_Color& theColor) noexcept
0192   {
0193     Quantity_NameOfColor aColorName = Quantity_NOC_BLACK;
0194     if (!ColorFromName(theColorNameString, aColorName))
0195     {
0196       return false;
0197     }
0198     theColor = aColorName;
0199     return true;
0200   }
0201 
0202 public:
0203   //!@name Routines converting colors between different encodings and color spaces
0204 
0205   //! Parses the string as a hex color (like "#FF0" for short sRGB color, or "#FFFF00" for sRGB
0206   //! color)
0207   //! @param theHexColorString the string to be parsed
0208   //! @param theColor a color that is a result of parsing
0209   //! @return true if parsing was successful, or false otherwise
0210   Standard_EXPORT static bool ColorFromHex(const char* const theHexColorString,
0211                                            Quantity_Color&   theColor);
0212 
0213   //! Returns hex sRGB string in format "#FFAAFF".
0214   static TCollection_AsciiString ColorToHex(const Quantity_Color& theColor,
0215                                             const bool            theToPrefixHash = true) noexcept
0216   {
0217     NCollection_Vec3<float> anSRgb = Convert_LinearRGB_To_sRGB((NCollection_Vec3<float>)theColor);
0218     NCollection_Vec3<int>   anSRgbInt(anSRgb * 255.0f + NCollection_Vec3<float>(0.5f));
0219     char                    aBuff[10];
0220     Sprintf(aBuff,
0221             theToPrefixHash ? "#%02X%02X%02X" : "%02X%02X%02X",
0222             anSRgbInt.r(),
0223             anSRgbInt.g(),
0224             anSRgbInt.b());
0225     return aBuff;
0226   }
0227 
0228   //! Converts sRGB components into HLS ones.
0229   Standard_EXPORT static NCollection_Vec3<float> Convert_sRGB_To_HLS(
0230     const NCollection_Vec3<float>& theRgb) noexcept;
0231 
0232   //! Converts HLS components into RGB ones.
0233   Standard_EXPORT static NCollection_Vec3<float> Convert_HLS_To_sRGB(
0234     const NCollection_Vec3<float>& theHls);
0235 
0236   //! Converts Linear RGB components into HLS ones.
0237   static NCollection_Vec3<float> Convert_LinearRGB_To_HLS(
0238     const NCollection_Vec3<float>& theRgb) noexcept
0239   {
0240     return Convert_sRGB_To_HLS(Convert_LinearRGB_To_sRGB(theRgb));
0241   }
0242 
0243   //! Converts HLS components into linear RGB ones.
0244   static NCollection_Vec3<float> Convert_HLS_To_LinearRGB(
0245     const NCollection_Vec3<float>& theHls) noexcept
0246   {
0247     return Convert_sRGB_To_LinearRGB(Convert_HLS_To_sRGB(theHls));
0248   }
0249 
0250   //! Converts linear RGB components into CIE Lab ones.
0251   Standard_EXPORT static NCollection_Vec3<float> Convert_LinearRGB_To_Lab(
0252     const NCollection_Vec3<float>& theRgb) noexcept;
0253 
0254   //! Converts CIE Lab components into CIE Lch ones.
0255   Standard_EXPORT static NCollection_Vec3<float> Convert_Lab_To_Lch(
0256     const NCollection_Vec3<float>& theLab) noexcept;
0257 
0258   //! Converts CIE Lab components into linear RGB ones.
0259   //! Note that the resulting values may be out of the valid range for RGB.
0260   Standard_EXPORT static NCollection_Vec3<float> Convert_Lab_To_LinearRGB(
0261     const NCollection_Vec3<float>& theLab) noexcept;
0262 
0263   //! Converts CIE Lch components into CIE Lab ones.
0264   Standard_EXPORT static NCollection_Vec3<float> Convert_Lch_To_Lab(
0265     const NCollection_Vec3<float>& theLch) noexcept;
0266 
0267   //! Convert the color value to ARGB integer value, with alpha equals to 0.
0268   //! So the output is formatted as 0x00RRGGBB.
0269   //! Note that this unpacking does NOT involve non-linear sRGB -> linear RGB conversion,
0270   //! as would be usually expected for RGB color packed into 4 bytes.
0271   //! @param[in] theColor  color to convert
0272   //! @param[out] theARGB  result color encoded as integer
0273   static constexpr void Color2argb(const Quantity_Color& theColor, int& theARGB) noexcept
0274   {
0275     const NCollection_Vec3<int> aColor(static_cast<int>(255.0f * theColor.myRgb.r() + 0.5f),
0276                                        static_cast<int>(255.0f * theColor.myRgb.g() + 0.5f),
0277                                        static_cast<int>(255.0f * theColor.myRgb.b() + 0.5f));
0278     theARGB = (((aColor.r() & 0xff) << 16) | ((aColor.g() & 0xff) << 8) | (aColor.b() & 0xff));
0279   }
0280 
0281   //! Convert integer ARGB value to Color. Alpha bits are ignored.
0282   //! Note that this packing does NOT involve linear -> non-linear sRGB conversion,
0283   //! as would be usually expected to preserve higher (for human eye) color precision in 4 bytes.
0284   static void Argb2color(const int theARGB, Quantity_Color& theColor) noexcept
0285   {
0286     const NCollection_Vec3<double> aColor(static_cast<double>((theARGB & 0xff0000) >> 16),
0287                                           static_cast<double>((theARGB & 0x00ff00) >> 8),
0288                                           static_cast<double>((theARGB & 0x0000ff)));
0289     theColor.SetValues(aColor.r() / 255.0,
0290                        aColor.g() / 255.0,
0291                        aColor.b() / 255.0,
0292                        Quantity_TOC_sRGB);
0293   }
0294 
0295   //! Convert linear RGB component into sRGB using OpenGL specs formula (double precision), also
0296   //! known as gamma correction.
0297   static double Convert_LinearRGB_To_sRGB(double theLinearValue) noexcept
0298   {
0299     return theLinearValue <= 0.0031308 ? theLinearValue * 12.92
0300                                        : std::pow(theLinearValue, 1.0 / 2.4) * 1.055 - 0.055;
0301   }
0302 
0303   //! Convert linear RGB component into sRGB using OpenGL specs formula (single precision), also
0304   //! known as gamma correction.
0305   static float Convert_LinearRGB_To_sRGB(float theLinearValue) noexcept
0306   {
0307     return theLinearValue <= 0.0031308f ? theLinearValue * 12.92f
0308                                         : powf(theLinearValue, 1.0f / 2.4f) * 1.055f - 0.055f;
0309   }
0310 
0311   //! Convert sRGB component into linear RGB using OpenGL specs formula (double precision), also
0312   //! known as gamma correction.
0313   static double Convert_sRGB_To_LinearRGB(double thesRGBValue) noexcept
0314   {
0315     return thesRGBValue <= 0.04045 ? thesRGBValue / 12.92
0316                                    : std::pow((thesRGBValue + 0.055) / 1.055, 2.4);
0317   }
0318 
0319   //! Convert sRGB component into linear RGB using OpenGL specs formula (single precision), also
0320   //! known as gamma correction.
0321   static float Convert_sRGB_To_LinearRGB(float thesRGBValue) noexcept
0322   {
0323     return thesRGBValue <= 0.04045f ? thesRGBValue / 12.92f
0324                                     : powf((thesRGBValue + 0.055f) / 1.055f, 2.4f);
0325   }
0326 
0327   //! Convert linear RGB components into sRGB using OpenGL specs formula.
0328   template <typename T>
0329   static NCollection_Vec3<T> Convert_LinearRGB_To_sRGB(const NCollection_Vec3<T>& theRGB) noexcept
0330   {
0331     return NCollection_Vec3<T>(Convert_LinearRGB_To_sRGB(theRGB.r()),
0332                                Convert_LinearRGB_To_sRGB(theRGB.g()),
0333                                Convert_LinearRGB_To_sRGB(theRGB.b()));
0334   }
0335 
0336   //! Convert sRGB components into linear RGB using OpenGL specs formula.
0337   template <typename T>
0338   static NCollection_Vec3<T> Convert_sRGB_To_LinearRGB(const NCollection_Vec3<T>& theRGB) noexcept
0339   {
0340     return NCollection_Vec3<T>(Convert_sRGB_To_LinearRGB(theRGB.r()),
0341                                Convert_sRGB_To_LinearRGB(theRGB.g()),
0342                                Convert_sRGB_To_LinearRGB(theRGB.b()));
0343   }
0344 
0345   //! Convert linear RGB component into sRGB using approximated uniform gamma coefficient 2.2.
0346   static float Convert_LinearRGB_To_sRGB_approx22(float theLinearValue) noexcept
0347   {
0348     return powf(theLinearValue, 2.2f);
0349   }
0350 
0351   //! Convert sRGB component into linear RGB using approximated uniform gamma coefficient 2.2
0352   static float Convert_sRGB_To_LinearRGB_approx22(float thesRGBValue) noexcept
0353   {
0354     return powf(thesRGBValue, 1.0f / 2.2f);
0355   }
0356 
0357   //! Convert linear RGB components into sRGB using approximated uniform gamma coefficient 2.2
0358   static NCollection_Vec3<float> Convert_LinearRGB_To_sRGB_approx22(
0359     const NCollection_Vec3<float>& theRGB) noexcept
0360   {
0361     return NCollection_Vec3<float>(Convert_LinearRGB_To_sRGB_approx22(theRGB.r()),
0362                                    Convert_LinearRGB_To_sRGB_approx22(theRGB.g()),
0363                                    Convert_LinearRGB_To_sRGB_approx22(theRGB.b()));
0364   }
0365 
0366   //! Convert sRGB components into linear RGB using approximated uniform gamma coefficient 2.2
0367   static NCollection_Vec3<float> Convert_sRGB_To_LinearRGB_approx22(
0368     const NCollection_Vec3<float>& theRGB) noexcept
0369   {
0370     return NCollection_Vec3<float>(Convert_sRGB_To_LinearRGB_approx22(theRGB.r()),
0371                                    Convert_sRGB_To_LinearRGB_approx22(theRGB.g()),
0372                                    Convert_sRGB_To_LinearRGB_approx22(theRGB.b()));
0373   }
0374 
0375   //! Converts HLS components into sRGB ones.
0376   static void HlsRgb(const double theH,
0377                      const double theL,
0378                      const double theS,
0379                      double&      theR,
0380                      double&      theG,
0381                      double&      theB) noexcept
0382   {
0383     const NCollection_Vec3<float> anRgb =
0384       Convert_HLS_To_sRGB(NCollection_Vec3<float>((float)theH, (float)theL, (float)theS));
0385     theR = anRgb[0];
0386     theG = anRgb[1];
0387     theB = anRgb[2];
0388   }
0389 
0390   //! Converts sRGB components into HLS ones.
0391   static void RgbHls(const double theR,
0392                      const double theG,
0393                      const double theB,
0394                      double&      theH,
0395                      double&      theL,
0396                      double&      theS) noexcept
0397   {
0398     const NCollection_Vec3<float> aHls =
0399       Convert_sRGB_To_HLS(NCollection_Vec3<float>((float)theR, (float)theG, (float)theB));
0400     theH = aHls[0];
0401     theL = aHls[1];
0402     theS = aHls[2];
0403   }
0404 
0405 public:
0406   //! Returns the value used to compare two colors for equality; 0.0001 by default.
0407   Standard_EXPORT static double Epsilon() noexcept;
0408 
0409   //! Set the value used to compare two colors for equality.
0410   Standard_EXPORT static void SetEpsilon(const double theEpsilon) noexcept;
0411 
0412   //! Dumps the content of me into the stream
0413   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, int theDepth = -1) const;
0414 
0415   //! Inits the content of me from the stream
0416   Standard_EXPORT bool InitFromJson(const Standard_SStream& theSStream, int& theStreamPos);
0417 
0418 private:
0419   //! Returns the values of a predefined color according to the mode.
0420   Standard_EXPORT static NCollection_Vec3<float> valuesOf(const Quantity_NameOfColor theName,
0421                                                           const Quantity_TypeOfColor theType);
0422 
0423 private:
0424   NCollection_Vec3<float> myRgb;
0425 };
0426 
0427 namespace std
0428 {
0429 template <>
0430 struct hash<Quantity_Color>
0431 {
0432   std::size_t operator()(const Quantity_Color& theColor) const noexcept
0433   {
0434     unsigned char aByteArr[3] = {static_cast<unsigned char>(255 * theColor.Red()),
0435                                  static_cast<unsigned char>(255 * theColor.Green()),
0436                                  static_cast<unsigned char>(255 * theColor.Blue())};
0437     return opencascade::hashBytes(aByteArr, sizeof(aByteArr));
0438   }
0439 };
0440 } // namespace std
0441 
0442 #endif // _Quantity_Color_HeaderFile