C语言 在 C 中从 HEX 颜色转换为 RGB 结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3723846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:27:15  来源:igfitidea点击:

Convert from HEX color to RGB struct in C

ccolorshexrgb

提问by Shavkat

How do you convert from color HEX code to RGB in pure C using C library only (without C++ or templates)? The RGB struct may be like this:

如何仅使用 C 库(不使用 C++ 或模板)在纯 C 中将颜色 HEX 代码转换为 RGB?RGB 结构可能是这样的:

typedef struct RGB {
    double r;
    double g;
     double b;
} RGB1;

The function?I'm looking for should return RGB1.

函数?我正在寻找应该返回RGB1。

回答by cubic1271

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:

假设您的十六进制值是 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; 
}

回答by Matthew

An RGB value can be stored as in integer via 0xRRGGBB. Examples:

RGB 值可以通过 0xRRGGBB 以整数形式存储。例子:

  • Red: 0xff0000
  • Green: 0x00ff00
  • Blue: 0x0000ff
  • 红色:0xff0000
  • 绿色:0x00ff00
  • 蓝色:0x0000ff

00 is hex for decimal 0, while ff is 255. 0 corresponds to 0.0 and 255 to 1.0. (Actually you didn't specify what the range is. I'm assuming 0.0 to 1.0.)

00 是十进制 0 的十六进制,而 ff 是 255。0 对应于 0.0,255 对应于 1.0。(实际上您没有指定范围是什么。我假设是 0.0 到 1.0。)

So with the above assumptions, you need to extract each component and divide by 255. Since it sounds a lot like a homework question, I'll just show you how you can do the red component.

因此,根据上述假设,您需要提取每个组件并除以 255。由于这听起来很像家庭作业,我将向您展示如何做红色组件。

int hex = 0x123456;
c.r = ((hex >> 16) & 0xff) / 255.0;

Each hex digit takes up 4 bits. So shift to the right by 16 bits (to move everything 4 digits to the right) to make 0xRRGGBBbecome 0xRR. Now you have the red component. (Just in case there is some data higher up in the integer, you can get rid of it by masking the data via & 0xff.)

每个十六进制数字占用 4 位。因此,向右移动 16 位(将所有内容向右移动 4 位)使0xRRGGBB成为0xRR. 现在你有了红色组件。(以防万一在整数中有一些更高的数据,您可以通过屏蔽数据来摆脱它& 0xff。)

If you are dealing with a string "#FFFFFF", then you'd first have to convert it to an integer for the above to work.

如果您正在处理 string "#FFFFFF",那么您首先必须将其转换为整数才能使上述工作正常进行。

回答by Tor Klingberg

If the hex code is a string, you can parse it like this

如果十六进制代码是一个字符串,你可以这样解析

char *str = "0000FF";
int r, g, b;
sscanf(str, "%02x%02x%02x", &r, &g, &b);

That is to ints, not doubles. Also do check that sscanfreturns 3, the number of items read.

那是整数,而不是双精度。还要检查是否sscanf返回 3,即读取的项目数。

回答by Andrey Pirozhenko

I guess RGB could be stored as 0xRRGGBB on some systems, but in Windows it is actually stored as 0xBBGGRR (see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183449). As the article mentions, there are macros GetRValue, GetGValue, and GetBValue already available.

我猜 RGB 在某些系统上可以存储为 0xRRGGBB,但在 Windows 中它实际上存储为 0xBBGGRR(请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/dd183449)。正如文章所述,已有宏 GetRValue、GetGValue 和 GetBValue 可用。