bytic /
utility
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Utility; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class Colors |
||
| 7 | * @package Nip\Utility |
||
| 8 | */ |
||
| 9 | class Colors |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @return mixed |
||
| 13 | */ |
||
| 14 | 3 | public static function colors() |
|
| 15 | { |
||
| 16 | 3 | static $colors = null; |
|
| 17 | 3 | if ($colors === null) { |
|
| 18 | 1 | $colors = require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'colors.php'; |
|
| 19 | } |
||
| 20 | 3 | return $colors; |
|
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param $color |
||
| 25 | * @return array |
||
| 26 | */ |
||
| 27 | 4 | public static function rgb($color) |
|
| 28 | { |
||
| 29 | 4 | $red = substr($color, 1, 2); |
|
| 30 | 4 | $green = substr($color, 3, 2); |
|
| 31 | 4 | $blue = substr($color, 5, 2); |
|
| 32 | |||
| 33 | 4 | $red = hexdec($red); |
|
| 34 | 4 | $green = hexdec($green); |
|
| 35 | 4 | $blue = hexdec($blue); |
|
| 36 | |||
| 37 | 4 | return [$red, $green, $blue]; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $color |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | 2 | public static function hsl($color) |
|
| 45 | { |
||
| 46 | 2 | list($red, $green, $blue) = static::rgb($color); |
|
| 47 | |||
| 48 | 2 | $red /= 255; |
|
| 49 | 2 | $green /= 255; |
|
| 50 | 2 | $blue /= 255; |
|
| 51 | |||
| 52 | 2 | $min = min([$red, min([$green, $blue])]); |
|
| 53 | 2 | $max = max([$red, max([$green, $blue])]); |
|
| 54 | |||
| 55 | 2 | $delta = $max - $min; |
|
| 56 | 2 | $lightness = ($min + $max) / 2; |
|
| 57 | |||
| 58 | 2 | $saturation = 0; |
|
| 59 | 2 | if ($lightness > 0 && $lightness < 1) { |
|
| 60 | 1 | $saturation = $delta / ($lightness < 0.5 ? (2 * $lightness) : 2 - (2 * $lightness)); |
|
| 61 | } |
||
| 62 | |||
| 63 | 2 | $hue = 0; |
|
| 64 | 2 | if ($delta > 0) { |
|
| 65 | 1 | if ($max == $red && $max != $green) { |
|
| 66 | $hue += ($green - $blue) / $delta; |
||
| 67 | } |
||
| 68 | 1 | if ($max == $green && $max != $blue) { |
|
| 69 | $hue += (2 + ($blue - $red) / $delta); |
||
| 70 | } |
||
| 71 | 1 | if ($max == $blue && $max != $red) { |
|
| 72 | 1 | $hue += (4 + ($red - $green) / $delta); |
|
| 73 | } |
||
| 74 | |||
| 75 | 1 | $hue /= 6; |
|
| 76 | } |
||
| 77 | |||
| 78 | 2 | $hue *= 255; |
|
| 79 | 2 | $saturation *= 255; |
|
| 80 | 2 | $lightness *= 255; |
|
| 81 | |||
| 82 | 2 | return [floor($hue), floor($saturation), floor($lightness)]; |
|
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $color |
||
| 88 | * @return array|bool |
||
| 89 | */ |
||
| 90 | 2 | public static function name($color) |
|
| 91 | { |
||
| 92 | 2 | $color = strtoupper($color); |
|
| 93 | |||
| 94 | 2 | if (strlen($color) < 3 || strlen($color) > 7) { |
|
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | 2 | if (strlen($color) % 3 == 0) { |
|
| 99 | 2 | $color = '#' . $color; |
|
| 100 | } |
||
| 101 | |||
| 102 | 2 | if (strlen($color) == 4) { |
|
| 103 | 2 | $color = '#' . $color[1] . $color[1] . $color[2] . $color[2] . $color[3] . $color[3]; |
|
| 104 | } |
||
| 105 | |||
| 106 | 2 | list($red, $green, $blue) = static::rgb($color); |
|
| 107 | 2 | list($h, $s, $l) = static::hsl($color); |
|
| 108 | |||
| 109 | 2 | $ndf1 = 0; |
|
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 110 | 2 | $ndf2 = 0; |
|
|
0 ignored issues
–
show
|
|||
| 111 | 2 | $ndf = 0; |
|
|
0 ignored issues
–
show
|
|||
| 112 | 2 | $cl = -1; |
|
| 113 | 2 | $df = -1; |
|
| 114 | |||
| 115 | 2 | $count = count(static::colors()); |
|
| 116 | 2 | $colors = static::colors(); |
|
| 117 | |||
| 118 | 2 | for ($index = 0; $index < $count; $index++) { |
|
| 119 | 2 | if ($color == '#' . $colors[$index][0]) { |
|
| 120 | 2 | return ['#' . $colors[$index][0], $colors[$index][1], true]; |
|
| 121 | } |
||
| 122 | |||
| 123 | 2 | $ndf1 = pow($red - $colors[$index][2], 2) + pow( |
|
| 124 | 2 | $green - $colors[$index][3], |
|
| 125 | 2 | 2 |
|
| 126 | 2 | ) + pow($blue - $colors[$index][4], 2); |
|
| 127 | 2 | $ndf2 = abs(pow($h - $colors[$index][5], 2)) + pow( |
|
| 128 | 2 | $s - $colors[$index][6], |
|
| 129 | 2 | 2 |
|
| 130 | 2 | ) + abs(pow($l - $colors[$index][7], 2)); |
|
| 131 | |||
| 132 | 2 | $ndf = $ndf1 + $ndf2 * 2; |
|
| 133 | 2 | if ($df < 0 || $df > $ndf) { |
|
| 134 | 2 | $df = $ndf; |
|
| 135 | 2 | $cl = $index; |
|
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $cl < 0 ? false : ["#" . static::colors()[$cl][0], static::colors()[$cl][1], false]; |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Uses luminosity to calculate the difference between the given colors. |
||
| 145 | * The returned value should be bigger than 5 for best readability. |
||
| 146 | * |
||
| 147 | * @param string|array $color1 |
||
| 148 | * @param string|array $color2 |
||
| 149 | * @return double |
||
| 150 | */ |
||
| 151 | public static function lumDiff($color1, $color2) |
||
| 152 | { |
||
| 153 | list($red1, $green1, $blue1) = is_array($color1) ? $color1 : static::rgb($color1); |
||
| 154 | list($red2, $green2, $blue2) = is_array($color2) ? $color2 : static::rgb($color2); |
||
| 155 | |||
| 156 | $lightness1 = 0.2126 * pow($red1 / 255, 2.2) + |
||
| 157 | 0.7152 * pow($green1 / 255, 2.2) + |
||
| 158 | 0.0722 * pow($blue1 / 255, 2.2); |
||
| 159 | |||
| 160 | $lightness2 = 0.2126 * pow($red2 / 255, 2.2) + |
||
| 161 | 0.7152 * pow($green2 / 255, 2.2) + |
||
| 162 | 0.0722 * pow($blue2 / 255, 2.2); |
||
| 163 | |||
| 164 | if ($lightness1 > $lightness2) { |
||
| 165 | return ($lightness1 + 0.05) / ($lightness2 + 0.05); |
||
| 166 | } else { |
||
| 167 | return ($lightness2 + 0.05) / ($lightness1 + 0.05); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 |