如何在 C++ 中将 RGB 颜色值转换为十六进制值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14375156/
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-08-27 18:17:22  来源:igfitidea点击:

how to convert a RGB color value to an hexadecimal value in C++?

c++hexrgb

提问by Tahlil

In my C++ application, I have a png image's color in terms of Red, Green, Blue values. I have stored these values in three integers.

在我的 C++ 应用程序中,我有一个 png 图像的颜色,即红、绿、蓝值。我已将这些值存储在三个整数中。

How to convert RGB values into the equivalent hexadecimal value?

如何将RGB值转换为等效的十六进制值?

Example of that like in this format 0x1906

类似这种格式的示例 0x1906

EDIT: I will save the format as GLuint.

编辑:我将格式保存为 GLuint。

回答by Nikos C.

Store the appropriate bits of each color into an unsigned integer of at least 24 bits (like a long):

将每种颜色的适当位存储为至少 24 位的无符号整数(如 a long):

unsigned long createRGB(int r, int g, int b)
{   
    return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}

Now instead of:

现在代替:

unsigned long rgb = 0xFA09CA;

you can do:

你可以做:

unsigned long rgb = createRGB(0xFA, 0x09, 0xCA);

Note that the above will not deal with the alpha channel. If you need to also encode alpha (RGBA), then you need this instead:

请注意,以上内容不会处理 alpha 通道。如果你还需要编码 alpha (RGBA),那么你需要这个:

unsigned long createRGBA(int r, int g, int b, int a)
{   
    return ((r & 0xff) << 24) + ((g & 0xff) << 16) + ((b & 0xff) << 8)
           + (a & 0xff);
}

Replace unsigned longwith GLuintif that's what you need.

如果您需要,请替换unsigned longGLuint

回答by unwind

If you want to build a string, you can probably use snprintf():

如果你想建立一个字符串,你可能可以使用snprintf()

const unsigned red = 0, green = 0x19, blue = 0x06;
char hexcol[16];

snprintf(hexcol, sizeof hexcol, "%02x%02x%02x", red, green, blue);

This will build the string 001906" inhexcol`, which is how I chose to interpret your example color (which is only four digits when it should be six).

这将构建字符串001906" inhexcol`,这就是我选择解释您的示例颜色的方式(它应该是六位时只有四位数字)。

You seem to be confused over the fact that the GL_ALPHApreprocessor symbol is defined to be 0x1906in OpenGL's header files. This is not a color, it's a format specifierused with OpenGL API calls that deal with pixels, so they know what format to expect.

您似乎对GL_ALPHA预处理器符号被定义0x1906在 OpenGL 的头文件中这一事实感到困惑。这不是颜色,它是与处理像素的 OpenGL API 调用一起使用的格式说明符,因此它们知道期望什么格式。

If you have a PNG image in memory, the GL_ALPHAformat would correspond to onlythe alpha values in the image (if present), the above is something totally different since it builds a string. OpenGL won't need a string, it will need an in-memory buffer holding the data in the format required.

如果内存中有 PNG 图像,则GL_ALPHA格式将对应于图像中的 alpha 值(如果存在),上面的内容完全不同,因为它构建了一个字符串。OpenGL 不需要字符串,它需要一个内存缓冲区,以所需的格式保存数据。

See the glTexImage2D()manual page for a discussion on how this works.

有关其glTexImage2D()工作原理的讨论,请参阅手册页。