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

Interactive version: https://www.calcopenly.com/programming/color-code-converter
Subject: Programming and tech calculators

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.

## Inputs

- **Color**: #RRGGBB, #RGB, a CSS name, rgb(), hsl(), hsv(), cmyk(), oklab(), oklch(), or lab() with the D65 white point.

## Results

- HEX — main result
- RGB
- HSL
- HSV / HSB
- CMYK
- CIE Lab (D65)
- OKLCH
- Nearest CSS named color
- Distance to that name (ΔE OK)
- Relative luminance

## Formula

$$
\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}
$$

## Worked examples

### Steel blue #2E86C1

- Color: #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%)**
- Checked against: Python 3.8 colorsys.rgb_to_hls / rgb_to_hsv on (46, 134, 193)/255, formatted to 1 dp

### Pure red

- Color: 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**
- Checked against: 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

- Color: hsl(120, 100%, 25.1%)
- **HEX: #008000**
- **Nearest CSS named color: green**
- Checked against: 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)

- Color: #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**
- Checked against: 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)

- Color: rgb(0, 0, 0)
- **HEX: #000000**
- **CMYK: cmyk(0.0%, 0.0%, 0.0%, 100.0%)**
- **Relative luminance: 0.0000**
- Checked against: K = 1 − max(R, G, B) = 1 and C = M = Y = 0 by the CMYK definition

### CMYK input

- Color: cmyk(0, 50, 100, 0)
- **HEX: #FF8000**
- **RGB: rgb(255, 128, 0)**
- Checked against: Python 3.8: (1 − 0.5) × 255 = 127.5 → 128 rounding half up; R = 255, B = 0

## Questions

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

### How accurate is the color code converter?

Accuracy depends on your inputs and the method's assumptions. Decimal arithmetic uses 50 significant digits, but estimates, numerical methods and source data can be less precise; the displayed rounding does not remove those limits. It is checked against 6 worked examples whose answers come from independent sources; for example, “Steel blue #2E86C1” is checked against Python 3.8 colorsys.rgb_to_hls / rgb_to_hsv on (46, 134, 193)/255, formatted to 1 dp.

### Where does the method come from?

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

## Sources

- [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/)
