-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
RgbaTest.php
163 lines (127 loc) · 4.77 KB
/
RgbaTest.php
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
<?php
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertNotSame;
use function PHPUnit\Framework\assertSame;
use Spatie\Color\Exceptions\InvalidColorValue;
use Spatie\Color\Rgba;
it('is initializable', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
assertInstanceOf(Rgba::class, $rgba);
assertSame(55, $rgba->red());
assertSame(155, $rgba->green());
assertSame(255, $rgba->blue());
assertSame(0.5, $rgba->alpha());
});
it('cant be initialized with a negative color value', function () {
new Rgba(-5, 255, 255, 0.5);
})->throws(InvalidColorValue::class);
it('cant be initialized with a color value higher than 255', function () {
new Rgba(300, 255, 255, 0.5);
})->throws(InvalidColorValue::class);
it('cant be initialized with a negative alpha value', function () {
new Rgba(255, 255, 255, -1);
})->throws(InvalidColorValue::class);
it('cant be initialized with an alpha value higher than 1', function () {
new Rgba(255, 255, 255, 1.5);
})->throws(InvalidColorValue::class);
it('can be created from a string', function () {
$rgba = Rgba::fromString('rgba(55,155,255,0.5)');
assertInstanceOf(Rgba::class, $rgba);
assertSame(55, $rgba->red());
assertSame(155, $rgba->green());
assertSame(255, $rgba->blue());
assertSame(0.5, $rgba->alpha());
});
it('can be created with an opacity value without leading zero', function () {
$rgba = Rgba::fromString('rgba(55,155,255,.555)');
assertInstanceOf(Rgba::class, $rgba);
assertSame(55, $rgba->red());
assertSame(155, $rgba->green());
assertSame(255, $rgba->blue());
assertSame(.555, $rgba->alpha());
});
it('can be created from a string with 3 decimals in opacity', function () {
$rgba = Rgba::fromString('rgba(55,155,255,0.555)');
assertInstanceOf(Rgba::class, $rgba);
assertSame(55, $rgba->red());
assertSame(155, $rgba->green());
assertSame(255, $rgba->blue());
assertSame(0.555, $rgba->alpha());
});
it('can be created from a string with spaces', function () {
$rgba = Rgba::fromString(' rgba( 55 , 155 , 255 , 0.5 ) ');
assertInstanceOf(Rgba::class, $rgba);
assertSame(55, $rgba->red());
assertSame(155, $rgba->green());
assertSame(255, $rgba->blue());
assertSame(0.5, $rgba->alpha());
});
it('cant be created from malformed string', function () {
Rgba::fromString('rgba(55,155,255,0.5');
})->throws(InvalidColorValue::class);
it('cant be created from a string with text around', function () {
Rgba::fromString('abc rgba(55,155,255,0.5) abc');
})->throws(InvalidColorValue::class);
it('can be casted to a string', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
assertSame('rgba(55,155,255,0.50)', (string) $rgba);
});
it('can be converted to CIELab', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$lab = $rgba->toCIELab();
assertSame(62.91, $lab->l());
assertSame(5.34, $lab->a());
assertSame(-57.73, $lab->b());
});
it('can be converted to cmyk', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$cmyk = $rgba->toCmyk();
assertSame($rgba->red(), $cmyk->red());
assertSame($rgba->green(), $cmyk->green());
assertSame($rgba->blue(), $cmyk->blue());
});
it('can be converted to rgba with with a specific alpha value', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$newRgba = $rgba->toRgba(0.7);
assertSame(55, $newRgba->red());
assertSame(155, $newRgba->green());
assertSame(255, $newRgba->blue());
assertSame(0.7, $newRgba->alpha());
assertNotSame($rgba, $newRgba);
});
it('can be converted to rgb without an alpha value', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$rgb = $rgba->toRgb();
assertSame(55, $rgb->red());
assertSame(155, $rgb->green());
assertSame(255, $rgb->blue());
});
it('can be converted to hex without an alpha value', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$hex = $rgba->toHex();
assertSame('37', $hex->red());
assertSame('9b', $hex->green());
assertSame('ff', $hex->blue());
});
it('can be converted to hsl without an alpha value', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$hsl = $rgba->toHsl();
assertSame(55, $hsl->red());
assertSame(155, $hsl->green());
assertSame(255, $hsl->blue());
});
it('can be converted to hsla with a specific alpha value', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$hsla = $rgba->toHsla(0.75);
assertSame(55, $hsla->red());
assertSame(155, $hsla->green());
assertSame(255, $hsla->blue());
assertSame(0.75, $hsla->alpha());
});
it('can be converted to xyz', function () {
$rgba = new Rgba(55, 155, 255, 0.5);
$xyz = $rgba->toXyz();
assertSame(31.3469, $xyz->x());
assertSame(31.4749, $xyz->y());
assertSame(99.0308, $xyz->z());
});