|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Color; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\Color\Exceptions\InvalidColorValue; |
|
6
|
|
|
|
|
7
|
|
|
class Validate |
|
8
|
|
|
{ |
|
9
|
|
|
public static function rgbChannelValue(int $value, string $channel) |
|
10
|
|
|
{ |
|
11
|
|
|
if ($value < 0 || $value > 255) { |
|
12
|
|
|
throw InvalidColorValue::rgbChannelValueNotInRange($value, $channel); |
|
13
|
|
|
} |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public static function alphaChannelValue(float $value) |
|
17
|
|
|
{ |
|
18
|
|
|
if ($value < 0 || $value > 1) { |
|
19
|
|
|
throw InvalidColorValue::alphaChannelValueNotInRange($value); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function hexChannelValue(string $value) |
|
24
|
|
|
{ |
|
25
|
|
|
if (strlen($value) !== 2) { |
|
26
|
|
|
throw InvalidColorValue::hexChannelValueHasInvalidLength($value); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (! preg_match('/[a-f0-9]{2}/i', $value)) { |
|
30
|
|
|
throw InvalidColorValue::hexValueContainsInvalidCharacters($value); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function hslValue(float $value, string $name) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($value < 0 || $value > 100) { |
|
37
|
|
|
throw InvalidColorValue::hslValueNotInRange($value, $name); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function rgbColorString($string) |
|
42
|
|
|
{ |
|
43
|
|
|
if (! preg_match('/^ *rgb\( *\d{1,3} *, *\d{1,3} *, *\d{1,3} *\) *$/i', $string)) { |
|
44
|
|
|
throw InvalidColorValue::malformedRgbColorString($string); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function rgbaColorString($string) |
|
49
|
|
|
{ |
|
50
|
|
|
if (! preg_match('/^ *rgba\( *\d{1,3} *, *\d{1,3} *, *\d{1,3} *, *[0-1](\.\d{1,2})? *\) *$/i', $string)) { |
|
51
|
|
|
throw InvalidColorValue::malformedRgbaColorString($string); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function hexColorString($string) |
|
56
|
|
|
{ |
|
57
|
|
|
if (! preg_match('/^#[a-f0-9]{6}$/i', $string)) { |
|
58
|
|
|
throw InvalidColorValue::malformedHexColorString($string); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function hslColorString($string) |
|
63
|
|
|
{ |
|
64
|
|
|
if (! preg_match('/^ *hsl\( *-?\d{1,3} *, *\d{1,3}%? *, *\d{1,3}%? *\) *$/i', $string)) { |
|
65
|
|
|
throw InvalidColorValue::malformedHslColorString($string); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function hslaColorString($string) |
|
70
|
|
|
{ |
|
71
|
|
|
if (! preg_match('/^ *hsla\( *\d{1,3} *, *\d{1,3}%? *, *\d{1,3}%? *, *[0-1](\.\d{1,2})? *\) *$/i', $string)) { |
|
72
|
|
|
throw InvalidColorValue::malformedHslaColorString($string); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|