# Color Formats & OKLCH

> Understand how CSS defines colors, and why OKLCH is the modern standard for perfect palettes.

Subject: CSS | Lesson 13 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/color-formats-and-oklch
Previous: [Text Styling & Wrapping](https://codewithanimation.in/css/text-styling-and-wrapping.md) | Next: [Custom Properties (CSS Variables)](https://codewithanimation.in/css/custom-properties.md)

## What it is

CSS supports several ways to define colors. Historically, developers used HEX (e.g., `#FF5733`) and RGB (`rgb(255, 87, 51)`). While computers understand these perfectly, they are very difficult for humans to adjust.

Modern CSS gives us OKLCH (`oklch(lightness chroma hue)`). It is a color space designed around human perception, ensuring that colors behave exactly how our eyes expect them to.

## Why it matters

If you try to build a design system using HSL (Hue, Saturation, Lightness), you will hit a major flaw: HSL is mathematically uniform, but not visually uniform.

For example, in HSL, yellow at 50% lightness looks incredibly bright, while blue at 50% lightness looks very dark. If you build buttons using these colors, the white text inside them will have inconsistent contrast.

OKLCH fixes this. If you set Lightness to 60%, every single hue (red, green, blue, yellow) will look equally bright to the human eye, guaranteeing consistent contrast.

## How it works

The OKLCH syntax is simple and doesn't require commas: `oklch(L C H / alpha)`.

**L (Lightness)**: A percentage from `0%` (black) to `100%` (white).

**C (Chroma)**: The intensity or purity of the color, usually ranging from `0` (gray) to around `0.37` (vibrant).

**H (Hue)**: An angle from `0` to `360` degrees on the color wheel (e.g., 0 is red, 140 is green, 250 is blue).

## Common mistake: The HEX code guessing game

If you have a HEX color like `#3B82F6` and you want to make a slightly darker version for a hover state, you have to completely change the letters and numbers. It requires opening a color picker tool.

With OKLCH, you just drop the Lightness value: `oklch(60% 0.15 250)` becomes `oklch(50% 0.15 250)`. It is entirely intuitive.

## Tip: Tailwind v4 is built on OKLCH

If you are using modern CSS frameworks like Tailwind CSS v4, the entire default color palette is generated using OKLCH.

You can also specify transparency directly in the color function using a slash, like `oklch(60% 0.15 250 / 50%)` for 50% opacity.

## Try it: Try it

Use the sliders to adjust Lightness, Chroma, and Hue, and see how perceptually uniform OKLCH is compared to HSL.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/color-formats-and-oklch

## Check yourself

### Question 1

What is the main advantage of the OKLCH color space over older formats like HSL?

- A. It is perceptually uniform
- B. It results in smaller CSS file sizes
- C. It is the only format that supports opacity

Correct answer: A

Perceptual uniformity means that two colors with the same Lightness value in OKLCH will appear equally bright to the human eye, regardless of their Hue. HSL does not have this property.

- Why not B: The string length of OKLCH is similar to HSL. The benefit is visual accuracy, not file size.
- Why not C: All CSS color formats (HEX, RGB, HSL) support alpha/opacity.

### Question 2

In the function oklch(L C H), what does the 'C' stand for?

- A. Chroma
- B. Contrast
- C. Color

Correct answer: A

Chroma measures the color's intensity. A Chroma of 0 means the color has no hue and is entirely grayscale, while a high Chroma makes the color extremely vibrant.

- Why not B: Contrast is determined by comparing the Lightness (L) of two different colors.
- Why not C: Chroma represents the amount or purity of the color, ranging from gray (0) to highly vibrant.

### Question 3

Why are HEX codes (like #FF0000) notoriously difficult for creating design systems?

- A. They are hard for humans to intuitively lighten or darken
- B. They are deprecated in modern CSS
- C. Browsers render them slower than RGB

Correct answer: A

Because HEX codes mix red, green, and blue light using hexadecimal math, shifting a color to a darker shade requires changing all the characters at once in a way humans can't easily calculate in their head.

- Why not B: HEX codes are fully supported and widely used; they just aren't intuitive for math.
- Why not C: Browsers parse and render all color formats at effectively the same speed.

## Remember this

- OKLCH stands for Lightness, Chroma, and Hue.
- OKLCH is perceptually uniform, making it the perfect tool for consistent contrast.
- Chroma (C) controls how grayscale or vibrant the color is.
- You can add transparency using a slash: `oklch(L C H / 50%)`.
