-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathColor.php
More file actions
173 lines (160 loc) Β· 5.83 KB
/
Color.php
File metadata and controls
173 lines (160 loc) Β· 5.83 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
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Intervention\Image\Colors\Cmyk\Channels\Cyan;
use Intervention\Image\Colors\Cmyk\Channels\Key;
use Intervention\Image\Colors\Cmyk\Channels\Magenta;
use Intervention\Image\Colors\Cmyk\Channels\Yellow;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Oklab\Color as OklabColor;
use Intervention\Image\Colors\Oklch\Color as OklchColor;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Colors\Cmyk\Decoders\StringColorDecoder as CmykStringColorDecoder;
use Intervention\Image\Colors\Hsl\Decoders\StringColorDecoder as HslStringColorDecoder;
use Intervention\Image\Colors\Hsv\Decoders\StringColorDecoder as HsvStringColorDecoder;
use Intervention\Image\Colors\Oklab\Decoders\StringColorDecoder as OklabStringColorDecoder;
use Intervention\Image\Colors\Oklch\Decoders\StringColorDecoder as OklchStringColorDecoder;
use Intervention\Image\Colors\Rgb\Channels\Alpha as RgbAlpha;
use Intervention\Image\Colors\Cmyk\Channels\Alpha as CmykAlpha;
use Intervention\Image\Colors\Hsl\Channels\Alpha as HslAlpha;
use Intervention\Image\Colors\Hsl\Channels\Hue as HslHue;
use Intervention\Image\Colors\Hsl\Channels\Luminance;
use Intervention\Image\Colors\Hsl\Channels\Saturation as HslSaturation;
use Intervention\Image\Colors\Hsv\Channels\Alpha as HsvAlpha;
use Intervention\Image\Colors\Hsv\Channels\Hue;
use Intervention\Image\Colors\Hsv\Channels\Saturation;
use Intervention\Image\Colors\Hsv\Channels\Value;
use Intervention\Image\Colors\Oklab\Channels\A;
use Intervention\Image\Colors\Oklab\Channels\Alpha as OklabAlpha;
use Intervention\Image\Colors\Oklab\Channels\B;
use Intervention\Image\Colors\Oklab\Channels\Lightness as OklabLightness;
use Intervention\Image\Colors\Oklch\Channels\Alpha as OklchAlpha;
use Intervention\Image\Colors\Oklch\Channels\Chroma;
use Intervention\Image\Colors\Oklch\Channels\Lightness as OklchLightness;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder as RgbHexColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\NamedColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder as RgbStringColorDecoder;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\NotSupportedException;
class Color
{
/**
* Parse color from string value.
*
* @throws InvalidArgumentException
* @throws ColorException
*/
public static function parse(string $input): ColorInterface
{
try {
$color = InputHandler::usingDecoders([
RgbStringColorDecoder::class,
CmykStringColorDecoder::class,
HsvStringColorDecoder::class,
HslStringColorDecoder::class,
OklabStringColorDecoder::class,
OklchStringColorDecoder::class,
NamedColorDecoder::class,
RgbHexColorDecoder::class,
])->handle($input);
} catch (NotSupportedException | DriverException $e) {
throw new InvalidArgumentException(
'Unable to parse RGB color from input "' . $input . '"',
previous: $e,
);
}
if (!$color instanceof ColorInterface) {
throw new ColorException('Result must be instance of ' . self::class . ', got ' . $color::class);
}
return $color;
}
/**
* Create new RGB color.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public static function rgb(int|Red $r, int|Green $g, int|Blue $b, float|RgbAlpha $a = 1): RgbColor
{
return new RgbColor($r, $g, $b, $a);
}
/**
* Create new CMYK color.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public static function cmyk(
int|Cyan $c,
int|Magenta $m,
int|Yellow $y,
int|Key $k,
float|CmykAlpha $a = 1,
): CmykColor {
return new CmykColor($c, $m, $y, $k, $a);
}
/**
* Create new HSL color.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public static function hsl(int|HslHue $h, int|HslSaturation $s, int|Luminance $l, float|HslAlpha $a = 1): HslColor
{
return new HslColor($h, $s, $l, $a);
}
/**
* Create new HSV color.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public static function hsv(int|Hue $h, int|Saturation $s, int|Value $v, float|HsvAlpha $a = 1): HsvColor
{
return new HsvColor($h, $s, $v, $a);
}
/**
* Create new OKLAB color.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public static function oklab(
float|OklabLightness $l,
float|A $a,
float|B $b,
float|OklabAlpha $alpha = 1,
): OklabColor {
return new OklabColor($l, $a, $b, $alpha);
}
/**
* Create new OKLCH color.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public static function oklch(
float|OklchLightness $l,
float|Chroma $c,
float|Hue $h,
float|OklchAlpha $a = 1,
): OklchColor {
return new OklchColor($l, $c, $h, $a);
}
/**
* Create transparent RGB color.
*/
public static function transparent(): ColorInterface
{
// @phpstan-ignore missingType.checkedException
return new RgbColor(255, 255, 255, 0);
}
}