| 1 |
<?php
|
| 2 |
// $Id: color.algorithms.inc,v 1.2.2.2 2008/08/20 16:45:41 skiquel Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Color manipulation functions
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Convert a hex triplet into a GD color.
|
| 10 |
*
|
| 11 |
* @param $img
|
| 12 |
* GD Image
|
| 13 |
* @param $hex
|
| 14 |
* String of hex
|
| 15 |
* @return
|
| 16 |
* GD Color
|
| 17 |
*/
|
| 18 |
function _color_gd($img, $hex) {
|
| 19 |
$c = array_merge(array($img), _color_unpack($hex));
|
| 20 |
return call_user_func_array('imagecolorallocate', $c);
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Blend two hex colors and return the GD color.
|
| 25 |
*
|
| 26 |
* @param $img
|
| 27 |
* GD Image
|
| 28 |
* @param $hex1
|
| 29 |
* String of first value.
|
| 30 |
* @param $hex2
|
| 31 |
* String of second value.
|
| 32 |
* @param $alpha
|
| 33 |
* Integer of alpha
|
| 34 |
* @return
|
| 35 |
* GD Color
|
| 36 |
*/
|
| 37 |
function _color_blend($img, $hex1, $hex2, $alpha) {
|
| 38 |
$in1 = _color_unpack($hex1);
|
| 39 |
$in2 = _color_unpack($hex2);
|
| 40 |
$out = array($img);
|
| 41 |
for ($i = 0; $i < 3; ++$i) {
|
| 42 |
$out[] = $in1[$i] + ($in2[$i] - $in1[$i]) * $alpha;
|
| 43 |
}
|
| 44 |
return call_user_func_array('imagecolorallocate', $out);
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Convert a hex color into an RGB triplet.
|
| 49 |
*
|
| 50 |
* @param $hex
|
| 51 |
* String of hex value
|
| 52 |
* @param $normalize
|
| 53 |
* Normalize RGB
|
| 54 |
* @return
|
| 55 |
* Array of RGB triplet
|
| 56 |
*/
|
| 57 |
function _color_unpack($hex, $normalize = FALSE) {
|
| 58 |
if (strlen($hex) == 4) {
|
| 59 |
$hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
|
| 60 |
}
|
| 61 |
$c = hexdec($hex);
|
| 62 |
for ($i = 16; $i >= 0; $i -= 8) {
|
| 63 |
$out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
|
| 64 |
}
|
| 65 |
return $out;
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Convert an RGB triplet to a hex color.
|
| 70 |
*
|
| 71 |
* @param $rgb
|
| 72 |
* Array of RGB integers
|
| 73 |
* @param $normalize
|
| 74 |
* Boolean
|
| 75 |
* @return
|
| 76 |
* String of full hex. (i.e. #ffffff)
|
| 77 |
*/
|
| 78 |
function _color_pack($rgb, $normalize = FALSE) {
|
| 79 |
$out = 0;
|
| 80 |
foreach ($rgb as $k => $v) {
|
| 81 |
$out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
|
| 82 |
}
|
| 83 |
return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Convert a HSL triplet into RGB
|
| 88 |
*
|
| 89 |
* @param $hsl
|
| 90 |
* Array of 3 integers, 0-1. HSL Value.
|
| 91 |
* @return
|
| 92 |
* Array, the RGB value
|
| 93 |
*/
|
| 94 |
function _color_hsl2rgb($hsl) {
|
| 95 |
$h = $hsl[0];
|
| 96 |
$s = $hsl[1];
|
| 97 |
$l = $hsl[2];
|
| 98 |
$m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
|
| 99 |
$m1 = $l * 2 - $m2;
|
| 100 |
return array(
|
| 101 |
_color_hue2rgb($m1, $m2, $h + 0.33333),
|
| 102 |
_color_hue2rgb($m1, $m2, $h),
|
| 103 |
_color_hue2rgb($m1, $m2, $h - 0.33333),
|
| 104 |
);
|
| 105 |
}
|
| 106 |
|
| 107 |
/**
|
| 108 |
* Helper function for _color_hsl2rgb().
|
| 109 |
*
|
| 110 |
* @param $m1
|
| 111 |
* Decimal, 0-1
|
| 112 |
* @param $m2
|
| 113 |
* Decimal, 0-1
|
| 114 |
* @param @h
|
| 115 |
* Decimal, 0-1
|
| 116 |
* @return
|
| 117 |
* Decimal, RGB value
|
| 118 |
*/
|
| 119 |
function _color_hue2rgb($m1, $m2, $h) {
|
| 120 |
$h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
|
| 121 |
if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
|
| 122 |
if ($h * 2 < 1) return $m2;
|
| 123 |
if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
|
| 124 |
return $m1;
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Convert an RGB triplet to HSL.
|
| 129 |
*
|
| 130 |
* @param $rgb
|
| 131 |
* Array of RGB integer values
|
| 132 |
* @return
|
| 133 |
* Array of HSL integer values
|
| 134 |
*/
|
| 135 |
function _color_rgb2hsl($rgb) {
|
| 136 |
$r = $rgb[0];
|
| 137 |
$g = $rgb[1];
|
| 138 |
$b = $rgb[2];
|
| 139 |
$min = min($r, min($g, $b));
|
| 140 |
$max = max($r, max($g, $b));
|
| 141 |
$delta = $max - $min;
|
| 142 |
$l = ($min + $max) / 2;
|
| 143 |
$s = 0;
|
| 144 |
if ($l > 0 && $l < 1) {
|
| 145 |
$s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
|
| 146 |
}
|
| 147 |
$h = 0;
|
| 148 |
if ($delta > 0) {
|
| 149 |
if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
|
| 150 |
if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
|
| 151 |
if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
|
| 152 |
$h /= 6;
|
| 153 |
}
|
| 154 |
return array($h, $s, $l);
|
| 155 |
}
|