Skip to content

Instantly share code, notes, and snippets.

@robertdrakedennis
Created November 17, 2021 01:57
Show Gist options
  • Save robertdrakedennis/00445592b655b3c88ade1a75cbbb40f6 to your computer and use it in GitHub Desktop.
Save robertdrakedennis/00445592b655b3c88ade1a75cbbb40f6 to your computer and use it in GitHub Desktop.
Tailwind shade generator using Spatie/Color
<?php
namespace App\Traits;
use Spatie\Color\Hex;
use Spatie\Color\Rgb;
trait SetupColors
{
protected 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 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 function renderLightShades(Hex $hex): void
{
$levels = [50, 100, 200, 300, 400];
foreach ($levels as $level) {
$color = $this->lighten($hex, $this->intensityMap[$level]);
$rgb = "{$color->red()}, {$color->green()}, {$color->blue()}";
$this->colors[$level] = $rgb;
}
}
protected function renderDarkShades(Hex $hex): void
{
$levels = [600, 700, 800, 900];
foreach ($levels as $level) {
$color = $this->darken($hex, $this->intensityMap[$level]);
$rgb = "{$color->red()}, {$color->green()}, {$color->blue()}";
$this->colors[$level] = $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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment