如何仅使用 C 库(不使用 C++ 或模板)在纯 C 中将颜色 HEX 代码转换为 RGB? RGB 结构可能是这样的:
typedef struct RGB {
double r;
double g;
double b;
} RGB1;
我正在寻找的函数应该返回 RGB1。
最佳答案
假设您的十六进制值是 32 位“int”类型,并且我们使用上述 RGB 结构,那么可能会执行如下操作:
struct RGB colorConverter(int hexValue)
{
struct RGB rgbColor;
rgbColor.r = ((hexValue >> 16) & 0xFF) / 255.0; // Extract the RR byte
rgbColor.g = ((hexValue >> 8) & 0xFF) / 255.0; // Extract the GG byte
rgbColor.b = ((hexValue) & 0xFF) / 255.0; // Extract the BB byte
return rgbColor;
}
关于在 C 中从 HEX 颜色转换为 RGB 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3723846/