-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
RgbTest.php
150 lines (116 loc) · 4.04 KB
/
RgbTest.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
<?php
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertNotSame;
use function PHPUnit\Framework\assertSame;
use Spatie\Color\Exceptions\InvalidColorValue;
use Spatie\Color\Rgb;
it('is initializable', function () {
$rgb = new Rgb(55, 155, 255);
assertInstanceOf(Rgb::class, $rgb);
assertSame(55, $rgb->red());
assertSame(155, $rgb->green());
assertSame(255, $rgb->blue());
});
it('cant be initialized with a negative color value', function () {
new Rgb(-5, 255, 255);
})->throws(InvalidColorValue::class);
it('cant be initialized with a color value higher than 255', function () {
new Rgb(300, 255, 255);
})->throws(InvalidColorValue::class);
it('can be created from a string', function () {
$rgb = Rgb::fromString('rgb(55,155,255)');
assertInstanceOf(Rgb::class, $rgb);
assertSame(55, $rgb->red());
assertSame(155, $rgb->green());
assertSame(255, $rgb->blue());
});
it('can be created from a string with spaces', function () {
$rgb = Rgb::fromString(' rgb( 55 , 155 , 255 ) ');
assertInstanceOf(Rgb::class, $rgb);
assertSame(55, $rgb->red());
assertSame(155, $rgb->green());
assertSame(255, $rgb->blue());
});
it('cant be created from malformed string', function () {
Rgb::fromString('rgb(55,155,255');
})->throws(InvalidColorValue::class);
it('cant be created from a string with text around', function () {
Rgb::fromString('abc rgb(55,155,255) abc');
})->throws(InvalidColorValue::class);
it('can be casted to a string', function () {
$rgb = new Rgb(55, 155, 255);
assertSame('rgb(55,155,255)', (string) $rgb);
});
it('can be converted to CIELab', function () {
$rgb = new Rgb(55, 155, 255);
$lab = $rgb->toCIELab();
assertSame(62.91, $lab->l());
assertSame(5.34, $lab->a());
assertSame(-57.73, $lab->b());
});
it('can be converted to cmyk', function () {
$rgb = new Rgb(55, 155, 255);
$cmyk = $rgb->toCmyk();
assertSame($rgb->red(), $cmyk->red());
assertSame($rgb->green(), $cmyk->green());
assertSame($rgb->blue(), $cmyk->blue());
});
it('can be converted to rgb', function () {
$rgb = new Rgb(55, 155, 255);
$newRgb = $rgb->toRgb();
assertSame($rgb->red(), $newRgb->red());
assertSame($rgb->green(), $newRgb->green());
assertSame($rgb->blue(), $newRgb->blue());
assertNotSame($rgb, $newRgb);
});
it('can be converted from rgb(0,0,0) to cmyk', function () {
$rgb = new Rgb(0, 0, 0);
$cmyk = $rgb->toCmyk();
assertSame($rgb->red(), $cmyk->red());
assertSame($rgb->green(), $cmyk->green());
assertSame($rgb->blue(), $cmyk->blue());
});
it('can be converted to rgba with a specific alpha value', function () {
$rgb = new Rgb(55, 155, 255);
$rgba = $rgb->toRgba(0.5);
assertSame(55, $rgba->red());
assertSame(155, $rgba->green());
assertSame(255, $rgba->blue());
assertSame(0.5, $rgba->alpha());
});
it('can be converted to hex', function () {
$rgb = new Rgb(55, 155, 255);
$hex = $rgb->toHex();
assertSame('37', $hex->red());
assertSame('9b', $hex->green());
assertSame('ff', $hex->blue());
});
it('can be converted to hsb', function () {
$rgb = new Rgb(128, 102, 102);
$hsb = $rgb->toHsb();
assertSame(0.0, $hsb->hue());
assertSame(20.0, $hsb->saturation());
assertSame(50.0, $hsb->brightness());
});
it('can be converted to hsl', function () {
$rgb = new Rgb(55, 155, 255);
$hsl = $rgb->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 () {
$rgb = new Rgb(55, 155, 255);
$hsla = $rgb->toHsla(0.5);
assertSame(55, $hsla->red());
assertSame(155, $hsla->green());
assertSame(255, $hsla->blue());
assertSame(0.5, $hsla->alpha());
});
it('can be converted to xyz', function () {
$rgb = new Rgb(55, 155, 255);
$xyz = $rgb->toXyz();
assertSame(31.3469, $xyz->x());
assertSame(31.4749, $xyz->y());
assertSame(99.0308, $xyz->z());
})->skip();