Skip to content

Commit

Permalink
Support hex colors with alpha channel: RGBA and RRGGBBAA (#337)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
fatihkizmaz authored Mar 4, 2024
1 parent 587410c commit 1dcb9c7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/claviska/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 1dcb9c7

Please sign in to comment.