Created
November 17, 2021 01:57
-
-
Save robertdrakedennis/00445592b655b3c88ade1a75cbbb40f6 to your computer and use it in GitHub Desktop.
Tailwind shade generator using Spatie/Color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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