Skip to content

Instantly share code, notes, and snippets.

@svecon
Created March 18, 2015 13:20
Show Gist options
  • Save svecon/f1e7f877c413bba89b15 to your computer and use it in GitHub Desktop.
Save svecon/f1e7f877c413bba89b15 to your computer and use it in GitHub Desktop.
function _color_rgb2hsl($rgb) {
$r = $rgb[0];
$g = $rgb[1];
$b = $rgb[2];
$min = min($r, min($g, $b));
$max = max($r, max($g, $b));
$delta = $max - $min;
$l = ($min + $max) / 2;
$s = 0;
if ($l > 0 && $l < 1) {
$s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
}
$h = 0;
if ($delta > 0) {
if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
$h /= 6;
}
return array($h, $s, $l);
}
function _color_hsl2rgb($hsl) {
$h = $hsl[0];
$s = $hsl[1];
$l = $hsl[2];
$m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
$m1 = $l * 2 - $m2;
return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
_color_hue2rgb($m1, $m2, $h),
_color_hue2rgb($m1, $m2, $h - 0.33333));
}
function _color_hue2rgb($m1, $m2, $h) {
$h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
if ($h * 2 < 1) return $m2;
if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
return $m1;
}
function _color_unpack($hex, $normalize = false) {
if (strlen($hex) == 4) {
$hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
}
$c = hexdec($hex);
for ($i = 16; $i >= 0; $i -= 8) {
$out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
}
return $out;
}
function _color_pack($rgb, $normalize = false) {
foreach ($rgb as $k => $v) {
@$out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
}
return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}
function color_change($hex, $koeficient){
$hex2rgb = _color_unpack($hex,true);
$rgb2hsl = _color_rgb2hsl($hex2rgb); $lightness1 = $rgb2hsl[2];
$rgb2hsl[2] = $rgb2hsl[2]+$koeficient;
$lightness2 = $rgb2hsl[2];
if($lightness2>1){
$rgb2hsl[2] = $rgb2hsl[2]-($koeficient*2);
$lightness2 = $rgb2hsl[2];
}
$hsl2rgb = _color_hsl2rgb($rgb2hsl);
$rgb2hex = _color_pack($hsl2rgb,true);
$n1 = hexdec(substr($rgb2hex,1,2));
$n2 = hexdec(substr($rgb2hex,3,2));
$n3 = hexdec(substr($rgb2hex,5,2));
if($lightness1>0.7){$color1 = "#000000";}
else{$color1 = "#ffffff";}
if($lightness2>0.7){$color2 = "#000000";}
else{$color2 = "#ffffff";}
return array($rgb2hex, $hex, $color1, $color2, $lightness1, $lightness2);
}
/*
echo "Old hex: ".$c[1]."<br />";
echo "Lightness: ".$c[4]."<br />";
echo "<hr />New hex: ".$c[0]."<br />";
echo "Lightness: ".$c[5]."<br />";
echo "<div style='width: 200px; height: 200px; background: {$c[1]}; float: left; color: {$c[2]};'>";
echo "Sample text. Sample text. Sample text. Sample text. Sample text. Sample text. Sample text. Sample text. Sample text.";
echo "</div>";
echo "<div style='width: 200px; height: 200px; background: {$c[0]}; float: left; color: {$c[3]};'>";
echo "Sample text. Sample text. Sample text. Sample text. Sample text. Sample text. Sample text. Sample text. Sample text.";
echo "</div>";
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment