charlieetienne/palette-generator

A PHP library for generating tailwind v4 color palettes from a hexadecimal color

Fund package maintenance!
CharlieEtienne

Installs: 335

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/charlieetienne/palette-generator

v1.1 2025-07-08 22:23 UTC

This package is auto-updated.

Last update: 2025-11-28 07:55:43 UTC


README

A PHP library for generating Tailwind v4 color palettes from a hexadecimal color.

Installation

composer require charlieetienne/palette-generator

Requirements

This package only requires PHP 8.4.

For Laravel users, this package will be auto-discovered thanks to PaletteGeneratorServiceProvider.

Usage

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)"
// ]

How it works

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:

  1. Use the DeltaE 2000 algorithm to iterate through the Tailwind CSS colors and find the nearest match
  2. 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.

Example

palette-generator

Performance

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.

Contribute

Your PR is welcome!