From 64f4ae815d4c426aa577a141984deef2dc9024f3 Mon Sep 17 00:00:00 2001 From: ondross Date: Wed, 3 Sep 2025 13:54:46 +0200 Subject: [PATCH 1/4] new type `HexColorAlpha` --- src/HexColorAlpha.php | 70 ++++++++++++++++++++++++++++++++++++ tests/HexColorAlphaTest.phpt | 54 ++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/HexColorAlpha.php create mode 100644 tests/HexColorAlphaTest.phpt diff --git a/src/HexColorAlpha.php b/src/HexColorAlpha.php new file mode 100644 index 0000000..09458a5 --- /dev/null +++ b/src/HexColorAlpha.php @@ -0,0 +1,70 @@ +preProcess($value); + + if (!$this->isValid($value)) { + throw new InvalidTypeException('Invalid hex color string: ' . $value); + } + + $value = $this->processAlpha($value); + + $this->value = $value; + } + + public function getValue(): string + { + return $this->value; + } + + private function isValid( + string $value + ): bool + { + return (bool) \preg_match('#^\#([A-F0-9]{3,4}|[A-F0-9]{6}|[A-F0-9]{8})\z#', $value); + } + + private function preProcess( + string $value + ): string + { + return Strings::upper($value); + } + + private function processAlpha( + string $value + ): string + { + $hex = \ltrim($value, '#'); + $length = \strlen($hex); + + return match ($length) { + 8, 4 => '#' . $hex, + 6 => '#' . $hex . 'FF', + 3 => '#' . $hex . 'F', + default => throw new InvalidTypeException('Invalid hex color length (' . $length . '): ' . $value), + }; + } + +} diff --git a/tests/HexColorAlphaTest.phpt b/tests/HexColorAlphaTest.phpt new file mode 100644 index 0000000..b607d23 --- /dev/null +++ b/tests/HexColorAlphaTest.phpt @@ -0,0 +1,54 @@ +getValue()); + } + +} + +(new HexColorAlphaTest())->run(); From da246412daf283735bde1c8d8130685ceb8818b2 Mon Sep 17 00:00:00 2001 From: ondross Date: Wed, 3 Sep 2025 14:04:50 +0200 Subject: [PATCH 2/4] cs --- tests/HexColorAlphaTest.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/HexColorAlphaTest.phpt b/tests/HexColorAlphaTest.phpt index b607d23..b96e3c0 100644 --- a/tests/HexColorAlphaTest.phpt +++ b/tests/HexColorAlphaTest.phpt @@ -21,7 +21,7 @@ final class HexColorAlphaTest extends TestCase '#ff', '#zzzzzz', 'skakalpes', - '#ffffffffff' + '#ffffffffff', ]; foreach ($invalidValues as $invalidValue) { @@ -42,7 +42,7 @@ final class HexColorAlphaTest extends TestCase ]; foreach ($validValues as $validValue) { - $hexColor = HexColorAlpha::from($validValue); + HexColorAlpha::from($validValue); } $hexColor = HexColorAlpha::from('#fff'); From b3e1b1eff18eb964d1c090338c3396c1cecb5888 Mon Sep 17 00:00:00 2001 From: ondross Date: Wed, 3 Sep 2025 14:15:15 +0200 Subject: [PATCH 3/4] lower case --- src/HexColorAlpha.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HexColorAlpha.php b/src/HexColorAlpha.php index 09458a5..b902a64 100644 --- a/src/HexColorAlpha.php +++ b/src/HexColorAlpha.php @@ -22,7 +22,7 @@ public function __construct( string $value ) { - $value = $this->preProcess($value); + $value = $this->preprocess($value); if (!$this->isValid($value)) { throw new InvalidTypeException('Invalid hex color string: ' . $value); @@ -45,7 +45,7 @@ private function isValid( return (bool) \preg_match('#^\#([A-F0-9]{3,4}|[A-F0-9]{6}|[A-F0-9]{8})\z#', $value); } - private function preProcess( + private function preprocess( string $value ): string { From 2aebf712fe69353df64efdd01ce75f0810259b93 Mon Sep 17 00:00:00 2001 From: ondross Date: Wed, 3 Sep 2025 17:07:56 +0200 Subject: [PATCH 4/4] adding readme for hex color alpha --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index ee171ff..7cda9ed 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Your code will be unbreakable and your IDE will love it. + [Domain](#domain) + [Hex 32](#hex-32) + [Hex Color](#hex-color) + + [Hex Color Alpha](#hex-color-alpha) + [GUID](#guid) + [IP address](#ip-address) + [URL](#url) @@ -229,6 +230,13 @@ Lowercased 32-characters long hexadecimal string useful as container for MD5 or Uppercased 7-characters long string useful as container for color. (`#006EDB`) +### Hex color alpha + +`SmartEmailing\Types\HexColorAlpha` + +Uppercased 9-character long string useful as a container for color with an alpha channel (transparency). (`#FFFFFFFF`) + +If you provide a standard 6-digit hex color (e.g., `#000000`), it will be automatically converted by appending FF for 100% opacity (`#000000FF`). ### GUID