-
-
Save Njengah/415fa17bd93d5520b263434a7ee3f314 to your computer and use it in GitHub Desktop.
Php color palette shade generator
This file contains hidden or 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 | |
class ColorPalette{ | |
public $color; | |
public function __construct($color){ | |
$this->color = $color; | |
} | |
public function color_mod($hex, $diff) { | |
$rgb = str_split(trim($hex, '# '), 2); | |
foreach ($rgb as &$hex) { | |
$dec = hexdec($hex); | |
if ($diff >= 0) { | |
$dec += $diff; | |
} | |
else { | |
$dec -= abs($diff); | |
} | |
$dec = max(0, min(255, $dec)); | |
$hex = str_pad(dechex($dec), 2, '0', STR_PAD_LEFT); | |
} | |
return '#'.implode($rgb); | |
} | |
public function createPalette($colorCount=4){ | |
$colorPalette = array(); | |
for($i=1; $i<=$colorCount; $i++){ | |
if($i == 1){ | |
$color = $this->color; | |
$colorVariation = -(($i*4) * 15); | |
} | |
if($i == 2){ | |
$color = $newColor; | |
$colorVariation = -($i * 15); | |
} | |
if($i == 3){ | |
$color = $newColor; | |
$colorVariation = -($i * 15); | |
} | |
if($i == 4){ | |
$color = $newColor; | |
$colorVariation = -($i * 15); | |
} | |
$newColor = $this->color_mod($color, $colorVariation); | |
array_push($colorPalette, $newColor); | |
} | |
return $colorPalette; | |
} | |
} | |
$color = new ColorPalette('#EF1D1D'); | |
$colorPalette = $color->createPalette(); | |
foreach($colorPalette as $color){?> | |
<div style="height:100px; background:<?php echo($color);?>">Box</div> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment