颜色值如何使用 C++ 输出字符串格式?

这段代码展示了如何在C++中将颜色值转换为字符串格式,并提供了输出RGB和ARGB组件的函数。`Color32`联合体用于存储颜色值,`toString()`方法生成包含RGB/ARGB十六进制表示的字符串。此外,还定义了两个友元运算符用于输出RGBA和ARGB结构体,以及一个用于输出`Color32`整型颜色值的友元运算符。
摘要由CSDN通过智能技术生成

颜色值如何使用 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)
			
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值