OKLab

The perceptual space behind OKLCH, in Cartesian form. PHPColor mixes here by default.

Channels
  • L Lightness 0 to 1
  • a Green to red -0.4 to 0.4
  • b Blue to yellow -0.4 to 0.4
CSS syntax
oklab()
White point
D65
Category
Perceptual
PHP class
OklabColor
Reach for it when
Mixing, interpolation, gradient construction.

Convert to OKLab in PHP

Any color converts with to(); the result is OKLab, an object with its own channels.

<?php

$color = Color::parse('#3b82f6')->to('oklab');

$color->getChannels();
// ['l' => 0.6231, 'a' => -0.0332, 'b' => -0.1851]
$color = Color::parse('#3b82f6')->to('oklab'); $color->getChannels(); // ['l' => 0.6231, 'a' => -0.0332, 'b' => -0.1851]

Parse OKLab in PHP

The regular CSS syntax goes straight into Color::parse().

<?php

$color = Color::parse('oklab(0.62 -0.02 -0.18)');

$color->toHex(); // #4b7ff2
$color = Color::parse('oklab(0.62 -0.02 -0.18)'); $color->toHex(); // #4b7ff2

Export OKLab in CSS

Every color object serializes back to the CSS form of its space.

<?php

Color::parse('#3b82f6')->to('oklab')->toCss();
// oklab(0.623083 -0.0332476 -0.185052)
Color::parse('#3b82f6')->to('oklab')->toCss(); // oklab(0.623083 -0.0332476 -0.185052)

Validate OKLab

tryFrom() returns null on invalid input instead of throwing.

<?php

Color::tryFrom('oklab(0.62 -0.02 -0.18)'); // instance

Color::tryFrom('oklab(nope)'); // null
Color::tryFrom('oklab(0.62 -0.02 -0.18)'); // instance Color::tryFrom('oklab(nope)'); // null