# Color code converter (HEX, RGB, HSL, CMYK, OKLCH)

> Convert a color from HEX to RGB, HSL, HSV, CMYK, CIE Lab and OKLCH or back, and find its nearest CSS named color and relative luminance.

Phiên bản tương tác: https://www.calcopenly.com/vi/programming/color-code-converter
Chủ đề: Máy tính lập trình và công nghệ

The converter reads any CSS color notation into sRGB channels between 0 and 1, then derives each format from them: HEX and RGB scale the channels to 0–255, HSL and HSV come from the largest and smallest channel, and CMYK sets K = 1 − max(R, G, B). For CIE Lab and OKLCH it first removes the sRGB gamma curve of IEC 61966-2-1, converts to CIE XYZ with the D65 white point, then applies the CIE 15 and Ottosson (2020) formulas.

Designers and front-end developers use it to move a color between CSS, design tools and print specs. The default, #2E86C1, is rgb(46, 134, 193), hsl(204.1, 61.5%, 46.9%) and oklch(59.57% 0.1217 242.57), and its nearest CSS named color is steelblue (#4682B4).

CMYK here is the plain formula with no printer profile, so a print shop's values will differ.

## Dữ liệu đầu vào

- **Màu sắc**: #RRGGBB, #RGB, a CSS name, rgb(), hsl(), hsv(), cmyk(), oklab(), oklch(), or lab() with the D65 white point.

## Kết quả

- HEX — kết quả chính
- RGB
- HSL
- HSV / HSB
- CMYK
- CIE Lab (D65)
- OKLCH
- Nearest CSS named color
- Distance to that name (ΔE OK)
- Relative luminance

## Công thức

$$
\begin{aligned} C_{lin} &= \begin{cases} C/12.92 & C \le 0.04045 \\ \left(\frac{C+0.055}{1.055}\right)^{2.4} & \text{otherwise}\end{cases} \\[4pt] L^* &= 116 f(Y/Y_n) - 16 \end{aligned}
$$

## Ví dụ có lời giải

### Steel blue #2E86C1

- Màu sắc: #2E86C1
- **RGB: rgb(46, 134, 193)**
- **HSL: hsl(204.1, 61.5%, 46.9%)**
- **HSV / HSB: hsv(204.1, 76.2%, 75.7%)**
- **CMYK: cmyk(76.2%, 30.6%, 0.0%, 24.3%)**
- Nguồn đối chiếu: Python 3.8 colorsys.rgb_to_hls / rgb_to_hsv on (46, 134, 193)/255, formatted to 1 dp

### Pure red

- Màu sắc: red
- **HEX: #FF0000**
- **CIE Lab (D65): L* 53.24, a* 80.09, b* 67.20**
- **OKLCH: oklch(62.80% 0.2577 29.23)**
- **Relative luminance: 0.2126**
- Nguồn đối chiếu: Separate Python 3.8 decimal script of the CIE 15 Lab (D65) and Ottosson OKLab formulas; matches Bruce Lindbloom's sRGB/D65 red (53.24, 80.09, 67.20) and CSS Color 4's oklch(62.8% 0.2577 29.23)

### HSL input lands exactly on a named color

- Màu sắc: hsl(120, 100%, 25.1%)
- **HEX: #008000**
- **Nearest CSS named color: green**
- Nguồn đối chiếu: Python 3.8 colorsys.hls_to_rgb(1/3, 0.251, 1.0) × 255 = (0, 128.01, 0) → #008000; CSS Color 4 §6.1 green = #008000

### White has no hue (edge)

- Màu sắc: #fff
- **HEX: #FFFFFF**
- **HSL: hsl(0.0, 0.0%, 100.0%)**
- **CMYK: cmyk(0.0%, 0.0%, 0.0%, 0.0%)**
- **OKLCH: oklch(100.00% 0.0000 none)**
- **Nearest CSS named color: white**
- Nguồn đối chiếu: Definition: max = min = 1 gives zero saturation and chroma; CSS Color 4 treats the hue of an achromatic color as powerless (none)

### Black is all key ink (edge)

