RSI helpers  0.1
RSI helpers
Color.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi;
4 
5 /**
6  * Color helpers.
7  * Default color notation is a triplet array with byte values for red, green, and blue.
8  */
9 class Color{
10 
11  /**
12  * Convert integer color to triplet array.
13  * @param mixed $value Color in integer notation.
14  * @return array Color as triplet array.
15  */
16  public static function intToRgb($value){
17  return is_integer($value) ? [($value >> 16) & 0xff,($value >> 8) & 0xff,$value & 0xff] : null;
18  }
19  /**
20  * Convert hexadecimal color to triplet array.
21  * @param mixed $value Color as hexadecimal string (3 or 6 digits, with or without leading #).
22  * @return array Color as triplet array.
23  */
24  public static function hexToRgb($value){
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;
29  default: return null;
30  }
31  return [hexdec(substr($value,0,2)),hexdec(substr($value,2,2)),hexdec(substr($value,4,2))];
32  }
33  /**
34  * Parse color to triplet array.
35  * @param mixed $value Color as hexadecimal string (3 or 6 digits, with or without leading #), integer, or triplet array.
36  * @return array Color as triplet array.
37  */
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);
42  return null;
43  }
44  /**
45  * Parse color to integer.
46  * @param mixed $value Color as hexadecimal string (3 or 6 digits, with or without leading #), integer, or triplet array.
47  * @return array Color as integer.
48  */
49  public static function toInt($value){
50  return ($value = self::toRgb($value)) ? ($value[0] << 16) | ($value[1] << 8) | $value[0] : null;
51  }
52  /**
53  * Format a color value to two digit hexadecimal.
54  * @param byte $value Color value (0..255).
55  * @return string Two digit hexadecimal representation.
56  */
57  protected static function decToHex($value){
58  return str_pad(dechex($value),2,'0',STR_PAD_LEFT);
59  }
60  /**
61  * Parse color to hexadecimal.
62  * @param mixed $value Color as hexadecimal string (3 or 6 digits, with or without leading #), integer, or triplet array.
63  * @return array Color as hexadecimal string.
64  */
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;
67  }
68  /**
69  * Grey value of a color.
70  * @param mixed $value Color as hexadecimal string (3 or 6 digits, with or without leading #), integer, or triplet array.
71  * @return byte Grey scale value (0..255).
72  */
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;
75  }
76  /**
77  * Lighten (tint) or darken (shade) a color.
78  * @param mixed $value Color as hexadecimal string (3 or 6 digits, with or without leading #), integer, or triplet array.
79  * @param float $tint Tint (0..1 makes a color lighter, with 1 = white; -1..0 makes a color darker, with -1 = black).
80  * @return array Color as triplet array.
81  */
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);
86  }
87  return $value;
88  }
89  /**
90  * Create a color palette (rainbow).
91  * @param int $count Number of colors in the palette.
92  * @return array Array with colors.
93  */
94  public static function palette($count = 12){
95  $palette = [];
96  for($i = 0; $i < $count; $i++){
97  $color = [];
98  for($j = 0; $j < 3; $j++) switch((intval($x = 6 * $i / $count) + 7 - 2 * $j) % 6){
99  case 0:
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;
104  }
105  $palette[] = $color;
106  }
107  return $palette;
108  }
109 
110 }
Color helpers.
Definition: Color.php:9
static palette($count=12)
Create a color palette (rainbow).
Definition: Color.php:94
static decToHex($value)
Format a color value to two digit hexadecimal.
Definition: Color.php:57
static toInt($value)
Parse color to integer.
Definition: Color.php:49
Definition: Color.php:3
static intToRgb($value)
Convert integer color to triplet array.
Definition: Color.php:16
static toRgb($value)
Parse color to triplet array.
Definition: Color.php:38
static hexToRgb($value)
Convert hexadecimal color to triplet array.
Definition: Color.php:24
static grey($value)
Grey value of a color.
Definition: Color.php:73
static toHex($value)
Parse color to hexadecimal.
Definition: Color.php:65
static tint($value, $tint)
Lighten (tint) or darken (shade) a color.
Definition: Color.php:82