从十六进制字符串颜色到RGB颜色
From hexadecimal string color to an RGB color
如何将hexadecimal
字符串颜色#FF0022
值转换为C++
中RGB
值
:
#FF0022
r:255
g:0
b:34
我不知道怎么做,我搜索了谷歌没有运气,请告诉我如何,这样我可以了解更多关于它
解析字符串,然后使用strtol()将每组两个字符转换为小数
代码示例:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
std::vector<std::string> SplitWithCharacters(const std::string& str, int splitLength) {
int NumSubstrings = str.length() / splitLength;
std::vector<std::string> ret;
for (int i = 0; i < NumSubstrings; i++) {
ret.push_back(str.substr(i * splitLength, splitLength));
}
// If there are leftover characters, create a shorter item at the end.
if (str.length() % splitLength != 0) {
ret.push_back(str.substr(splitLength * NumSubstrings));
}
return ret;
}
struct COLOR {
short r;
short g;
short b;
};
COLOR hex2rgb(string hex) {
COLOR color;
if(hex.at(0) == '#') {
hex.erase(0, 1);
}
while(hex.length() != 6) {
hex += "0";
}
std::vector<string> colori=SplitWithCharacters(hex,2);
color.r = stoi(colori[0],nullptr,16);
color.g = stoi(colori[1],nullptr,16);
color.b = stoi(colori[2],nullptr,16);
return color;
}
int main() {
string hexcolor;
cout << "Insert hex color: ";
cin >> hexcolor;
COLOR color = hex2rgb(hexcolor);
cout << "RGB:" << endl;
cout << "R: " << color.r << endl;
cout << "G: " << color.g << endl;
cout << "B: " << color.b << endl;
return 0;
}
相关文章:
- 在圆上找到一个角度的RGB颜色
- 如何从 GIFLIB 保存图像结构中获取 RGB 颜色数据
- Qmage:使用颜色表从灰度转换为RGB
- 从RGB到YUV(YCoCg)的颜色转换
- RGB颜色映射为归一化值
- 将RGB颜色图像转换为OpenCV C 中的索引颜色图像类型
- 24位图图像和16位图图像中的RGB颜色
- RGB颜色图像直方图使用OpenCV C 进行对比度拉伸
- 浮点RGB值与颜色的UByte RGB值
- 将整数转换为位图颜色 RGB 值 C++
- RGB 颜色到 HSL 字节
- 在open cv中,我如何将灰度图像转换回RGB图像(颜色)
- OpenCV创建轨迹栏RGB,并在更改值轨迹栏时更改图像颜色
- 处理 RGB 到 CIEL*a*b* 到 RGB "Out-of-Gamut"颜色转换
- 开发哈希算法:RGB 颜色 ID 和字符串到 int
- 如何在FTGL OpenGL中更改RGB格式的颜色
- 使用 c++ 通过魔术获取 rgb 颜色
- 为什么我的 RGB 转十六进制函数在传递颜色分量时返回 0
- 最快的 RGB 颜色检测C++
- 如何优化 YUV 到 RGB 颜色转换代码