- Màu sắc: rgb(0, 0, 0)
- **HEX: #000000**
- **CMYK: cmyk(0.0%, 0.0%, 0.0%, 100.0%)**
- **Relative luminance: 0.0000**
- Nguồn đối chiếu: K = 1 − max(R, G, B) = 1 and C = M = Y = 0 by the CMYK definition

### CMYK input

- Màu sắc: cmyk(0, 50, 100, 0)
- **HEX: #FF8000**
- **RGB: rgb(255, 128, 0)**
- Nguồn đối chiếu: Python 3.8: (1 − 0.5) × 255 = 127.5 → 128 rounding half up; R = 255, B = 0

## Câu hỏi

### How do you convert HEX to RGB?

Split the six hex digits into three pairs and convert each pair from base 16 to decimal: #2E86C1 is 2E = 46, 86 = 134 and C1 = 193, so rgb(46, 134, 193). A three-digit shorthand doubles each digit, so #F80 means #FF8800. To go the other way, write each 0–255 channel as two hex digits, padding with a leading 0 below 16.

### What is the difference between HSL and HSV?

Both use the same hue angle but define the other two values differently. HSL lightness is the average of the largest and smallest channel, so pure colors sit at 50% and white at 100%; HSV value is the largest channel, so pure colors sit at 100%. #2E86C1 is hsl(204.1, 61.5%, 46.9%) but hsv(204.1, 76.2%, 75.7%). CSS has hsl() and hwb() but no hsv().

### Why doesn't my CMYK conversion match what the printer uses?

A plain conversion only rearranges screen RGB: K = 1 − max(R, G, B) and each ink is (1 − channel − K) ÷ (1 − K), with no knowledge of paper, ink or press. Print workflows convert through an ICC profile such as FOGRA39 or GRACoL 2006, which limits total ink coverage and shifts colors, so treat these numbers as a starting point and ask the printer for their profile.

### What is OKLCH and why use it in CSS?

OKLCH is the polar form of Björn Ottosson's 2020 OKLab space: lightness L, chroma C and hue h. Equal steps in L look about equally different in brightness across hues, which HSL does not manage, so it suits palettes and accessible color scales. CSS Color Level 4 defines oklch(), and every major browser has supported it since 2023. Pure red is oklch(62.8% 0.2577 29.23).

### What is relative luminance?

Relative luminance is a color's brightness on a scale from 0 (black) to 1 (white), computed on linear channels after removing the sRGB gamma curve: L = 0.2126 R + 0.7152 G + 0.0722 B. Green weighs most because the eye is most sensitive to it, so pure red scores 0.2126 and #2E86C1 scores 0.2148. WCAG uses it for contrast ratios, (L1 + 0.05) ÷ (L2 + 0.05).

### “Color code converter (HEX, RGB, HSL, CMYK, OKLCH)” chính xác đến mức nào?

Độ chính xác phụ thuộc vào dữ liệu nhập và giả định của phương pháp. Phép tính thập phân dùng 50 chữ số có nghĩa, nhưng ước lượng, phương pháp số và dữ liệu nguồn có thể kém chính xác hơn; làm tròn khi hiển thị không loại bỏ các giới hạn đó. Ví dụ có lời giải đã đối chiếu với nguồn độc lập: 6. Ví dụ, “Steel blue #2E86C1” được kiểm tra bằng Python 3.8 colorsys.rgb_to_hls / rgb_to_hsv on (46, 134, 193)/255, formatted to 1 dp.

### Phương pháp này lấy từ đâu?

W3C CSS Color Module Level 4 — sRGB, HSL, HWB conversions and sample code; IEC 61966-2-1:1999 — Default RGB colour space sRGB; CIE 15:2004 Colorimetry, 3rd ed. — CIELAB (ε = 216/24389, κ = 24389/27); Björn Ottosson (2020), A perceptual color space for image processing (OKLab).

## Nguồn

- [W3C CSS Color Module Level 4 — sRGB, HSL, HWB conversions and sample code](https://www.w3.org/TR/css-color-4/)
- IEC 61966-2-1:1999 — Default RGB colour space sRGB
- CIE 15:2004 Colorimetry, 3rd ed. — CIELAB (ε = 216/24389, κ = 24389/27)
- [Björn Ottosson (2020), A perceptual color space for image processing (OKLab)](https://bottosson.github.io/posts/oklab/)
