Colors 将十六进制颜色转换为RGB,反之亦然

Colors 将十六进制颜色转换为RGB,反之亦然,colors,hex,rgb,Colors,Hex,Rgb,最有效的方法是什么?十六进制值就是用十六进制表示的RGB数字。所以你只需要把每对十六进制数字转换成十进制 例如: #FF6400 = RGB(0xFF, 0x64, 0x00) = RGB(255, 100, 0) 很快: int r = ( hexcolor >> 16 ) & 0xFF; int g = ( hexcolor >> 8 ) & 0xFF; int b = hexcolor & 0xFF; int hexcolor = (

最有效的方法是什么?

十六进制值就是用十六进制表示的RGB数字。所以你只需要把每对十六进制数字转换成十进制

例如:

#FF6400 = RGB(0xFF, 0x64, 0x00) = RGB(255, 100, 0)
很快:

int r = ( hexcolor >> 16 ) & 0xFF;

int g = ( hexcolor >> 8 ) & 0xFF;

int b = hexcolor & 0xFF;

int hexcolor = (r << 16) + (g << 8) + b;
intr=(hexcolor>>16)和0xFF;
int g=(hexcolor>>8)和0xFF;
intb=hexcolor&0xFF;

int hexcolor=(r真实答案:取决于您要查找的十六进制颜色值(例如565、555、888、8888等)、alpha位的数量、实际颜色分布(rgb与bgr…)以及大量其他变量

这是一个使用C++模板(ScummVM直接)的RGB值的通用算法。

模板
uint32 RGB颜色(uint8 r、uint8 g、uint8 b){
返回T::kAlphaMask|
((r>(8-T::kRedBits))&T::kRedMask)|
((g>(8-T::kGreenBits))&T::kGreenMask)|
((b>(8-T::kBlueBits))&T::kBlueMask);
}
下面是565的示例颜色结构(16位颜色的标准格式):

模板
结构色掩模{
枚举{
高位=0xF7DEF7DE,
低位=0x08210821,
qhighBits=0xE79CE79C,
qlowBits=0x18631863,
kBytesPerPixel=2,
kAlphaBits=0,
kRedBits=5,
kGreenBits=6,
kBlueBits=5,
kAlphaShift=kRedBits+kGreenBits+kBlueBits,
kRedShift=kGreenBits+kBlueBits,
kGreenShift=kBlueBits,
kBlueShift=0,
kAlphaMask=((1在python中:

def hex_to_rgb(value):
    """Return (red, green, blue) for the color given as #rrggbb."""
    value = value.lstrip('#')
    lv = len(value)
    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))

def rgb_to_hex(red, green, blue):
    """Return color as #rrggbb for the given color values."""
    return '#%02x%02x%02x' % (red, green, blue)

hex_to_rgb("#ffffff")           #==> (255, 255, 255)
hex_to_rgb("#ffffffffffff")     #==> (65535, 65535, 65535)
rgb_to_hex(255, 255, 255)       #==> '#ffffff'
rgb_to_hex(65535, 65535, 65535) #==> '#ffffffffffff'
#!/usr/bin/env python 进口稀土 导入系统 def十六进制到rgb(值): value=value.lstrip(“#”) lv=len(值) 返回元组(int(值[i:i+lv/3],16)表示范围(0,lv,lv/3)内的i) def rgb_至_十六进制(rgb): rgb=评估(rgb) r=rgb[0] g=rgb[1] b=rgb[2] 返回“#%02X%02X%02X%”(r,g,b) def main(): 颜色=原始输入(“十六进制[#FFFFFF]或RGB[255,255,255]值(无值退出程序):”) 而颜色: 如果重新搜索(“\\\\[a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]”,颜色): 已转换=十六进制为rgb(颜色) 打印转换 elif检索('[0-9]{1,3},[0-9]{1,3},[0-9]{1,3}',颜色): 已转换=rgb_到_十六进制(颜色) 打印转换 elif颜色=='': 系统出口(0) 其他: 打印“您没有输入有效值!” 颜色=原始输入(“十六进制[#FFFFFF]或RGB[255,255,255]值(无值退出程序):”) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': main()
修改Jeremy的python答案以处理短CSS rgb值,如0、#999和#fff(浏览器将呈现为黑色、中灰色和白色):


这是我为自己在c++11中使用而创建的代码片段。 您可以发送十六进制值或字符串:

    void Color::SetColor(string color) {
    // try catch will be necessary if your string is not sanitized before calling this function.
         SetColor(std::stoul(color, nullptr, 16));
    }

    void Color::SetColor(uint32_t number) {
        B = number & 0xFF;
        number>>= 8;
        G = number & 0xFF;
        number>>= 8;
        R = number & 0xFF;
    }



 // ex:
 SetColor("ffffff");
 SetColor(0xFFFFFF);

在python中,十六进制和“rgb”之间的转换也包含在绘图包
matplotlib

import matplotlib.colors as colors
然后

需要注意的是,颜色中的rgb值被假定为介于0.0和1.0之间。如果要介于0和255之间,则需要进行小的转换。特别是

def hex_to_rgb(hex_string):
    rgb = colors.hex2color(hex_string)
    return tuple([int(255*x) for x in rgb])

def rgb_to_hex(rgb_tuple):
    return colors.rgb2hex([1.0*x/255 for x in rgb_tuple])

另一个注意事项是,
colors.hex2color
只接受有效的十六进制颜色字符串。

您只需将十六进制值(部分)转换为十进制,反之亦然。还需要考虑,十六进制值可能包含6或3个字符(不含字符“#”)

在Python 3.5上的实现

"""Utils for working with colors."""

import textwrap


def rgb_to_hex(value1, value2, value3):
    """
    Convert RGB value (as three numbers each ranges from 0 to 255) to hex format.

    >>> rgb_to_hex(235, 244, 66)
    '#EBF442'
    >>> rgb_to_hex(56, 28, 26)
    '#381C1A'
    >>> rgb_to_hex(255, 255, 255)
    '#FFFFFF'
    >>> rgb_to_hex(0, 0, 0)
    '#000000'
    >>> rgb_to_hex(203, 244, 66)
    '#CBF442'
    >>> rgb_to_hex(53, 17, 8)
    '#351108'
    """

    for value in (value1, value2, value3):
        if not 0 <= value <= 255:
            raise ValueError('Value each slider must be ranges from 0 to 255')
    return '#{0:02X}{1:02X}{2:02X}'.format(value1, value2, value3)


def hex_to_rgb(value):
    """
    Convert color`s value in hex format to RGB format.

    >>> hex_to_rgb('fff')
    (255, 255, 255)
    >>> hex_to_rgb('ffffff')
    (255, 255, 255)
    >>> hex_to_rgb('#EBF442')
    (235, 244, 66)
    >>> hex_to_rgb('#000000')
    (0, 0, 0)
    >>> hex_to_rgb('#000')
    (0, 0, 0)
    >>> hex_to_rgb('#54433f')
    (84, 67, 63)
    >>> hex_to_rgb('#f7efed')
    (247, 239, 237)
    >>> hex_to_rgb('#191616')
    (25, 22, 22)
    """

    if value[0] == '#':
        value = value[1:]

    len_value = len(value)

    if len_value not in [3, 6]:
        raise ValueError('Incorect a value hex {}'.format(value))

    if len_value == 3:
        value = ''.join(i * 2 for i in value)
    return tuple(int(i, 16) for i in textwrap.wrap(value, 2))


if __name__ == '__main__':
    import doctest
    doctest.testmod()

非常简单和简短的实现:

color_in_hex = 'FF00EE64' # Green Color
print('RGB =', tuple(int(color_in_hex[i:i+2], 16) for i in (0, 2, 4)))

你可能需要定义语言。这将对选择的算法产生影响。他写了一个等式。RGB到hex的读数是从右到左。hex到RGB的读数是从左到右。小心你的运算符优先级:+比Nice的优先级高。答案已经存在1.5年了,没有人发现。嗯…如果int是16位。红色i“这已经转移到了位桶上。@哈维,完全正确,8位也是一个问题。不确定有多少16位机器有C/Java编译器,但我确定这比我6年多前写这个答案时要多。你能解释一下
rbg\u to\u hex
的格式吗?在函数中,rgb是一个3个整数的元组。那种格式字符串只是一个#后跟三个%02x,它给出了一个0填充的2位十六进制int值。我很久以前就发布了这个,它仍然是我最喜欢的python代码块之一(我已经编写过)。这是一个伟大的发现,你不介意我在我的一些GPL代码中使用它吗?:)对于
rgb_to_hex
,如果不想将一个元组作为参数放入,并且它仍然可以正常工作(如果不放入三个参数,则会导致错误),则始终可以在参数列表中执行
*rbg
import matplotlib.colors as colors
colors.hex2color('#ffffff')        #==> (1.0, 1.0, 1.0)
colors.rgb2hex((1.0, 1.0, 1.0))    #==> '#ffffff'
def hex_to_rgb(hex_string):
    rgb = colors.hex2color(hex_string)
    return tuple([int(255*x) for x in rgb])

def rgb_to_hex(rgb_tuple):
    return colors.rgb2hex([1.0*x/255 for x in rgb_tuple])
"""Utils for working with colors."""

import textwrap


def rgb_to_hex(value1, value2, value3):
    """
    Convert RGB value (as three numbers each ranges from 0 to 255) to hex format.

    >>> rgb_to_hex(235, 244, 66)
    '#EBF442'
    >>> rgb_to_hex(56, 28, 26)
    '#381C1A'
    >>> rgb_to_hex(255, 255, 255)
    '#FFFFFF'
    >>> rgb_to_hex(0, 0, 0)
    '#000000'
    >>> rgb_to_hex(203, 244, 66)
    '#CBF442'
    >>> rgb_to_hex(53, 17, 8)
    '#351108'
    """

    for value in (value1, value2, value3):
        if not 0 <= value <= 255:
            raise ValueError('Value each slider must be ranges from 0 to 255')
    return '#{0:02X}{1:02X}{2:02X}'.format(value1, value2, value3)


def hex_to_rgb(value):
    """
    Convert color`s value in hex format to RGB format.

    >>> hex_to_rgb('fff')
    (255, 255, 255)
    >>> hex_to_rgb('ffffff')
    (255, 255, 255)
    >>> hex_to_rgb('#EBF442')
    (235, 244, 66)
    >>> hex_to_rgb('#000000')
    (0, 0, 0)
    >>> hex_to_rgb('#000')
    (0, 0, 0)
    >>> hex_to_rgb('#54433f')
    (84, 67, 63)
    >>> hex_to_rgb('#f7efed')
    (247, 239, 237)
    >>> hex_to_rgb('#191616')
    (25, 22, 22)
    """

    if value[0] == '#':
        value = value[1:]

    len_value = len(value)

    if len_value not in [3, 6]:
        raise ValueError('Incorect a value hex {}'.format(value))

    if len_value == 3:
        value = ''.join(i * 2 for i in value)
    return tuple(int(i, 16) for i in textwrap.wrap(value, 2))


if __name__ == '__main__':
    import doctest
    doctest.testmod()
const assert = require('assert');

/**
 * Return a color`s value in the hex format by passed the RGB format.
 * @param  {integer} value1 An value in ranges from 0 to 255
 * @param  {integer} value2 An value in ranges from 0 to 255
 * @param  {integer} value3 An value in ranges from 0 to 255
 * @return {string}        A color`s value in the hex format
 */
const RGBtoHex = (value1, value2, value3) => {
    const values = [value1, value2, value3];
    let result = '#';
    for (let i = 0; i < 3; i += 1) {
        // validation input
        if (values[i] < 0 || values[i] > 255) throw new Error('An each value of RGB format must be ranges from 0 to 255');

        // append to result values as hex with at least width 2
        result += (('0' + values[i].toString(16)).slice(-2));
    }
    return result.toUpperCase();
};


/**
 * Convert a value from the hex format to RGB and return as an array
 * @param  {int} value A color`s value in the hex format
 * @return {array}     Array values of the RGB format
 */
const hexToRGB = (value) => {
    let val = value;

    val = (value[0] === '#') ? value.slice(1) : value;

    if ([3, 6].indexOf(val.length) === -1) throw new Error(`Incorect a value of the hex format: ${value}`);

    if (val.length === 3) val = val.split('').map(item => item.repeat(2)).join('');

    return val.match(/.{2}/g).map(item => parseInt(`0x${item}`, 16));
};

assert.deepEqual(hexToRGB('fff'), [255, 255, 255]);
assert.deepEqual(hexToRGB('#fff'), [255, 255, 255]);
assert.deepEqual(hexToRGB('#000000'), [0, 0, 0]);
assert.deepEqual(hexToRGB('000000'), [0, 0, 0]);
assert.deepEqual(hexToRGB('#d7dee8'), [215, 222, 232]);
assert.deepEqual(hexToRGB('#1E2F49'), [30, 47, 73]);
assert.deepEqual(hexToRGB('#030914'), [3, 9, 20]);

assert.equal(RGBtoHex(255, 255, 255), '#FFFFFF');
assert.equal(RGBtoHex(0, 0, 0), '#000000');
assert.equal(RGBtoHex(96, 102, 112), '#606670');
assert.equal(RGBtoHex(199, 204, 214), '#C7CCD6');
assert.equal(RGBtoHex(22, 99, 224), '#1663E0');
assert.equal(RGBtoHex(0, 8, 20), '#000814');


module.exports.RGBtoHex = RGBtoHex;
module.exports.hexToRGB = hexToRGB;
// a type for a struct of RGB color
typedef struct _ColorRGB {
    unsigned short int red;
    unsigned short int green;
    unsigned short int blue;
} colorRGB_t;


/*
    Convert a color`s value from the hex format to the RGB.
    Return -1 if a passed value in the hex format is not correct, otherwise - return 0;
 */
static int
convertColorHexToRGB(const char originValue[], colorRGB_t *colorRGB) {

    // a full value of color in hex format must constains 6 charapters
    char completedValue[6];
    size_t lenOriginValue;
    size_t lenCompletedValue;

    // an intermediary variable for keeping value in the hex format
    char hexSingleValue[3];

    // a temp pointer to char, need only to the strtol()
    char *ptr;

    // a variable for keeping a converted number in the hex to the decimal format
    long int number;

    // validation input
    lenOriginValue = strlen(originValue);
    if (lenOriginValue > 7 || lenOriginValue < 3) return -1;

    // copy value without sign '#', if found as first in the string
    (originValue[0] == '#') ? strcpy(completedValue, originValue + 1) : strcpy(completedValue, originValue);

    lenCompletedValue = strlen(completedValue);

    // if the value has only 3 charapters, dublicate an each after itself
    // but if not full version of the hex name of a color (6 charapters), return -1
    if (lenCompletedValue == 3) {
        completedValue[5] = completedValue[2];
        completedValue[4] = completedValue[2];
        completedValue[3] = completedValue[1];
        completedValue[2] = completedValue[1];
        completedValue[1] = completedValue[0];
    } else if (lenCompletedValue != 6) return -1;

    // convert string, by parts, to decimal values and keep it in a struct

    sprintf(hexSingleValue, "%c%c", completedValue[0], completedValue[1]);
    number = strtol(hexSingleValue, &ptr, 16);
    colorRGB->red = number;

    sprintf(hexSingleValue, "%c%c", completedValue[2], completedValue[3]);
    number = strtol(hexSingleValue, &ptr, 16);
    colorRGB->green = number;

    sprintf(hexSingleValue, "%c%c", completedValue[4], completedValue[5]);
    number = strtol(hexSingleValue, &ptr, 16);
    colorRGB->blue = number;

    return 0;
}


/*
    Convert a color`s value from the RGB format to the hex
 */
static int
convertColorRGBToHex(const colorRGB_t *colorRGB, char value[8]) {
    sprintf(value, "#%02X%02X%02X", colorRGB->red, colorRGB->green, colorRGB->blue);
    return 0;
}


/*
    Forming a string representation data in an instance of the structure colorRGB_t
 */
static int
getRGBasString(const colorRGB_t *colorRGB, char str[18]) {
    sprintf(str, "rgb(%d, %d, %d)", colorRGB->red, colorRGB->green, colorRGB->blue);
    return 0;
}


int main (int argv, char **argc)
{
    char str[18];
    char hex[8];

    colorRGB_t *colorRGB_;
    colorRGB_ = (colorRGB_t *)malloc(sizeof(colorRGB_));

    convertColorHexToRGB("fff", colorRGB_);
    getRGBasString(colorRGB_, str);
    printf("Hex 'fff' to RGB %s\n", str);
    convertColorRGBToHex(colorRGB_, hex);
    printf("RGB %s to hex '%s'\n", str, hex);

    convertColorHexToRGB("000000", colorRGB_);
    getRGBasString(colorRGB_, str);
    printf("Hex '000000' to RGB %s\n", str);
    convertColorRGBToHex(colorRGB_, hex);
    printf("RGB %s to hex '%s'\n", str, hex);

    convertColorHexToRGB("#000000", colorRGB_);
    getRGBasString(colorRGB_, str);
    printf("Hex '#000000' to RGB %s\n", str);
    convertColorRGBToHex(colorRGB_, hex);
    printf("RGB %s to hex '%s'\n", str, hex);

    convertColorHexToRGB("#FFF", colorRGB_);
    getRGBasString(colorRGB_, str);
    printf("Hex '#FFF' to RGB %s\n", str);
    convertColorRGBToHex(colorRGB_, hex);
    printf("RGB %s to hex '%s'\n", str, hex);

    free(colorRGB_);
}
Hex 'fff' to RGB rgb(255, 255, 255)
RGB rgb(255, 255, 255) to hex '#FFFFFF'
Hex '000000' to RGB rgb(0, 0, 0)
RGB rgb(0, 0, 0) to hex '#000000'
Hex '#000000' to RGB rgb(0, 0, 0)
RGB rgb(0, 0, 0) to hex '#000000'
Hex '#FFF' to RGB rgb(255, 255, 255)
RGB rgb(255, 255, 255) to hex '#FFFFFF'
color_in_hex = 'FF00EE64' # Green Color
print('RGB =', tuple(int(color_in_hex[i:i+2], 16) for i in (0, 2, 4)))