c/c++实现rgb转16色

如果已知rgb值,能否根据他们转成16色呢?(PS∶不是转换成十六进制)

unsigned short convert_rgb_to_16_bit(unsigned char red, unsigned char green, unsigned char blue)
{
    return (red << 11) + (green << 5) + blue;
}
#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();

测试结果

img


如有帮助,采纳支持一下,谢谢。