|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MikeAlmond\Color; |
|
4
|
|
|
|
|
5
|
|
|
use MikeAlmond\Color\Exceptions\ColorException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CssGenerator |
|
9
|
|
|
* @package MikeAlmond\Color |
|
10
|
|
|
*/ |
|
11
|
|
|
class CssGenerator |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param Color $color |
|
16
|
|
|
* |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
1 |
|
public static function rgb(Color $color) |
|
20
|
|
|
{ |
|
21
|
1 |
|
return sprintf( |
|
22
|
1 |
|
'rgb(%d, %d, %d)', |
|
23
|
1 |
|
$color->getRgb()['r'], |
|
24
|
1 |
|
$color->getRgb()['g'], |
|
25
|
1 |
|
$color->getRgb()['b'] |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Color $color |
|
31
|
|
|
* @param float $alpha |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function rgba(Color $color, $alpha = 1.0) |
|
36
|
|
|
{ |
|
37
|
1 |
|
return sprintf( |
|
38
|
1 |
|
'rgba(%d, %d, %d, %s)', |
|
39
|
1 |
|
$color->getRgb()['r'], |
|
40
|
1 |
|
$color->getRgb()['g'], |
|
41
|
1 |
|
$color->getRgb()['b'], |
|
42
|
1 |
|
$alpha + 0 |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Color $color |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public static function hex(Color $color) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return '#' . $color->getHex(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Color $color |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public static function hsl(Color $color) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$hsl = $color->getHsl(); |
|
64
|
|
|
|
|
65
|
1 |
|
return sprintf( |
|
66
|
1 |
|
'hsl(%s, %s%%, %s%%)', |
|
67
|
1 |
|
sprintf('%0.1f', $hsl['h'] * 360) + 0, |
|
68
|
1 |
|
sprintf('%0.1f', $hsl['s'] * 100) + 0, |
|
69
|
1 |
|
sprintf('%0.1f', $hsl['l'] * 100) + 0 |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Color $color |
|
75
|
|
|
* @param float $alpha |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public static function hsla(Color $color, float $alpha = 1.0) |
|
80
|
|
|
{ |
|
81
|
1 |
|
$hsl = $color->getHsl(); |
|
82
|
|
|
|
|
83
|
1 |
|
return sprintf( |
|
84
|
1 |
|
'hsla(%s, %s%%, %s%%, %s)', |
|
85
|
1 |
|
sprintf('%0.1f', $hsl['h'] * 360) + 0, |
|
86
|
1 |
|
sprintf('%0.1f', $hsl['s'] * 100) + 0, |
|
87
|
1 |
|
sprintf('%0.1f', $hsl['l'] * 100) + 0, |
|
88
|
1 |
|
$alpha + 0 |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param \MikeAlmond\Color\Color $color |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public static function hasName(Color $color): bool |
|
98
|
|
|
{ |
|
99
|
2 |
|
return isset(X11Colors::$colors[$color->getHex()]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param \MikeAlmond\Color\Color $color |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public static function name(Color $color): string |
|
108
|
|
|
{ |
|
109
|
2 |
|
if (!self::hasName($color)) { |
|
110
|
1 |
|
throw new ColorException('No matching CSS color name found for ' . self::hex($color)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
return X11Colors::$colors[$color->getHex()][0]; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|