Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created September 1, 2013 21:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save phpfiddle/6407539 to your computer and use it in GitHub Desktop.
Save phpfiddle/6407539 to your computer and use it in GitHub Desktop.
Php color palette shade generator
<?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