Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:36

0001 /*************************************************************************
0002  * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers.               *
0003  * All rights reserved.                                                  *
0004  *                                                                       *
0005  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0006  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0007  *************************************************************************/
0008 
0009 #ifndef ROOT7_RColor
0010 #define ROOT7_RColor
0011 
0012 #include <cstdint>
0013 #include <vector>
0014 #include <string>
0015 #include <array>
0016 #include <DllImport.h>
0017 
0018 namespace ROOT {
0019 namespace Experimental {
0020 
0021 // TODO: see also imagemagick's C++ interface for RColor operations!
0022 // https://www.imagemagick.org/api/magick++-classes.php
0023 
0024 /** \class RColor
0025 \ingroup GpadROOT7
0026 \brief The color class
0027 \author Axel Naumann <axel@cern.ch>
0028 \author Sergey Linev <S.Linev@gsi.de>
0029 \date 2017-09-26
0030 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
0031 */
0032 
0033 class RColor {
0034 
0035    using RGB_t = std::array<uint8_t, 3>;
0036 
0037 private:
0038 
0039    std::string fColor; ///< string representation of color
0040 
0041    static std::string toHex(uint8_t v);
0042 
0043    static std::vector<uint8_t> ConvertNameToRGB(const std::string &name);
0044 
0045    bool SetRGBHex(const std::string &hex);
0046    bool SetAlphaHex(const std::string &hex);
0047 
0048 public:
0049 
0050    RColor() = default;
0051 
0052    /** Construct color with provided r,g,b values */
0053    RColor(uint8_t r, uint8_t g, uint8_t b) { SetRGB(r, g, b); }
0054 
0055    /** Construct color with provided r,g,b and alpha values */
0056    RColor(uint8_t r, uint8_t g, uint8_t b, float alpha)
0057    {
0058       SetRGBA(r, g, b, alpha);
0059    }
0060 
0061    /** Construct color with provided RGB_t value */
0062    RColor(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); };
0063 
0064    /** Construct color with provided string  */
0065    RColor(const std::string &color) { SetColor(color); };
0066 
0067    /** Construct color with provided ordinal value  */
0068    RColor(float ordinal) { SetOrdinal(ordinal); };
0069 
0070    /** Returns true if color is empty */
0071    bool IsEmpty() const { return fColor.empty(); }
0072 
0073    bool IsRGB() const;
0074    bool IsRGBA() const;
0075    bool IsName() const;
0076    bool IsAuto() const;
0077    bool IsOrdinal() const;
0078 
0079    /** Set r/g/b components of color */
0080    void SetRGB(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); }
0081 
0082    /** Set r/g/b components of color */
0083    void SetRGB(uint8_t r, uint8_t g, uint8_t b);
0084 
0085    /** Set r/g/b/a components of color, a is integer between 0..255 */
0086    void SetRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha);
0087 
0088    /** Set alpha as value from range 0..255 */
0089    void SetAlpha(uint8_t alpha);
0090 
0091    /** Set alpha as float value from range 0..1 */
0092    void SetAlphaFloat(float alpha)
0093    {
0094       if (alpha <= 0.)
0095          SetAlpha(0);
0096       else if (alpha >= 1.)
0097          SetAlpha(255);
0098       else
0099          SetAlpha((uint8_t)(alpha * 255));
0100    }
0101 
0102    /** Returns true if color alpha (opacity) was specified */
0103    bool HasAlpha() const { return IsRGBA(); }
0104 
0105    /** Returns color as RGBA array, trying also convert color name into RGBA value */
0106    std::vector<uint8_t> AsRGBA() const;
0107 
0108    /** Returns red color component 0..255 */
0109    uint8_t GetRed() const
0110    {
0111       auto rgba = AsRGBA();
0112       return rgba.size() > 2 ? rgba[0] : 0;
0113    }
0114 
0115    /** Returns green color component 0..255 */
0116    uint8_t GetGreen() const
0117    {
0118       auto rgba = AsRGBA();
0119       return rgba.size() > 2 ? rgba[1] : 0;
0120    }
0121 
0122    /** Returns blue color component 0..255 */
0123    uint8_t GetBlue() const
0124    {
0125       auto rgba = AsRGBA();
0126       return rgba.size() > 2 ? rgba[2] : 0;
0127    }
0128 
0129    /** Returns color alpha (opacity) as uint8_t 0..255 */
0130    uint8_t GetAlpha() const
0131    {
0132       auto rgba = AsRGBA();
0133       return rgba.size() > 3 ? rgba[3] : 0xFF;
0134    }
0135 
0136    /** Returns color alpha (opacity) as float from 0..1 */
0137    float GetAlphaFloat() const
0138    {
0139       return GetAlpha() / 255.;
0140    }
0141 
0142    /** Set color as plain SVG name like "white" or "lightblue" */
0143    bool SetName(const std::string &name)
0144    {
0145       fColor = name;
0146       if (!IsName()) {
0147          Clear();
0148          return false;
0149       }
0150       return true;
0151    }
0152 
0153    void SetOrdinal(float val);
0154    float GetOrdinal() const;
0155 
0156    /** Returns color as it stored as string */
0157    const std::string& AsString() const { return fColor; }
0158 
0159    /** Set color as string */
0160    void SetColor(const std::string &col) { fColor = col; }
0161 
0162    /** Return the Hue, Light, Saturation (HLS) definition of this RColor */
0163    bool GetHLS(float &hue, float &light, float &satur) const;
0164 
0165    /** Set the Red Green and Blue (RGB) values from the Hue, Light, Saturation (HLS). */
0166    void SetHLS(float hue, float light, float satur);
0167 
0168    std::string AsHex(bool with_alpha = false) const;
0169    std::string AsSVG() const;
0170 
0171    void Clear()
0172    {
0173       fColor.clear();
0174    }
0175 
0176    static const RColor &AutoColor();
0177 
0178    R__DLLEXPORT static constexpr RGB_t kBlack{{0, 0, 0}};
0179    R__DLLEXPORT static constexpr RGB_t kGreen{{0, 0x80, 0}};
0180    R__DLLEXPORT static constexpr RGB_t kLime{{0, 0xFF, 0}};
0181    R__DLLEXPORT static constexpr RGB_t kAqua{{0, 0xFF, 0xFF}};
0182    R__DLLEXPORT static constexpr RGB_t kPurple{{0x80, 0, 0x80}};
0183    R__DLLEXPORT static constexpr RGB_t kGrey{{0x80, 0x80, 0x80}};
0184    R__DLLEXPORT static constexpr RGB_t kFuchsia{{0xFF, 0, 0xFF}};
0185    R__DLLEXPORT static constexpr RGB_t kNavy{{0, 0, 0x80}};
0186    R__DLLEXPORT static constexpr RGB_t kBlue{{0, 0, 0xff}};
0187    R__DLLEXPORT static constexpr RGB_t kTeal{{0, 0x80, 0x80}};
0188    R__DLLEXPORT static constexpr RGB_t kOlive{{0x80, 0x80, 0}};
0189    R__DLLEXPORT static constexpr RGB_t kSilver{{0xc0, 0xc0, 0xc0}};
0190    R__DLLEXPORT static constexpr RGB_t kMaroon{{0x80, 0, 0}};
0191    R__DLLEXPORT static constexpr RGB_t kRed{{0xff, 0, 0}};
0192    R__DLLEXPORT static constexpr RGB_t kYellow{{0xff, 0xff, 0}};
0193    R__DLLEXPORT static constexpr RGB_t kWhite{{0xff, 0xff, 0xff}};
0194    R__DLLEXPORT static constexpr float kTransparent{0.};
0195    R__DLLEXPORT static constexpr float kSemiTransparent{0.5};
0196    R__DLLEXPORT static constexpr float kOpaque{1.};
0197 
0198    friend bool operator==(const RColor &lhs, const RColor &rhs)
0199    {
0200       if (lhs.fColor == rhs.fColor) return true;
0201 
0202       auto l = lhs.AsRGBA();
0203       auto r = rhs.AsRGBA();
0204 
0205       return !l.empty() && (l == r);
0206    }
0207 };
0208 
0209 } // namespace Experimental
0210 } // namespace ROOT
0211 
0212 #endif