1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Utils; |
4
|
|
|
|
5
|
|
|
class ColorHelpers |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Converts a color from hex to rgba. |
9
|
|
|
* |
10
|
|
|
* @since 2.0 |
11
|
|
|
* |
12
|
|
|
* @param string $color The color to convert. |
13
|
|
|
* @param int $opacity The color opacity. |
14
|
|
|
* @param string $returnType The return format, string or array. |
15
|
|
|
* |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
|
|
public static function hexToRgba($color, $opacity = 1, $returnType = 'array') |
19
|
|
|
{ |
20
|
|
|
$color = str_replace('#', '', $color); |
21
|
|
|
|
22
|
|
|
if (strlen($color) === 3) { |
23
|
|
|
$r = hexdec(substr($color, 0, 1) . substr($color, 0, 1)); |
24
|
|
|
$g = hexdec(substr($color, 1, 1) . substr($color, 1, 1)); |
25
|
|
|
$b = hexdec(substr($color, 2, 1) . substr($color, 2, 1)); |
26
|
|
|
} else { |
27
|
|
|
$r = hexdec(substr($color, 0, 2)); |
28
|
|
|
$g = hexdec(substr($color, 2, 2)); |
29
|
|
|
$b = hexdec(substr($color, 4, 2)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$rgba = [ $r, $g, $b, $opacity ]; |
33
|
|
|
|
34
|
|
|
if ($returnType === 'string') { |
35
|
|
|
return 'rgba(' . implode(',', $rgba) . ')'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $rgba; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Converts a color from rgba to hsla. |
43
|
|
|
* |
44
|
|
|
* @since 2.0 |
45
|
|
|
* |
46
|
|
|
* @param string $color The color to convert. |
47
|
|
|
* @param int $opacity The color opacity. |
48
|
|
|
* @param string $returnType The return format, string or array. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public static function rgbaToHsla($color, $opacity = 1, $returnType = 'array') |
53
|
|
|
{ |
54
|
|
|
$r = $color[0] / 255; |
55
|
|
|
$g = $color[1] / 255; |
56
|
|
|
$b = $color[2] / 255; |
57
|
|
|
|
58
|
|
|
$min = min($r, min($g, $b)); |
59
|
|
|
$max = max($r, max($g, $b)); |
60
|
|
|
$delta = $max - $min; |
61
|
|
|
|
62
|
|
|
$h = 0; |
63
|
|
|
$s = 0; |
|
|
|
|
64
|
|
|
$l = 0; |
|
|
|
|
65
|
|
|
|
66
|
|
|
if ($delta > 0) { |
67
|
|
|
if ($max === $r) { |
68
|
|
|
$h = fmod((($g - $b) / $delta), 6); |
69
|
|
|
} else if ($max === $g) { |
70
|
|
|
$h = ($b - $r) / $delta + 2; |
71
|
|
|
} else { |
72
|
|
|
$h = ($r - $g) / $delta + 4; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$h = round($h * 60); |
77
|
|
|
|
78
|
|
|
if ($h < 0) { |
79
|
|
|
$h += 360; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$l = ($min + $max) / 2; |
83
|
|
|
$s = $delta == 0 ? 0 : $delta / (1 - abs(2 * $l - 1)); |
84
|
|
|
|
85
|
|
|
$s = round($s * 100, 1); |
86
|
|
|
$l = round($l * 100, 1); |
87
|
|
|
|
88
|
|
|
$hsla = [ $h, "$s%", "$l%", $opacity ]; |
89
|
|
|
|
90
|
|
|
if ($returnType === 'string') { |
91
|
|
|
return 'hsla(' . implode(',', $hsla) . ')'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $hsla; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Converts a color from hex to hsla. |
99
|
|
|
* |
100
|
|
|
* @since 2.0 |
101
|
|
|
* |
102
|
|
|
* @param string $color The color to convert. |
103
|
|
|
* @param int $opacity The color opacity. |
104
|
|
|
* @param string $returnType The return format, string or array. |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public static function hexToHsla($color, $opacity = 1, $returnType = 'array') |
109
|
|
|
{ |
110
|
|
|
$rgba = self::hexToRgba($color, $opacity); |
111
|
|
|
return self::rgbaToHsla($rgba, $opacity, $returnType); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|