简体   繁体   English

将RGB转换为Hex C ++?

[英]Convert RGB to Hex C++?

I am looking for a simple C++ function that takes three integers as r, g, and b and returns the corresponding hex color code as an integer. 我正在寻找一个简单的C ++函数,该函数将三个整数分别作为r,g和b并以整数形式返回相应的十六进制颜色代码。 Thanks in advance! 提前致谢!

int hexcolor(int r, int g, int b)
{
    return (r<<16) | (g<<8) | b;
}

Of course you'll need some output formatting to show it as hex. 当然,您需要一些输出格式以将其显示为十六进制。

unsigned long rgb = (r<<16)|(g<<8)|b; 

given that r,g,b are unsigned 8bit chars. 鉴于r,g,b是无符号的8位字符。
(That´s really easy, and Google would´ve helped.) (这确实很容易,而Google会提供帮助。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM