A PHP library for generating Tailwind v4 color palettes from a hexadecimal color.
composer require charlieetienne/palette-generatorThis package only requires PHP 8.4.
For Laravel users, this package will be auto-discovered thanks to PaletteGeneratorServiceProvider.
use CharlieEtienne\PaletteGenerator\PaletteGenerator;
// Generate a Tailwind 4 like palette from a hex color
$palette = PaletteGenerator::generatePalette('#3b82f6');
// Returns this array:
// [
// 50 => "oklch(0.97 0.014 254.604)"
// 100 => "oklch(0.932 0.027 255.585)"
// 200 => "oklch(0.882 0.045 254.128)"
// 300 => "oklch(0.809 0.084 251.813)"
// 400 => "oklch(0.707 0.14 254.624)"
// 500 => "oklch(0.623 0.188 259.815)"
// 600 => "oklch(0.546 0.22 262.881)"
// 700 => "oklch(0.488 0.222 264.376)"
// 800 => "oklch(0.424 0.185 265.638)"
// 900 => "oklch(0.379 0.141 265.522)"
// 950 => "oklch(0.282 0.091 267.935)"
// ]This package was inspired by uihue from Matteo Frana. The author detailed his method in an article called "The Mystery of Tailwind Colors (v4)", but the short version is:
- Use the DeltaE 2000 algorithm to iterate through the Tailwind CSS colors and find the nearest match
- Apply this delta with a smoothing effect as we approach the extreme light and dark hues to preserve the color's distinctive features.
I simply tried to do the same in PHP.
Due to the algorithm itself, it may be more resource-intensive than other solutions.
Here are some benchmarks:
CharlieEtienne\PaletteGenerator::generatePalette('#ad70e5')
- Execution time: ~0.00012 seconds per run
- Benchmark speed: ~8,155 ops/sec
Tests were run on a MB Air M2, inside a Laravel app.
To improve performance, you may want to cache the results.
Your PR is welcome!