CSS Color Conversion Reference: HEX, RGB, HSL, OKLCH, and Accessible Contrast
CSScolor systemsaccessibilitydesign tokens

CSS Color Conversion Reference: HEX, RGB, HSL, OKLCH, and Accessible Contrast

CCloud Dev Toolbox Editorial Team
2026-11-28
6 min read

Convert HEX, RGB, HSL, and OKLCH safely with alpha syntax, gamut guidance, design tokens, and a practical CSS contrast checklist.

Converting CSS colors is easy to do mechanically, but preserving a usable design requires more than swapping HEX for RGB or HSL. This reference explains the main color formats, shows practical conversion patterns, covers transparency and OKLCH gamut issues, and provides a repeatable contrast-checking checklist for design tokens and components.

Overview

HEX, RGB, HSL, and OKLCH describe color in different ways. HEX is compact and common in existing stylesheets. RGB represents red, green, and blue channels directly. HSL separates hue, saturation, and lightness, which can make simple color adjustments easier to reason about. OKLCH is designed to provide a more perceptually consistent way to describe lightness, chroma, and hue.

A conversion does not automatically make two colors visually equivalent in every browser or color space. Before changing a token, identify the purpose of the color: text, background, border, focus indicator, icon, chart series, or decorative detail. Then check contrast, transparency, and whether the target format can represent the source color without clipping.

FormatExampleBest useWatch for
HEX#3366CCCompact tokens and legacy stylesheetsLess intuitive for adjusting lightness; alpha uses eight digits
RGBrgb(51 102 204)Direct channel values and debuggingValues describe a color space, not perceived brightness
HSLhsl(220 60% 50%)Simple hue and lightness variationsEqual lightness values are not necessarily equally perceived
OKLCHoklch(55% 0.18 260)Scalable palettes and perceptual adjustmentsSome colors may fall outside a display or target color space

Conversion examples

For a basic opaque color, #3366CC splits into hexadecimal channel pairs: 33, 66, and CC. Converting each pair from base 16 gives RGB values of 51, 102, and 204, so the equivalent CSS is rgb(51 102 204). The same color can be represented in HSL as approximately hsl(220 60% 50%). Small differences in decimal rounding are normal when moving between tools.

For modern CSS, space-separated channel syntax is clear and supports an optional slash for transparency: rgb(51 102 204 / 0.5) or hsl(220 60% 50% / 50%). The eight-digit HEX equivalent is #3366CC80, where the final pair represents alpha. Use a CSS color converter or a local design-tool workflow to calculate conversions, then verify the rendered result rather than trusting the text alone.

Checklist by scenario

When converting an existing brand color

  1. Record the original value exactly, including whether it has an alpha channel.
  2. Choose the destination format based on the task. Use HEX for compact compatibility, HSL for basic hue adjustments, or OKLCH for systematic palette work.
  3. Keep a source-of-truth token rather than replacing values independently in every component.
  4. Compare the converted value in the same background and component state as the original.
  5. Check text, icons, borders, hover states, disabled states, and focus indicators separately.

When building a light and dark theme

  1. Define semantic roles such as --color-text-primary and --color-surface, not only raw names such as --blue-600.
  2. Test each foreground and background pairing in both themes.
  3. Do not assume that inverting a HEX value creates a usable dark-mode equivalent.
  4. Review translucent overlays against every background they can appear on.
  5. Test focus, error, success, and warning colors where adjacent hues may become difficult to distinguish.

When moving a palette to OKLCH

  1. Choose a lightness range appropriate to the role, then adjust chroma and hue.
  2. Inspect the result in the browser and in the design tool used by the team.
  3. Look for gamut clipping: a highly saturated OKLCH color may not map cleanly to the intended output space.
  4. Provide a fallback when your support requirements include environments that do not accept the chosen function.
  5. Run contrast checks after conversion; matching a lightness number does not guarantee matching contrast.

Reusable design-token template

:root {
  --color-brand: oklch(55% 0.18 260);
  --color-surface: oklch(99% 0.01 260);
  --color-text-primary: oklch(25% 0.03 260);
  --color-focus: oklch(62% 0.19 250);

  --button-background: var(--color-brand);
  --button-text: oklch(99% 0.01 260);
}

.button {
  background: var(--button-background);
  color: var(--button-text);
}

.button:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

Keep the semantic aliases even when the underlying values change. This makes a palette easier to review and reduces the risk of changing a color in one component while leaving a conflicting hard-coded value elsewhere.

What to double-check

Contrast: Check the actual rendered foreground and background combination. For a typical WCAG review, teams commonly evaluate normal text against the applicable 4.5:1 threshold, larger text against 3:1, and meaningful non-text interface elements against a suitable non-text contrast requirement. Confirm the target level for the product rather than treating one ratio as universal.

Transparency: Contrast tools need the composited result. A color such as rgb(0 0 0 / 0.4) can look very different over white, gray, an image, or a tinted surface. Test every realistic background.

Gamut: A color specified in OKLCH may be outside the gamut of a target RGB space or display. A browser or tool may reduce chroma during conversion. If the result looks duller or shifts hue, record the adjusted value instead of assuming the original can be reproduced exactly.

Syntax and fallback: Check commas versus space-separated syntax, percentage versus number channels, alpha placement, and missing-channel behavior. If a project needs broad compatibility, place a tested fallback before the newer declaration and verify the cascade in actual target browsers. The cross-browser HTML testing checklist can help structure that review.

Context: Color alone should not be the only way to communicate an error, status, selection, or focus state. Pair it with text, an icon, a border, a pattern, or another clear visual cue where appropriate.

Common mistakes

  • Changing format and value at the same time: Convert first, compare second, and adjust only after the equivalence is understood.
  • Treating HSL lightness as perceived brightness: Two HSL colors with the same lightness can appear very different because channels contribute differently to perception.
  • Testing only the default state: Hover, focus, disabled, visited, validation, and dark-theme states can introduce new contrast failures.
  • Copying a transparent color without its background: An alpha value has no stable visual meaning until it is composited.
  • Using a contrast ratio as the whole design review: Also check thin strokes, small labels, color-blind interpretation, focus visibility, and the distinction between adjacent surfaces.
  • Leaving conversion values undocumented: Store the original source, converted value, intended role, and any fallback so future edits are traceable.

When to revisit

Revisit this checklist before a seasonal brand refresh, a design-system release, a light or dark theme launch, or a migration to new color functions. It is also worth repeating when browser support requirements change, a design tool changes its export behavior, a component gains a new state, or a palette is moved into a wider-gamut workflow.

For a practical final pass, export the current tokens, list every foreground/background pairing, convert only the values that need conversion, and record fallbacks where required. Render representative components in their real states, run contrast checks on the composited colors, and test the result across the browsers and display conditions that matter to the project. If layout or component behavior changes alongside the palette, combine this review with the CSS container queries guide and the Chrome DevTools troubleshooting workflow as appropriate. Keeping this short checklist with the design tokens makes color conversion a repeatable front-end workflow rather than a one-time guess.

Related Topics

#CSS#color systems#accessibility#design tokens
C

Cloud Dev Toolbox Editorial Team

Technical Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.