| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <iomanip> |
| #include <sstream> |
|
|
| #include "Color.h" |
|
|
| using namespace Base; |
|
|
|
|
| |
| Color::Color(uint32_t rgba) |
| : Color {} |
| { |
| setPackedValue(rgba); |
| } |
|
|
| bool Color::operator==(const Color& color) const |
| { |
| return getPackedValue() == color.getPackedValue(); |
| } |
|
|
| bool Color::operator!=(const Color& color) const |
| { |
| return !operator==(color); |
| } |
|
|
| |
| void Color::set(float red, float green, float blue, float alpha) |
| { |
| r = red; |
| g = green; |
| b = blue; |
| a = alpha; |
| } |
|
|
| float Color::transparency() const |
| { |
| return 1.0F - a; |
| } |
|
|
| void Color::setTransparency(float value) |
| { |
| a = 1.0F - value; |
| } |
|
|
| |
| Color& Color::setPackedValue(uint32_t rgba) |
| { |
| |
| this->set(static_cast<float> (rgba >> 24) / 255.0F, |
| static_cast<float>((rgba >> 16) & 0xff) / 255.0F, |
| static_cast<float>((rgba >> 8) & 0xff) / 255.0F, |
| static_cast<float> (rgba & 0xff) / 255.0F); |
| return *this; |
| |
| } |
|
|
| uint32_t Color::getPackedValue() const |
| { |
| |
| return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 | |
| static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 | |
| static_cast<uint32_t>(std::lround(b * 255.0F)) << 8 | |
| static_cast<uint32_t>(std::lround(a * 255.0F))); |
| |
| } |
|
|
| void Color::setPackedRGB(uint32_t rgb) |
| { |
| |
| this->set(static_cast<float>((rgb >> 24) & 0xff) / 255.0F, |
| static_cast<float>((rgb >> 16) & 0xff) / 255.0F, |
| static_cast<float>((rgb >> 8) & 0xff) / 255.0F); |
| |
| } |
|
|
| uint32_t Color::getPackedRGB() const |
| { |
| |
| return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 | |
| static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 | |
| static_cast<uint32_t>(std::lround(b * 255.0F)) << 8); |
| |
| } |
|
|
| uint32_t Color::getPackedARGB() const |
| { |
| |
| return (static_cast<uint32_t>(std::lround(a * 255.0F)) << 24 | |
| static_cast<uint32_t>(std::lround(r * 255.0F)) << 16 | |
| static_cast<uint32_t>(std::lround(g * 255.0F)) << 8 | |
| static_cast<uint32_t>(std::lround(b * 255.0F))); |
| |
| } |
|
|
| void Color::setPackedARGB(uint32_t argb) |
| { |
| |
| this->set(static_cast<float>((argb >> 16) & 0xff) / 255.0F, |
| static_cast<float>((argb >> 8) & 0xff) / 255.0F, |
| static_cast<float> (argb & 0xff) / 255.0F, |
| static_cast<float> (argb >> 24) / 255.0F); |
| |
| } |
|
|
| std::string Color::asHexString() const |
| { |
| |
| std::stringstream ss; |
| ss << "#" << std::hex << std::uppercase << std::setfill('0') |
| << std::setw(2) << int(std::lround(r * 255.0F)) |
| << std::setw(2) << int(std::lround(g * 255.0F)) |
| << std::setw(2) << int(std::lround(b * 255.0F)); |
| return ss.str(); |
| |
| } |
|
|
| bool Color::fromHexString(const std::string& hex) |
| { |
| if (hex.size() < 7 || hex[0] != '#') { |
| return false; |
| } |
|
|
| |
| if (hex.size() == 7) { |
| std::stringstream ss(hex); |
| unsigned int rgb; |
| char ch {}; |
|
|
| ss >> ch >> std::hex >> rgb; |
| int rc = (rgb >> 16) & 0xff; |
| int gc = (rgb >> 8) & 0xff; |
| int bc = rgb & 0xff; |
|
|
| r = rc / 255.0F; |
| g = gc / 255.0F; |
| b = bc / 255.0F; |
|
|
| return true; |
| } |
| |
| if (hex.size() == 9) { |
| std::stringstream ss(hex); |
| unsigned int rgba; |
| char ch {}; |
|
|
| ss >> ch >> std::hex >> rgba; |
| int rc = (rgba >> 24) & 0xff; |
| int gc = (rgba >> 16) & 0xff; |
| int bc = (rgba >> 8) & 0xff; |
| int ac = rgba & 0xff; |
|
|
| r = rc / 255.0F; |
| g = gc / 255.0F; |
| b = bc / 255.0F; |
| a = ac / 255.0F; |
|
|
| return true; |
| } |
|
|
| return false; |
| } |
| |
|
|