File indexing completed on 2025-01-18 10:10:36
0001
0002
0003
0004
0005
0006
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
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 class RColor {
0034
0035 using RGB_t = std::array<uint8_t, 3>;
0036
0037 private:
0038
0039 std::string fColor;
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
0053 RColor(uint8_t r, uint8_t g, uint8_t b) { SetRGB(r, g, b); }
0054
0055
0056 RColor(uint8_t r, uint8_t g, uint8_t b, float alpha)
0057 {
0058 SetRGBA(r, g, b, alpha);
0059 }
0060
0061
0062 RColor(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); };
0063
0064
0065 RColor(const std::string &color) { SetColor(color); };
0066
0067
0068 RColor(float ordinal) { SetOrdinal(ordinal); };
0069
0070
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
0080 void SetRGB(const RGB_t &rgb) { SetRGB(rgb[0], rgb[1], rgb[2]); }
0081
0082
0083 void SetRGB(uint8_t r, uint8_t g, uint8_t b);
0084
0085
0086 void SetRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha);
0087
0088
0089 void SetAlpha(uint8_t alpha);
0090
0091
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
0103 bool HasAlpha() const { return IsRGBA(); }
0104
0105
0106 std::vector<uint8_t> AsRGBA() const;
0107
0108
0109 uint8_t GetRed() const
0110 {
0111 auto rgba = AsRGBA();
0112 return rgba.size() > 2 ? rgba[0] : 0;
0113 }
0114
0115
0116 uint8_t GetGreen() const
0117 {
0118 auto rgba = AsRGBA();
0119 return rgba.size() > 2 ? rgba[1] : 0;
0120 }
0121
0122
0123 uint8_t GetBlue() const
0124 {
0125 auto rgba = AsRGBA();
0126 return rgba.size() > 2 ? rgba[2] : 0;
0127 }
0128
0129
0130 uint8_t GetAlpha() const
0131 {
0132 auto rgba = AsRGBA();
0133 return rgba.size() > 3 ? rgba[3] : 0xFF;
0134 }
0135
0136
0137 float GetAlphaFloat() const
0138 {
0139 return GetAlpha() / 255.;
0140 }
0141
0142
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
0157 const std::string& AsString() const { return fColor; }
0158
0159
0160 void SetColor(const std::string &col) { fColor = col; }
0161
0162
0163 bool GetHLS(float &hue, float &light, float &satur) const;
0164
0165
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 }
0210 }
0211
0212 #endif