# 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)