C++ 颜色RGB值转换为16进制
#include <sstream>
/**
* Transform RGB value to hex.
*/
std::string rgb2hex(int r, int g, int b, bool with_head = false);
std::string rgb2hex(int r, int g, int b, bool with_head)
{
std::stringstream ss;
if (with_head)
ss << "#";
ss << std::hex << (r << 16 | g << 8 | b);
return ss.str();
}
测试结果如下: