# RGBA顏色轉Hex顏色在線轉換工具

RGBA顏色

Hex顏色#

## RGBA顏色系統:

RGBA顏色系統中"R"代表紅色通道("Red"), "G"代表綠色通道("Green"), "B"代表藍色通道("Blue"),A代表透明度(Opacity).例如`rgba(16,110,190,0.7)` .

## Hex顏色系統:

HTML的顏色系統可以用16進制的數字表示,從`#000000` (純黑色) to `#FFFFFF` (純白色).例如, `#123456`代表紅色通道是"12" (在"00"到"FF"之間).綠色通道是"34",藍色通道是"56". Hex顏色系統也支持簡化顯示,例如`#e1a``#ee11aa`是等價的.

## 如何進行RGBA顏色轉Hex顏色轉換?

1. 步驟1: 分別獲得RGBA顏色的紅色通道值,綠色通道值,藍色通道10進制值和透明度值.

2. 步驟2: 把顏色通道值從10進制轉換為16進制.

3. 步驟3: 按Hex顏色系統語法把3種顏色通道值組合起來.

 Step 1: RGBA Color "rgba(16,110,190,0.66)": Red(16), Green(110), Blue(190), Opacity(0.66) Step 2: Red: (16)->(0x10), Green: (110)->(0x6e), Blue: (190)->(0xbe), Opacity: (0.66)->(66%) Step 3: RGB Color "rgba(16,110,190,0.66)" -> "#106ebe" with opacity 66%

## 用Python進行RGBA顏色到Hex顏色轉換:

```import re

def rgb_to_hex(rgb_color):
rgb_color = re.search('\(.*\)', rgb_color).group(0).replace(' ', '').lstrip('(').rstrip(')')
[r, g, b] = [int(x) for x in rgb_color.split(',')]
# check if in range 0~255
assert 0 <= r <= 255
assert 0 <= g <= 255
assert 0 <= b <= 255

r = hex(r).lstrip('0x')
g = hex(g).lstrip('0x')
b = hex(b).lstrip('0x')
# re-write '7' to '07'
r = (2 - len(r)) * '0' + r
g = (2 - len(g)) * '0' + g
b = (2 - len(b)) * '0' + b

hex_color = '#' + r + g + b
return hex_color

rgb_input = 'rgb(7,110,190)'
hex_output = rgb_to_hex(rgb_input)
print('Hex color result is:{0}'.format(hex_output))

-------------------
Hex color result is:#076ebe```