| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | #ifndef APP_COLOR_H |
| | #define APP_COLOR_H |
| |
|
| | #ifdef __GNUC__ |
| | # include <cstdint> |
| | #endif |
| | #include <cmath> |
| | #include <string> |
| |
|
| | #include <FCGlobal.h> |
| |
|
| | |
| | namespace Base |
| | { |
| |
|
| | template<class color_type> |
| | struct color_traits |
| | { |
| | color_traits() = default; |
| | explicit color_traits(const color_type& ct) |
| | : ct(ct) |
| | {} |
| | float redF() const |
| | { |
| | return static_cast<float>(ct.redF()); |
| | } |
| | float greenF() const |
| | { |
| | return static_cast<float>(ct.greenF()); |
| | } |
| | float blueF() const |
| | { |
| | return static_cast<float>(ct.blueF()); |
| | } |
| | float alphaF() const |
| | { |
| | return static_cast<float>(ct.alphaF()); |
| | } |
| | int red() const |
| | { |
| | return ct.red(); |
| | } |
| | int green() const |
| | { |
| | return ct.green(); |
| | } |
| | int blue() const |
| | { |
| | return ct.blue(); |
| | } |
| | int alpha() const |
| | { |
| | return ct.alpha(); |
| | } |
| | static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| | { |
| | return color_type {red, green, blue, alpha}; |
| | } |
| |
|
| | private: |
| | color_type ct; |
| | }; |
| |
|
| | |
| | |
| | class BaseExport Color |
| | { |
| | public: |
| | |
| | |
| | |
| | |
| | constexpr explicit Color(float R = 0.0, float G = 0.0, float B = 0.0, float A = 1.0) |
| | : r(R) |
| | , g(G) |
| | , b(B) |
| | , a(A) |
| | {} |
| |
|
| | |
| | |
| | |
| | |
| | explicit Color(uint32_t rgba); |
| |
|
| | |
| | Color(const Color& c) = default; |
| | Color(Color&&) = default; |
| |
|
| | |
| | bool operator==(const Color& c) const; |
| | bool operator!=(const Color& c) const; |
| | |
| | |
| | |
| | |
| | void set(float R, float G, float B, float A = 1.0); |
| | float transparency() const; |
| | void setTransparency(float value); |
| | Color& operator=(const Color& c) = default; |
| | Color& operator=(Color&& c) = default; |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | Color& setPackedValue(uint32_t rgba); |
| | |
| | |
| | |
| | |
| | |
| | uint32_t getPackedValue() const; |
| | |
| | |
| | |
| | uint32_t getPackedRGB() const; |
| | |
| | |
| | |
| | void setPackedRGB(uint32_t); |
| | |
| | |
| | |
| | uint32_t getPackedARGB() const; |
| | |
| | |
| | |
| | void setPackedARGB(uint32_t); |
| |
|
| | template<typename T> |
| | static uint32_t asPackedRGBA(const T& color) |
| | { |
| | color_traits<T> ct {color}; |
| | return (ct.red() << 24) | (ct.green() << 16) | (ct.blue() << 8) | ct.alpha(); |
| | } |
| |
|
| | template<typename T> |
| | static T fromPackedRGBA(uint32_t color) |
| | { |
| | return color_traits<T>::makeColor( |
| | (color >> 24) & 0xff, |
| | (color >> 16) & 0xff, |
| | (color >> 8) & 0xff, |
| | (color & 0xff) |
| | ); |
| | } |
| |
|
| | template<typename T> |
| | static uint32_t asPackedRGB(const T& color) |
| | { |
| | color_traits<T> ct {color}; |
| | return (ct.red() << 24) | (ct.green() << 16) | (ct.blue() << 8); |
| | } |
| |
|
| | template<typename T> |
| | static T fromPackedRGB(uint32_t color) |
| | { |
| | return color_traits<T>::makeColor((color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff); |
| | } |
| | |
| | |
| | |
| | template<typename T> |
| | void setValue(const T& q) |
| | { |
| | color_traits<T> ct {q}; |
| | set(ct.redF(), ct.greenF(), ct.blueF(), ct.alphaF()); |
| | } |
| | |
| | |
| | |
| | |
| | template<typename T> |
| | inline T asValue() const |
| | { |
| | |
| | return color_traits<T>::makeColor(int(std::lround(r * 255.0F)), |
| | int(std::lround(g * 255.0F)), |
| | int(std::lround(b * 255.0F)), |
| | int(std::lround(a * 255.0F))); |
| | |
| | } |
| | |
| | |
| | |
| | template<typename T> |
| | static Color fromValue(const T& q) |
| | { |
| | color_traits<T> ct {q}; |
| | return Color(ct.redF(), ct.greenF(), ct.blueF(), ct.alphaF()); |
| | } |
| | |
| | |
| | |
| | |
| | std::string asHexString() const; |
| |
|
| | |
| | |
| | |
| | |
| | bool fromHexString(const std::string& hex); |
| |
|
| | |
| | float r {}, g {}, b {}, a {}; |
| | }; |
| |
|
| | |
| | template<> |
| | struct color_traits<Base::Color> |
| | { |
| | using color_type = Base::Color; |
| | color_traits() = default; |
| | explicit color_traits(const color_type& ct) |
| | : ct(ct) |
| | {} |
| | float redF() const |
| | { |
| | return ct.r; |
| | } |
| | float greenF() const |
| | { |
| | return ct.g; |
| | } |
| | float blueF() const |
| | { |
| | return ct.b; |
| | } |
| | float alphaF() const |
| | { |
| | return ct.a; |
| | } |
| | void setRedF(float red) |
| | { |
| | ct.r = red; |
| | } |
| | void setGreenF(float green) |
| | { |
| | ct.g = green; |
| | } |
| | void setBlueF(float blue) |
| | { |
| | ct.b = blue; |
| | } |
| | void setAlphaF(float alpha) |
| | { |
| | ct.a = alpha; |
| | } |
| | int red() const |
| | { |
| | return int(std::lround(ct.r * 255.0F)); |
| | } |
| | int green() const |
| | { |
| | return int(std::lround(ct.g * 255.0F)); |
| | } |
| | int blue() const |
| | { |
| | return int(std::lround(ct.b * 255.0F)); |
| | } |
| | int alpha() const |
| | { |
| | return int(std::lround(ct.a * 255.0F)); |
| | } |
| | void setRed(int red) |
| | { |
| | ct.r = static_cast<float>(red) / 255.0F; |
| | } |
| | void setGreen(int green) |
| | { |
| | ct.g = static_cast<float>(green) / 255.0F; |
| | } |
| | void setBlue(int blue) |
| | { |
| | ct.b = static_cast<float>(blue) / 255.0F; |
| | } |
| | void setAlpha(int alpha) |
| | { |
| | ct.a = static_cast<float>(alpha) / 255.0F; |
| | } |
| | static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| | { |
| | return color_type { |
| | static_cast<float>(red) / 255.0F, |
| | static_cast<float>(green) / 255.0F, |
| | static_cast<float>(blue) / 255.0F, |
| | static_cast<float>(alpha) / 255.0F |
| | }; |
| | } |
| |
|
| | private: |
| | color_type ct; |
| | }; |
| |
|
| | } |
| | |
| |
|
| | #endif |
| |
|