1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the class {@see RGBAColor_PresetsManager}. |
4
|
|
|
* |
5
|
|
|
* @package AppUtils |
6
|
|
|
* @subpackage RGBAColor |
7
|
|
|
* @see RGBAColor_PresetsManager |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The presets manager allows adding more presets that can |
16
|
|
|
* then be used with the {@see RGBAColor_Presets} class to |
17
|
|
|
* create color instances. |
18
|
|
|
* |
19
|
|
|
* To get the manager instance, use the factory's method: |
20
|
|
|
* {@see RGBAColor_Factory::getPresetsManager()}. |
21
|
|
|
* |
22
|
|
|
* @package AppUtils |
23
|
|
|
* @subpackage RGBAColor |
24
|
|
|
* @author Sebastian Mordziol <[email protected]> |
25
|
|
|
* |
26
|
|
|
* @link https://en.wikipedia.org/wiki/List_of_colors_(compact) |
27
|
|
|
*/ |
28
|
|
|
class RGBAColor_PresetsManager |
29
|
|
|
{ |
30
|
|
|
const ERROR_CANNOT_OVERWRITE_BUILT_IN_PRESET = 94001; |
31
|
|
|
|
32
|
|
|
const COLOR_WHITE = 'white'; |
33
|
|
|
const COLOR_BLACK = 'black'; |
34
|
|
|
const COLOR_TRANSPARENT = 'transparent'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array<string,array<string,int>> |
38
|
|
|
*/ |
39
|
|
|
private static $globalPresets = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private static $initialized = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array<string,array<string,int>> |
48
|
|
|
*/ |
49
|
|
|
private $customPresets = array(); |
50
|
|
|
|
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$this->init(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Registers the global color presets. |
58
|
|
|
* |
59
|
|
|
* @throws RGBAColor_Exception |
60
|
|
|
*/ |
61
|
|
|
private function init() : void |
62
|
|
|
{ |
63
|
|
|
if(self::$initialized === true) |
64
|
|
|
{ |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this |
69
|
|
|
->registerGlobalPreset(self::COLOR_WHITE, 255, 255, 255, 255) |
70
|
|
|
->registerGlobalPreset(self::COLOR_BLACK, 0,0,0, 255) |
71
|
|
|
->registerGlobalPreset(self::COLOR_TRANSPARENT, 0, 0, 0, 0); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $name |
76
|
|
|
* @return int[] |
77
|
|
|
* |
78
|
|
|
* @throws RGBAColor_Exception |
79
|
|
|
* @see RGBAColor::ERROR_UNKNOWN_COLOR_PRESET |
80
|
|
|
*/ |
81
|
|
|
public function getPreset(string $name) : array |
82
|
|
|
{ |
83
|
|
|
if(isset($this->customPresets[$name])) |
84
|
|
|
{ |
85
|
|
|
return $this->customPresets[$name]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if(isset(self::$globalPresets[$name])) |
89
|
|
|
{ |
90
|
|
|
return self::$globalPresets[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new RGBAColor_Exception( |
94
|
|
|
'No such color preset.', |
95
|
|
|
sprintf( |
96
|
|
|
'The color preset [%s] has not been registered, either as global or custom preset.', |
97
|
|
|
$name |
98
|
|
|
), |
99
|
|
|
RGBAColor::ERROR_UNKNOWN_COLOR_PRESET |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $name |
105
|
|
|
* @param int $red |
106
|
|
|
* @param int $green |
107
|
|
|
* @param int $blue |
108
|
|
|
* @param int $alpha |
109
|
|
|
* @return $this |
110
|
|
|
* @throws RGBAColor_Exception |
111
|
|
|
*/ |
112
|
|
|
private function registerGlobalPreset(string $name, int $red, int $green, int $blue, int $alpha) : RGBAColor_PresetsManager |
113
|
|
|
{ |
114
|
|
|
$this->requireNotGlobal($name); |
115
|
|
|
|
116
|
|
|
if(!isset(self::$globalPresets[$name])) |
117
|
|
|
{ |
118
|
|
|
self::$globalPresets[$name] = array( |
119
|
|
|
RGBAColor::COMPONENT_RED => $red, |
120
|
|
|
RGBAColor::COMPONENT_GREEN => $green, |
121
|
|
|
RGBAColor::COMPONENT_BLUE=> $blue, |
122
|
|
|
RGBAColor::COMPONENT_ALPHA => $alpha |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw new RGBAColor_Exception( |
129
|
|
|
'Cannot replace global color preset', |
130
|
|
|
sprintf( |
131
|
|
|
'The built-in global presets like [%s] may not be overwritten. Prefer adding a regular preset instead.', |
132
|
|
|
$name |
133
|
|
|
), |
134
|
|
|
self::ERROR_CANNOT_OVERWRITE_BUILT_IN_PRESET |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function registerPreset(string $name, int $red, int $green, int $blue, int $alpha=255) : RGBAColor_PresetsManager |
139
|
|
|
{ |
140
|
|
|
$this->requireNotGlobal($name); |
141
|
|
|
|
142
|
|
|
$this->customPresets[$name] = array( |
143
|
|
|
RGBAColor::COMPONENT_RED => $red, |
144
|
|
|
RGBAColor::COMPONENT_GREEN => $green, |
145
|
|
|
RGBAColor::COMPONENT_BLUE=> $blue, |
146
|
|
|
RGBAColor::COMPONENT_ALPHA => $alpha |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function requireNotGlobal(string $name) : void |
153
|
|
|
{ |
154
|
|
|
if(!isset(self::$globalPresets[$name])) |
155
|
|
|
{ |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
throw new RGBAColor_Exception( |
160
|
|
|
'Cannot replace global color preset', |
161
|
|
|
sprintf( |
162
|
|
|
'The built-in global presets like [%s] may not be overwritten.', |
163
|
|
|
$name |
164
|
|
|
), |
165
|
|
|
self::ERROR_CANNOT_OVERWRITE_BUILT_IN_PRESET |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|