从十六进制颜色转换为用C RGB结构 [英] Convert from HEX color to RGB struct in C
本文介绍了从十六进制颜色转换为用C RGB结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从彩色HEX code转换为RGB纯C使用C库只(不包括C ++,模板等)RGB结构可以是这样的 - > typedef结构{RGB双R;双克;双B: } RGB1;
函数返回RGB1
Convert from color HEX code to RGB in pure c using C library only (without C++,templates, etc.) RGB struct may be like this -> typedef struct RGB{ double r; double g; double b; } RGB1; function should return RGB1
推荐答案
假设你的十六进制值是一个32位的'廉政'类型,我们使用上述RGB结构,那么也许这样做:
Assuming that your hex value is a 32-bit 'int' type, and that we use the RGB struct described above, then maybe do something like:
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 RGB结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文