Skip to content

Instantly share code, notes, and snippets.

@saade
Created October 28, 2022 20:39
Show Gist options
  • Save saade/dd8c4764f643310af532c8d0a48fd4c3 to your computer and use it in GitHub Desktop.
Save saade/dd8c4764f643310af532c8d0a48fd4c3 to your computer and use it in GitHub Desktop.
Tailwindcss Shade Generator PHP
<?php
namespace App\Support;
use Exception;
use Spatie\Color\Hex;
use Spatie\Color\Rgb;
class ShadeGenerator
{
protected ?string $color = null;
protected array $palette = [];
protected static $intensityMap = [
50 => 0.95,
100 => 0.9,
200 => 0.75,
300 => 0.6,
400 => 0.3,
600 => 0.9,
700 => 0.75,
800 => 0.6,
900 => 0.49,
];
public function __construct(string $color)
{
$this->color = $color;
}
public static function make(string $color)
{
return new self($color);
}
protected static function darken(Hex $hex, float $intensity): Rgb
{
$color = $hex->toRgb();
$r = round($color->red() * $intensity);
$g = round($color->green() * $intensity);
$b = round($color->blue() * $intensity);
return Rgb::fromString("rgb({$r}, {$g}, {$b})");
}
protected static function lighten(Hex $hex, float $intensity): Rgb
{
$color = $hex->toRgb();
$r = round($color->red() + (255 - $color->red()) * $intensity);
$g = round($color->green() + (255 - $color->green()) * $intensity);
$b = round($color->blue() + (255 - $color->blue()) * $intensity);
return Rgb::fromString("rgb({$r}, {$g}, {$b})");
}
protected static function getLightShades(Hex $hex): array
{
$levels = [50, 100, 200, 300, 400];
$shades = [];
foreach ($levels as $level) {
$color = static::lighten($hex, static::$intensityMap[$level]);
$rgb = "{$color->red()}, {$color->green()}, {$color->blue()}";
$shades[(string) $level] = $rgb;
}
return $shades;
}
protected static function getDarkShades(Hex $hex): array
{
$levels = [600, 700, 800, 900];
$shades = [];
foreach ($levels as $level) {
$color = static::darken($hex, static::$intensityMap[$level]);
$rgb = "{$color->red()}, {$color->green()}, {$color->blue()}";
$shades[(string) $level] = $rgb;
}
return $shades;
}
protected static function getPrimaryShade(Hex $hex): array
{
$color = $hex->toRgb();
$rgb = "{$color->red()}, {$color->green()}, {$color->blue()}";
return [
500 => $rgb,
];
}
protected function prefixRenderedColors(string $prefix): array
{
$colors = $this->colors;
return array_combine(
array_map(
function ($key) use ($prefix) {
return $prefix . $key;
},
array_keys($this->colors)
),
$colors
);
}
public function palette(): self
{
$hex = Hex::fromString($this->color);
$lightShades = static::getLightShades($hex);
$primaryShade = static::getPrimaryShade($hex);
$darkShades = static::getDarkShades($hex);
$this->palette = $lightShades + $primaryShade + $darkShades;
return $this;
}
public function getPalette(): array
{
return $this->palette;
}
public function toCss(string $name): string
{
if (! $palette = $this->getPalette()) {
throw new Exception('Please call `->palette()` to generate a palette before converting it to CSS.');
}
return collect($palette)
->map(
fn ($rgb, $shade) => <<<EOT
.bg-{$name}-{$shade} {
background-color: rgb($rgb);
}
.text-{$name}-{$shade} {
color: rgb($rgb);
}
EOT
)
->join("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment