颜色值如何使用 C++ 输出字符串格式?
//https://developer.android.com/reference/android/graphics/Color.html
//https://en.wikipedia.org/wiki/RGBA_color_model#Representation
union Color32
{
unsigned int color;
struct RGBA{ unsigned char r, g, b, a; } rgba;
struct ARGB{ unsigned char a, r, g, b; } argb;//
std::string toString() {
std::string rgba = "#";
rgba += n2hexstr<int>((int)this->argb.r,2);
rgba += n2hexstr<int>((int)this->argb.g,2);
rgba += n2hexstr<int>((int)this->argb.b,2);
rgba += n2hexstr<int>((int)this->argb.a,2);
return rgba;
}
private:
template <typename I> std::string n2hexstr(I w, size_t hex_len = sizeof(I) << 1) {
static const char* digits = "0123456789ABCDEF";
std::string rc(hex_len, '0');
for (size_t i = 0, j = (hex_len - 1) * 4; i < hex_len; ++i, j -= 4)