charlieetienne/palette-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

charlieetienne/palette-generator

最新稳定版本:v1.1

Composer 安装命令:

composer require charlieetienne/palette-generator

包简介

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

README 文档

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!

统计信息

  • 总下载量: 335
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-07