From 1dcb9c785c44960890970d26e25c437a2a252bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20K=C4=B1zmaz?= Date: Mon, 4 Mar 2024 18:41:11 +0300 Subject: [PATCH] Support hex colors with alpha channel: RGBA and RRGGBBAA (#337) * Support hex colors with alpha channel: RGBA and RRGGBBAA Added support for 4 and 8 digit hex colors with alpha channel. Eg: #00ff0080 * Fix max val. --- src/claviska/SimpleImage.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/claviska/SimpleImage.php b/src/claviska/SimpleImage.php index 703c599..75a1826 100644 --- a/src/claviska/SimpleImage.php +++ b/src/claviska/SimpleImage.php @@ -2337,18 +2337,24 @@ public static function normalizeColor(string|array $color): array $hex = strval(preg_replace('/^#/', '', $color)); // Support short and standard hex codes - if (strlen($hex) === 3) { + if (strlen($hex) === 3 || strlen($hex) === 4) { [$red, $green, $blue] = [ $hex[0].$hex[0], $hex[1].$hex[1], $hex[2].$hex[2], ]; - } elseif (strlen($hex) === 6) { + if (strlen($hex) === 4) { + $alpha = hexdec($hex[3]) / 255; + } + } elseif (strlen($hex) === 6 || strlen($hex) === 8) { [$red, $green, $blue] = [ $hex[0].$hex[1], $hex[2].$hex[3], $hex[4].$hex[5], ]; + if (strlen($hex) === 8) { + $alpha = hexdec($hex[6].$hex[7]) / 255; + } } else { throw new Exception("Invalid color value: $color", self::ERR_INVALID_COLOR); }