#pragma once #include #include "common.hpp" #include "utils/string.hpp" LEMONBUDDY_NS namespace color_util { template struct color { using type = color; union { struct { ChannelType red; ChannelType green; ChannelType blue; ChannelType alpha; } bits; ValueType value = 0U; } colorspace; explicit color(ValueType v) { colorspace.value = v; } }; template auto make_24bit(T&& value) { return color(forward(value)); } template auto make_32bit(T&& value) { return color(forward(value)); } template uint8_t alpha(const color c) { return ((c.colorspace.value >> 24) << 8) | ((c.colorspace.value >> 24)); } template T red(const color c) { uint8_t r = c.colorspace.value >> 16; if (std::is_same::value) return r << 8 / 0xFF; if (std::is_same::value) return r << 8 | r << 8 / 0xFF; } template T green(const color c) { uint8_t g = c.colorspace.value >> 8; if (std::is_same::value) return g << 8 / 0xFF; if (std::is_same::value) return g << 8 | g << 8 / 0xFF; } template T blue(const color c) { uint8_t b = c.colorspace.value; if (std::is_same::value) return b << 8 / 0xFF; if (std::is_same::value) return b << 8 | b << 8 / 0xFF; } string hex(const color value) { // clang-format off return string_util::from_stream(stringstream() << "#" << std::setw(6) << std::setfill('0') << std::hex << std::uppercase << (value.colorspace.value & 0x00FFFFFF)); // clang-format on } string hex(const color value) { // clang-format off return string_util::from_stream(stringstream() << "#" << std::setw(8) << std::setfill('0') << std::hex << std::uppercase << value.colorspace.value); // clang-format on } } LEMONBUDDY_NS_END