17 return is_integer($value) ? [($value >> 16) & 0xff,($value >> 8) & 0xff,$value & 0xff] : null;
25 if(!is_string($value))
return null;
26 switch(strlen($value = strtolower(ltrim($value,
'#')))){
27 case 3: $value = $value[0] . $value[0] . $value[1] . $value[1] . $value[2] . $value[2];
28 case 6:
if(preg_match(
'/^[\\da-f]{6}$/',$value))
break;
31 return [hexdec(substr($value,0,2)),hexdec(substr($value,2,2)),hexdec(substr($value,4,2))];
38 public static function toRgb($value){
39 if(is_integer($value))
return self::intToRgb($value);
40 if(is_string($value))
return self::hexToRgb($value);
41 if(is_array($value) && (count($value) == 3) && (min($value) >= 0) && (max($value) < 256))
return array_values($value);
49 public static function toInt($value){
50 return ($value = self::toRgb($value)) ? ($value[0] << 16) | ($value[1] << 8) | $value[0] : null;
58 return str_pad(dechex($value),2,
'0',STR_PAD_LEFT);
65 public static function toHex($value){
66 return ($value = self::toRgb($value)) ?
'#' . static::decToHex($value[0]) . static::decToHex($value[1]) . static::decToHex($value[2]) : null;
73 public static function grey($value){
74 return ($value = self::toRgb($value)) ? round(0.2125 * $value[0] + 0.7154 * $value[1] + 0.0721 * $value[2]) : null;
82 public static function tint($value,$tint){
83 if($value = self::toRgb($value)){
84 if($tint > 0)
for($i = 0; $i < 3; $i++) $value[$i] += (255 - $value[$i]) * min($tint,1);
85 elseif($tint < 0)
for($i = 0; $i < 3; $i++) $value[$i] *= 1 + max($tint,-1);
94 public static function palette($count = 12){
96 for($i = 0; $i < $count; $i++){
98 for($j = 0; $j < 3; $j++)
switch((intval($x = 6 * $i / $count) + 7 - 2 * $j) % 6){
100 case 1: $color[] = 255;
break;
101 case 2: $color[] = round((ceil($x) - $x) * 255) ?: 255;
break;
102 case 5: $color[] = round(($x - floor($x)) * 255);
break;
103 default: $color[] = 0;
static palette($count=12)
Create a color palette (rainbow).
static decToHex($value)
Format a color value to two digit hexadecimal.
static toInt($value)
Parse color to integer.
static intToRgb($value)
Convert integer color to triplet array.
static toRgb($value)
Parse color to triplet array.
static hexToRgb($value)
Convert hexadecimal color to triplet array.
static grey($value)
Grey value of a color.
static toHex($value)
Parse color to hexadecimal.
static tint($value, $tint)
Lighten (tint) or darken (shade) a color.