# Hex颜色到RGB颜色在线转换工具

Hex颜色#

RGB颜色

## Hex颜色系统:

HTML的颜色系统可以用16进制的数字表示,从 `#000000` (纯黑色) to `#FFFFFF` (纯白色).例如,`#123456`代表红色通道是 "12" (在"00" 到 "FF" 之间). 绿色通道是 "34", 蓝色通道是 "56". Hex颜色系统也支持简化显示,例如 `#e1a``#ee11aa` 是等价的.

## RGB颜色系统:

RGB颜色系统中 "R" 代表红色通道("Red"), "G" 代表绿色通道 ("Green"), "B" 代表蓝色通道("Blue").例如rgb(16,110,190).

## 如何进行Hex颜色到RGB颜色转换?

1. 步骤1: 分别获得Hex颜色的红色通道值,绿色通道值,蓝色通道值.

2. 步骤2: 把颜色通道值从16进制转换为10进制.

3. 步骤3: 按RGB颜色系统语法把3种颜色通道值组合起来.

 Step 1: Hex Color "#106ebe": Red(0x10), Green(0x6e), Blue(0xbe) Step 2: Red: (0x10)->(16), Green: (0x6e)->(110), Blue: (0xbe)->(190) Step 3: Hex Color "#106ebe" -> rgb(16,110,190)

 Step 1: Hex Color "#ea3" -> "#eeaa33" Step 2: Hex Color "#eeaa33": Red(0xee), Green(0xaa), Blue(0x33) Step 3: Red: (0xee)->(238), Green: (0xaa)->(170), Blue: (0x33)->(51) Step 4: Hex Color "#ea3" -> rgb(238,170,51)

## 用Python进行Hex颜色到RGB颜色转换:

```def hex_to_rgb(hex_color):
hex_color = hex_color.replace(" ", "").replace("#", "")
# 3-digits hex color
if len(hex_color) == 3:
r = hex_color[0] * 2
g = hex_color[1] * 2
b = hex_color[2] * 2
# 6-digits hex color
elif len(hex_color) == 6:
r = hex_color[0:2]
g = hex_color[2:4]
b = hex_color[4:6]
else:
return "length error"
# convert hex to decimal
r = int(r, 16)
g = int(g, 16)
b = int(b, 16)
# check if in range 0~255
assert 0 <= r <= 255
assert 0 <= g <= 255
assert 0 <= b <= 255
# write rgb in correct syntax
rgb_color = "rgb({0},{1},{2})".format(r, g, b)
return rgb_color

hex_input = '#106ebe'
RGB_output = hex_to_rgb(hex_input)
print('RGB result is:{0}'.format(RGB_output))

-------------------
RGB result is:rgb(16,110,190)```