-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenColorColors.php
More file actions
150 lines (124 loc) · 4.11 KB
/
Copy pathOpenColorColors.php
File metadata and controls
150 lines (124 loc) · 4.11 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
<?php
declare(strict_types=1);
/*
* This file is part of the PHPColor library.
*
* (c) Simon André & Raphaël Geffroy
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace PhpColor\Colors\OpenColor;
use PhpColor\Color\Palette\ColorPalette;
use PhpColor\Color\Palette\ColorPaletteInterface;
/**
* @method string gray(int $shade = 5)
* @method string red(int $shade = 5)
* @method string pink(int $shade = 5)
* @method string grape(int $shade = 5)
* @method string violet(int $shade = 5)
* @method string indigo(int $shade = 5)
* @method string blue(int $shade = 5)
* @method string cyan(int $shade = 5)
* @method string teal(int $shade = 5)
* @method string green(int $shade = 5)
* @method string lime(int $shade = 5)
* @method string yellow(int $shade = 5)
* @method string orange(int $shade = 5)
*
* @implements \IteratorAggregate<string, array<int, string>>
*/
final class OpenColorColors extends \stdClass implements \IteratorAggregate, \Countable
{
public static function colors(): self
{
/** @var array<string, array<int, string>|string> $colors */
$colors = require __DIR__.'/Resources/colors.php';
return new self($colors);
}
/**
* @param array<string, array<int, string>|string> $colors
*/
private function __construct(private readonly array $colors)
{
}
/**
* @return list<string>
*/
public function getNames(): array
{
return array_keys($this->colors);
}
/**
* @return list<int>
*/
public function getShades(?string $color = null): array
{
$color ??= 'gray';
if (!isset($this->colors[$color]) || !is_array($this->colors[$color])) {
return [];
}
return array_keys($this->colors[$color]);
}
public function get(string $color, int $shade = 5): string
{
if (!isset($this->colors[$color])) {
throw new \InvalidArgumentException(sprintf('The color "%s" does not exist.', $color));
}
if (is_string($this->colors[$color])) {
return $this->colors[$color];
}
if (!isset($this->colors[$color][$shade])) {
throw new \InvalidArgumentException(sprintf('The shade "%d" does not exist for the color "%s".', $shade, $color));
}
return $this->colors[$color][$shade];
}
public function has(string $name): bool
{
return array_key_exists($name, $this->colors);
}
public function palette(string $name): ColorPaletteInterface
{
if (!class_exists(ColorPalette::class)) {
throw new \LogicException('The palette() method requires phpcolor/phpcolor. Install it with "composer require phpcolor/phpcolor".');
}
if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf('The color "%s" does not exist.', $name));
}
$colors = $this->colors[$name];
return ColorPalette::parse(is_array($colors) ? $colors : [$name => $colors]);
}
public function count(): int
{
return count($this->colors, COUNT_RECURSIVE);
}
/**
* @return \ArrayIterator<string, array<int, string>|string>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->colors);
}
/**
* @param array<mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
if (!$this->has($name)) {
throw new \BadMethodCallException(sprintf('The method "%s" does not exist.', $name));
}
$shade = $arguments[0] ?? 5;
if (!\is_int($shade)) {
throw new \InvalidArgumentException(sprintf('The first argument of "%s" must be an integer, "%s" given.', $name, get_debug_type($shade)));
}
return $this->get($name, $shade);
}
public function __get(string $name): string
{
return $this->get($name);
}
public function __set(string $name, mixed $value): void
{
throw new \BadMethodCallException('OpenColorColors are read-only.');
}
}