Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended HSL/HSLA/HSB Handling with Float Support #93

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/HsPatterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Spatie\Color;

class HsPatterns
{
protected const HUE = '\d{1,3}';
protected const COMPONENT = '\d{1,3}(?:\.\d+)?%?';
protected const ALPHA = '[0-1](?:\.\d{1,2})?';

private const VALIDATION_PATTERNS = [
'hsb' => '/^ *hs[vb]\( *-?' . self::HUE . ' *, *' . self::COMPONENT . ' *, *' . self::COMPONENT . ' *\) *$/i',
'hsl' => '/^ *hsl\( *-?' . self::HUE . ' *, *' . self::COMPONENT . ' *, *' . self::COMPONENT . ' *\) *$/i',
'hsla' => '/^ *hsla\( *' . self::HUE . ' *, *' . self::COMPONENT . ' *, *' . self::COMPONENT . ' *, *' . self::ALPHA . ' *\) *$/i',
];

private const EXTRACTION_PATTERNS = [
'hsb' => '/hs[vb]\( *(-?' . self::HUE . ') *, *(' . self::COMPONENT . ') *, *(' . self::COMPONENT . ') *\)/i',
'hsl' => '/hsl\( *(-?' . self::HUE . ') *, *(' . self::COMPONENT . ') *, *(' . self::COMPONENT . ') *\)/i',
'hsla' => '/hsla\( *(' . self::HUE . ') *, *(' . self::COMPONENT . ') *, *(' . self::COMPONENT . ') *, *(' . self::ALPHA . ') *\)/i',
];

public static function getValidationPattern(string $type): string
{
if (!isset(self::VALIDATION_PATTERNS[$type])) {
throw new \InvalidArgumentException('Invalid color type: ' . $type);
}

return self::VALIDATION_PATTERNS[$type];
}

public static function getExtractionPattern(string $type): string
{
if (!isset(self::EXTRACTION_PATTERNS[$type])) {
throw new \InvalidArgumentException('Invalid color type: ' . $type);
}

return self::EXTRACTION_PATTERNS[$type];
}
}
8 changes: 6 additions & 2 deletions src/Hsb.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public static function fromString(string $string)
Validate::hsbColorString($string);

$matches = null;
preg_match('/hs[vb]\( *(-?\d{1,3}) *, *(\d{1,3})%? *, *(\d{1,3})%? *\)/i', $string, $matches);
preg_match(HsPatterns::getExtractionPattern('hsb'), $string, $matches);

return new static($matches[1], $matches[2], $matches[3]);
return new static(
(float) $matches[1],
(float) $matches[2],
(float) $matches[3],
);
}

public function hue(): float
Expand Down
10 changes: 8 additions & 2 deletions src/Hsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ public static function fromString(string $string)
Validate::hslColorString($string);

$matches = null;
preg_match('/hsl\( *(-?\d{1,3}) *, *(\d{1,3})%? *, *(\d{1,3})%? *\)/i', $string, $matches);

return new static($matches[1], $matches[2], $matches[3]);

preg_match(HsPatterns::getExtractionPattern('hsl'), $string, $matches);

return new static(
(float) $matches[1],
(float) $matches[2],
(float) $matches[3],
);
}

public function hue(): float
Expand Down
9 changes: 7 additions & 2 deletions src/Hsla.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ public static function fromString(string $string)
Validate::hslaColorString($string);

$matches = null;
preg_match('/hsla\( *(\d{1,3}) *, *(\d{1,3})%? *, *(\d{1,3})%? *, *([0-1](\.\d{1,2})?) *\)/i', $string, $matches);
preg_match(HsPatterns::getExtractionPattern('hsla'), $string, $matches);

return new static($matches[1], $matches[2], $matches[3], $matches[4]);
return new static(
(float) $matches[1],
(float) $matches[2],
(float) $matches[3],
(float) $matches[4]
);
}

public function hue(): float
Expand Down
6 changes: 3 additions & 3 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,21 @@ public static function hexColorString($string): void

public static function hsbColorString($string): void
{
if (! preg_match('/^ *hs[vb]\( *-?\d{1,3} *, *\d{1,3}%? *, *\d{1,3}%? *\) *$/i', $string)) {
if (! preg_match(HsPatterns::getValidationPattern('hsb'), $string)) {
throw InvalidColorValue::malformedHslColorString($string);
}
}

public static function hslColorString($string): void
{
if (! preg_match('/^ *hsl\( *-?\d{1,3} *, *\d{1,3}%? *, *\d{1,3}%? *\) *$/i', $string)) {
if (! preg_match(HsPatterns::getValidationPattern('hsl'), $string)) {
throw InvalidColorValue::malformedHslColorString($string);
}
}

public static function hslaColorString($string): void
{
if (! preg_match('/^ *hsla\( *\d{1,3} *, *\d{1,3}%? *, *\d{1,3}%? *, *[0-1](\.\d{1,2})? *\) *$/i', $string)) {
if (! preg_match(HsPatterns::getValidationPattern('hsla'), $string)) {
throw InvalidColorValue::malformedHslaColorString($string);
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Datasets/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
yield 'hsla(200, 65%, 75%, 0.1)' => ['hsla(200, 65%, 75%, 0.1)', 150, 205, 233];
yield 'hsla(242, 35%, 25%, 0.1)' => ['hsla(242, 35%, 25%, 0.1)', 43, 41, 86];
yield 'hsla(319, 65%, 25%, 0.1)' => ['hsla(319, 65%, 25%, 0.1)', 105, 22, 79];
yield 'hsla(357, 100%, 44.5%, 0.1)' => ['hsla(357, 100%, 44.5%, 0.1)', 227, 0, 11];
yield 'hsla(357, 12.12%, 44.5%, 0.1)' => ['hsla(357, 12.12%, 44.5%, 0.1)', 127, 100, 101];
});

dataset('hsl_string_and_rgb_values', function () {
Expand All @@ -98,4 +100,5 @@
yield 'hsl(319, 65%, 25%)' => ['hsl(319, 65%, 25%)', 105, 22, 79];
yield 'hsl(-1, 50%, 50%)' => ['hsl(-1, 50%, 50%)', 191, 64, 66];
yield 'hsl(359, 50%, 50%)' => ['hsl(359, 50%, 50%)', 191, 64, 66];
yield 'hsl(221, 11.52%, 32.35%)' => ['hsl(221, 11.52%, 32.35%)', 73, 79, 92];
});
Loading