Skip to content

Commit 472e11c

Browse files
committed
Add checks for number of color channels
1 parent de0fd7f commit 472e11c

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

src/Colors/Cmyk/Colorspace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class Colorspace implements ColorspaceInterface
4040
*/
4141
public static function colorFromNormalized(array $normalized): CmykColor
4242
{
43+
if (!in_array(count($normalized), [4, 5])) {
44+
throw new InvalidArgumentException('Number of color channels must be 4 or 5 for ' . static::class);
45+
}
46+
4347
// add alpha value if missing
4448
$normalized = count($normalized) === 4 ? array_pad($normalized, 5, 1) : $normalized;
4549

src/Colors/Hsl/Colorspace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class Colorspace implements ColorspaceInterface
4242
*/
4343
public static function colorFromNormalized(array $normalized): HslColor
4444
{
45+
if (!in_array(count($normalized), [3, 4])) {
46+
throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);
47+
}
48+
4549
// add alpha value if missing
4650
$normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;
4751

src/Colors/Hsv/Colorspace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class Colorspace implements ColorspaceInterface
4242
*/
4343
public static function colorFromNormalized(array $normalized): HsvColor
4444
{
45+
if (!in_array(count($normalized), [3, 4])) {
46+
throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);
47+
}
48+
4549
// add alpha value if missing
4650
$normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;
4751

src/Colors/Oklab/Colorspace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class Colorspace implements ColorspaceInterface
4141
*/
4242
public static function colorFromNormalized(array $normalized): OklabColor
4343
{
44+
if (!in_array(count($normalized), [3, 4])) {
45+
throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);
46+
}
47+
4448
// add alpha value if missing
4549
$normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;
4650

src/Colors/Oklch/Colorspace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class Colorspace implements ColorspaceInterface
4444
*/
4545
public static function colorFromNormalized(array $normalized): OklchColor
4646
{
47+
if (!in_array(count($normalized), [3, 4])) {
48+
throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);
49+
}
50+
4751
// add alpha value if missing
4852
$normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;
4953

src/Colors/Rgb/Colorspace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class Colorspace implements ColorspaceInterface
4343
*/
4444
public static function colorFromNormalized(array $normalized): RgbColor
4545
{
46+
if (!in_array(count($normalized), [3, 4])) {
47+
throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);
48+
}
49+
4650
// add alpha value if missing
4751
$normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;
4852

0 commit comments

Comments
 (0)