Skip to content

Instantly share code, notes, and snippets.

@bowheart
Created November 8, 2016 15:44
Show Gist options
  • Save bowheart/2b1659833dc380bb10d9cdd894d87756 to your computer and use it in GitHub Desktop.
Save bowheart/2b1659833dc380bb10d9cdd894d87756 to your computer and use it in GitHub Desktop.
A few sass-style functions for color manipulation in php
<?php
class Sasser {
private $hexChars = array('a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15);
public function toRGB($hex) {
if (is_array($hex)) return $hex;
if (substr($hex, 0, 1) === '#') $hex = substr($hex, 1);
if (strlen($hex) === 3) $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
if (strlen($hex) !== 6) return 'Error: Invalid hex value';
$rgb = array(0, 0, 0);
$rgb[0] = str_pad((string) hexdec($hex[0] . $hex[1]), 2, '0', STR_PAD_LEFT);
$rgb[1] = str_pad((string) hexdec($hex[2] . $hex[3]), 2, '0', STR_PAD_LEFT);
$rgb[2] = str_pad((string) hexdec($hex[4] . $hex[5]), 2, '0', STR_PAD_LEFT);
return $rgb;
}
public function toHex($rgb) {
$hex = '#';
for ($i = 0; $i < count($rgb); $i++) {
$hex .= str_pad(dechex(round($rgb[$i])), 2, '0', STR_PAD_LEFT);
}
return $hex;
}
public function lighten($hex, $percent) {
$rgb = $this->toRGB($hex);
$hsl = $this->toHSL($rgb);
if (is_string($percent) && substr($percent, -1) === '%') $percent = (float) substr($percent, 0, -1);
$hsl[2] += $percent;
return $this->toHex($this->HSLtoRGB($hsl));
}
public function darken($hex, $percent) {
$rgb = $this->toRGB($hex);
$hsl = $this->toHSL($rgb);
if (is_string($percent) && substr($percent, -1) === '%') $percent = (int) substr($percent, 0, -1);
$hsl[2] -= $percent;
return $this->toHex($this->HSLtoRGB($hsl));
}
public function hueToRGB($p, $q, $decHue) {
if ($decHue < 0) $decHue += 1;
else if ($decHue > 1) $decHue -= 1;
if ($decHue * 6 < 1) return $p + ($q - $p) * $decHue * 6;
if ($decHue * 2 < 1) return $q;
if ($decHue * 3 < 2) return $p + ($q - $p) * (2/3 - $decHue) * 6;
return $p;
}
// hue from 0 to 360, saturation and lightness from 0 to 100
public function HSLtoRGB($hsl) {
$hue = $hsl[0]; $saturation = $hsl[1]; $lightness = $hsl[2];
if ($hue < 0) $hue += 360;
$decHue = $hue / 360;
$decSaturation = min(100, max(0, $saturation)) / 100;
$decLightness = min(100, max(0, $lightness)) / 100;
$q = $decLightness <= 0.5 ? $decLightness * ($decSaturation + 1) : $decLightness + $decSaturation - $decLightness * $decSaturation;
$p = $decLightness * 2 - $q;
$red = $this->hueToRGB($p, $q, $decHue + 1/3) * 255;
$green = $this->hueToRGB($p, $q, $decHue) * 255;
$blue = $this->hueToRGB($p, $q, $decHue - 1/3) * 255;
return array($red, $green, $blue);
}
public function toHSL($rgb) {
$red = $rgb[0]; $green = $rgb[1]; $blue = $rgb[2];
$max = max($red, $green, $blue);
$min = min($red, $green, $blue);
$lightness = $max + $min;
if ($max === $min) {
$saturation = $hue = 0;
} else {
$diff = $max - $min;
if ($lightness < 255) $saturation = $diff / $lightness;
else $saturation = $diff / (510 - $lightness);
if ($max === $red) $hue = 60 * ($green - $blue) / $diff;
elseif ($max === $green) $hue = 60 * ($blue - $red) / $diff + 120;
elseif ($max === $blue) $hue = 60 * ($red - $green) / $diff + 240;
}
return array(fmod($hue, 360), $saturation * 100, $lightness / 5.1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment