跳至主要内容
Redmoon Converters
🎨 设计与创意

颜色换算器——HEX、RGB、HSL、OKLCH与对比度

在HEX、RGB、HSL、HWB、OKLCH和LCH之间换算任意颜色。内置WCAG 2.2对比度检测工具,可针对任意背景色或替代前景色进行检查。

支持 hex、rgb、hsl、hwb、oklch、lch 以及 CSS 颜色名称。
格式数值

WCAG 对比度检测

对比度
正文文本 16 px
大号文本 24 px
计算原理

先用标准伽马曲线将 sRGB 转换为线性RGB,再通过 Björn Ottosson 的矩阵转换为 OKLab,然后用极坐标转换为 OKLCH。HSL/HWB 则采用 CSS Color Module 4 标准公式。

WCAG 2.2 对比度计算:比值 =(L1 + 0.05)/(L2 + 0.05),其中相对亮度由 sRGB→线性RGB 的加权和得出(0.2126 R + 0.7152 G + 0.0722 B)。普通文本 AA 需达到4.5,AAA 需达到7;大号文本(18pt / 14pt粗体)分别为3和4.5。

计算原理

Type any colour and it is parsed to sRGB, then re-expressed six ways: HEX, RGB, HSL, HWB, OKLCH and LCH. Conversions follow the standard path. sRGB is linearised with the usual gamma curve, dividing values at or below 0.04045 by 12.92 and otherwise raising (v + 0.055) ÷ 1.055 to the power 2.4. From there it goes through Björn Ottosson's OKLab matrices for OKLCH, or through XYZ and CIE Lab with a D65 white point for LCH.

The input field accepts more than the label suggests: hex in 3, 4, 6 or 8 digits, rgb() and rgba(), hsl() and hsla(), hwb(), oklch(), lch(), and seventeen basic CSS colour names such as teal, olive and magenta. Separate Foreground and Background fields drive the WCAG contrast panel, which defaults to #FFFFFF on #F42C2C. That panel reports the ratio and pass/fail chips for AA normal (4.5), AA large (3), AAA normal (7) and AAA large (4.5).

Alpha is parsed but then discarded, so an 8-digit hex or an rgba() with transparency converts as though fully opaque, and contrast is always computed on opaque colours. Out-of-gamut OKLCH and LCH inputs are clipped channel by channel back into sRGB rather than properly gamut-mapped, which can shift hue on very saturated values. The contrast maths is WCAG 2.x relative luminance, not the newer APCA model, and it grades colour pairs rather than real rendered text.

常见问题

What is OKLCH and why use it instead of HSL?

OKLCH is a perceptually uniform polar space, meaning equal changes in its lightness value look equally different to the eye, which HSL does not guarantee. That makes it far better for generating tints and shades of a brand colour without lightness drifting between hues.

Does white text pass contrast on a red background?

With the defaults of #FFFFFF on #F42C2C the ratio comes out around 4.00:1. That clears AA large text at 3:1 but falls short of AA normal at 4.5:1 and both AAA thresholds, so reserve it for headings at 24 px or larger rather than body copy.

Can I paste a colour that has transparency?

You can paste 8-digit hex or rgba() and it will parse, but the alpha channel is dropped. Every output format and the contrast ratio treat the colour as fully opaque, so for a semi-transparent overlay you need to work out the composited colour first.

Which named colours does the input recognise?

Seventeen of them: black, white, red, lime, blue, yellow, cyan, magenta, silver, gray, olive, green, purple, teal, navy, maroon and orange. Anything outside that list, such as rebeccapurple or coral, is rejected as an invalid colour, so paste the hex value instead.

What is the difference between the LCH and OKLCH rows?

Both are polar lightness, chroma and hue forms, but LCH is built on CIE Lab with a D65 white point while OKLCH uses OKLab. OKLab was fitted specifically to fix Lab's hue shifts in blues and purples, so OKLCH hue stays truer as you change lightness.

别名

color converter oklchhex to hslwcag contrast checkerrgb to hex

相关工具