7
7
use Intervention \Image \Colors \Hsv \Color as HsvColor ;
8
8
use Intervention \Image \Colors \Hsl \Color as HslColor ;
9
9
use Intervention \Image \Colors \Cmyk \Color as CmykColor ;
10
+ use Intervention \Image \Exceptions \ColorException ;
10
11
use Intervention \Image \Interfaces \ColorInterface ;
11
12
use Intervention \Image \Interfaces \ColorspaceInterface ;
12
13
@@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
34
35
}
35
36
36
37
/**
37
- * {@inheritdoc}
38
- *
39
- * @see ColorspaceInterface::importColor()
38
+ * @param ColorInterface $color
39
+ * @return ColorInterface
40
+ * @throws ColorException
40
41
*/
41
42
public function importColor (ColorInterface $ color ): ColorInterface
42
43
{
@@ -48,17 +49,35 @@ public function importColor(ColorInterface $color): ColorInterface
48
49
};
49
50
}
50
51
51
- protected function importCmykColor (CmykColor $ color ): ColorInterface
52
+ /**
53
+ * @param ColorInterface $color
54
+ * @return ColorInterface
55
+ * @throws ColorException
56
+ */
57
+ protected function importCmykColor (ColorInterface $ color ): ColorInterface
52
58
{
59
+ if (!($ color instanceof CmykColor)) {
60
+ throw new ColorException ('Unabled to import color of type ' . $ color ::class . '. ' );
61
+ }
62
+
53
63
return new Color (
54
64
(int ) (255 * (1 - $ color ->cyan ()->normalize ()) * (1 - $ color ->key ()->normalize ())),
55
65
(int ) (255 * (1 - $ color ->magenta ()->normalize ()) * (1 - $ color ->key ()->normalize ())),
56
66
(int ) (255 * (1 - $ color ->yellow ()->normalize ()) * (1 - $ color ->key ()->normalize ())),
57
67
);
58
68
}
59
69
60
- protected function importHsvColor (HsvColor $ color ): ColorInterface
70
+ /**
71
+ * @param ColorInterface $color
72
+ * @return ColorInterface
73
+ * @throws ColorException
74
+ */
75
+ protected function importHsvColor (ColorInterface $ color ): ColorInterface
61
76
{
77
+ if (!($ color instanceof HsvColor)) {
78
+ throw new ColorException ('Unabled to import color of type ' . $ color ::class . '. ' );
79
+ }
80
+
62
81
$ chroma = $ color ->value ()->normalize () * $ color ->saturation ()->normalize ();
63
82
$ hue = $ color ->hue ()->normalize () * 6 ;
64
83
$ x = $ chroma * (1 - abs (fmod ($ hue , 2 ) - 1 ));
@@ -83,8 +102,17 @@ protected function importHsvColor(HsvColor $color): ColorInterface
83
102
return $ this ->colorFromNormalized ($ values );
84
103
}
85
104
86
- protected function importHslColor (HslColor $ color ): ColorInterface
105
+ /**
106
+ * @param ColorInterface $color
107
+ * @return ColorInterface
108
+ * @throws ColorException
109
+ */
110
+ protected function importHslColor (ColorInterface $ color ): ColorInterface
87
111
{
112
+ if (!($ color instanceof HslColor)) {
113
+ throw new ColorException ('Unabled to import color of type ' . $ color ::class . '. ' );
114
+ }
115
+
88
116
// normalized values of hsl channels
89
117
list ($ h , $ s , $ l ) = array_map (function ($ channel ) {
90
118
return $ channel ->normalize ();
0 commit comments