-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorCollection.php
More file actions
328 lines (201 loc) · 7.87 KB
/
ColorCollection.php
File metadata and controls
328 lines (201 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
include_once "./Color.php";
include_once "./struct/NumericallyIndexableMap.php";
class ColorCollection implements ArrayAccess, Iterator {
/**
* @var NumericallyIndexableMap
*/
protected $colorOccurrences;
/**
* @param Color[] $colors
*/
public function __construct(...$colors) {
$this->colorOccurrences = new NumericallyIndexableMap();
$this->addColors(...$colors);
}
/**
* @param Color[] $colors
*/
public function addColors(...$colors): void {
foreach ($colors as $color) {
$key = $color->getUniqueColorID();
if ($this->colorOccurrences->has($key)) $this->colorOccurrences->get($key)->increment();
else $this->colorOccurrences->set($key, new ColorOccurrence($color));
}
}
public function addColorOccurrences(...$colorOccurrences): void {
foreach ($colorOccurrences as $colorOccurrence) {
$key = $colorOccurrence->getColor()->getUniqueColorID();
if ($this->colorOccurrences->has($key)) $this->colorOccurrences->get($key)->increment(count($colorOccurrence));
else $this->colorOccurrences->set($key, $colorOccurrence);
}
}
/**
* @param Color[] $colors
*/
public function removeColors(...$colors): void {
$this->colorOccurrences->removeMany(...array_map(function (Color $color): string {
return $color->getUniqueColorID();
}, $colors));
}
public function sortByIncidence(bool $descending = true): void {
$this->colorOccurrences->uasort(function (ColorOccurrence $a, ColorOccurrence $b) use ($descending): int {
$comparisonValue = count($a) - count($b);
if ($descending) $comparisonValue *= -1;
return $comparisonValue;
});
}
public function mergeToColor($merger): Color {
return $merger(...$this->colorOccurrences->values())->getColor();
}
/**
* Attempts to merge the ColorOccurrences inside this ColorCollection based on the provided '$equivalency' and
* '$merger' functions. The '$equivalency' function is used to test whether two colors SHOULD be merged, while the
* '$merger' function actually performs the merging operation once the appropriate colors have been gathered.
*
* Note that this function greedily selects colors to merge, meaning that if a given color is found to be equivalent
* with some color in this ColorCollection, no other later colors will be able to merge with it (even though in most
* cases the later color that 'wanted' to merge with the aforementioned color would have also been found
* equivalent).
*
* For examples of valid equivalency functions (and a hint as to the correct signature of an equivalency function),
* see {@link ColorEquivalencyFunctions}.
*
* For examples of valid merger functions (and a hint as to the correct signature of a merger function), see
* {@link ColorMergerFunctions}.
*
* @param callable $equivalencyTester The function that will be used to test if two given colors are equivalent.
* @param callable $merger The function that will be used to merge a given set of 'equivalent' colors.
* @return ColorCollection A ColorCollection that was produced as a result of merging all of the colors of this
* ColorCollection that were found to be equivalent.
*/
public function getMergedColorCollection(callable $equivalencyTester, callable $merger): ColorCollection {
$result = new ColorCollection();
$colorOccurrences = iterator_to_array($this->colorOccurrences);
$colorOccurrencesLength = count($colorOccurrences);
for ($i = 0; $i < $colorOccurrencesLength; $i++) {
if (!array_key_exists($i, $colorOccurrences)) continue;
$colorsToMerge = [ $colorOccurrences[$i] ];
unset($colorOccurrences[$i]);
for ($j = $i + 1; $j < $colorOccurrencesLength; $j++) {
if (!array_key_exists($j, $colorOccurrences)) continue;
$areColorsEquivalent = $equivalencyTester(
$colorsToMerge[0]->getColor(),
$colorOccurrences[$j]->getColor()
);
if ($areColorsEquivalent) {
$colorsToMerge[] = $colorOccurrences[$j];
unset($colorOccurrences[$j]);
}
}
$result->addColorOccurrences($merger(...$colorsToMerge));
}
return $result;
}
public function getFilteredColorCollection(callable $filterFunction, bool $inverse = false): ColorCollection {
$result = new ColorCollection();
$totalOccurrences = $this->getOccurrenceCount();
if ($inverse) {
foreach ($this->colorOccurrences as $colorOccurrence) {
if (!$filterFunction($colorOccurrence, $totalOccurrences)) $result->addColorOccurrences($colorOccurrence);
}
} else {
foreach ($this->colorOccurrences as $colorOccurrence) {
if ($filterFunction($colorOccurrence, $totalOccurrences)) $result->addColorOccurrences($colorOccurrence);
}
}
return $result;
}
public function head(int $amount, int $offset = 0): ColorCollection {
$result = new ColorCollection();
$resultSize = 0;
foreach ($this->colorOccurrences as $colorOccurrence) {
if ($offset > 0) $offset--;
else if ($resultSize >= $amount) return $result;
else {
$result->addColorOccurrences($colorOccurrence);
$resultSize++;
}
}
return $result;
}
public function toHTMLColorBlockTable(int $blockSize = null, int $padding = 0): string {
$result = "";
$stylings = "display: flex;";
$stylings .= " justify-content: center;";
$stylings .= " align-items: flex-start;";
$stylings .= " flex-wrap: wrap;";
$result .= "<div style='$stylings'>";
foreach ($this->colorOccurrences as $colorOccurrence) {
$hexLabel = "<p style='" . Color::getColorBlockContentStylings() . "'>";
$hexLabel .= $colorOccurrence->getColor()->toHexString(true);
$hexLabel .= "</p>";
$result .= "<div style='padding: " . $padding . "px'>";
$result .= $colorOccurrence->toRGBLabelledHTMLColorBlock($blockSize, $hexLabel);
$result .= "</div>";
}
$result .= "</div>";
return $result;
}
/**
* Returns the grand total number of occurrences of every color in this ColorCollection.
*
* @return int The grand total number of occurrences of every color in this ColorCollection.
*/
public function getOccurrenceCount(): int {
return array_reduce(
iterator_to_array($this->colorOccurrences),
function(int $carry, ColorOccurrence $colorOccurrence): int {
return $carry + count($colorOccurrence);
},
0
);
}
/**
* Returns the number of unique colors contained in this ColorCollection.
*
* @return int The number of unique colors contained in this ColorCollection.
*/
public function getColorCount(): int {
return count($this->colorOccurrences);
}
public function __toString(): string {
$result = "";
foreach ($this->colorOccurrences as $colorOccurrence) $result .= "$colorOccurrence\n";
return $result;
}
// ArrayAccess Implementation Methods:
public function offsetExists($offset): bool {
return $this->colorOccurrences->offsetExists($offset);
}
public function offsetGet($offset): ?ColorOccurrence {
return $this->colorOccurrences->offsetGet($offset);
}
/**
* @param null $offset
* @param Color $value
*/
public function offsetSet($offset, $value): void {
if (is_null($offset)) $this->addColors($value);
else throw new Error("Cannot set Colors in a ColorCollection by index.");
}
public function offsetUnset($offset): void {
$this->colorOccurrences->offsetUnset($offset);
}
// Iterator Implementation Methods:
public function current(): ColorOccurrence {
return $this->colorOccurrences->current();
}
public function key(): int {
return $this->colorOccurrences->key();
}
public function next(): void {
$this->colorOccurrences->next();
}
public function rewind(): void {
$this->colorOccurrences->rewind();
}
public function valid(): bool {
return $this->colorOccurrences->valid();
}
}