Skip to content

Commit

Permalink
TASK: Unify handling of alpha values
Browse files Browse the repository at this point in the history
rbg and hsl values now work with alpha values between 0 and 100
The methods rgb, rgba, hsl and hsla are created as synonyms.
  • Loading branch information
mficzel committed Aug 20, 2021
1 parent 46df29b commit ac6adfe
Show file tree
Hide file tree
Showing 10 changed files with 6,743 additions and 66 deletions.
1 change: 1 addition & 0 deletions Build/Travis/.phpunit.result.cache

Large diffs are not rendered by default.

6,630 changes: 6,630 additions & 0 deletions Build/Travis/composer.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Classes/Domain/ValueObject/AbstractColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __toString(): string
public function getHexString(): string
{
$rgba = $this->asRgba();
if ($rgba->getAlpha() == 255) {
if ($rgba->getAlpha() == 100) {
return '#'
.str_pad(dechex((int) round($rgba->getRed())), 2, '0')
.str_pad(dechex((int) round($rgba->getGreen())), 2, '0')
Expand All @@ -54,7 +54,7 @@ public function getHexString(): string
.str_pad(dechex((int) round($rgba->getRed())), 2, '0')
.str_pad(dechex((int) round($rgba->getGreen())), 2, '0')
.str_pad(dechex((int) round($rgba->getBlue())), 2, '0')
.str_pad(dechex((int) round($rgba->getAlpha())), 2, '0');
.str_pad(dechex((int) round($rgba->getAlpha() * 2.55)), 2, '0');
}
}

Expand All @@ -64,10 +64,10 @@ public function getHexString(): string
public function getHslaString(): string
{
$hslaColor = $this->asHsla();
if ($hslaColor->getAlpha() == 1) {
if ($hslaColor->getAlpha() == 100) {
return sprintf('hsl(%s, %s%%, %s%%)', round($hslaColor->getHue()), round($hslaColor->getSaturation()), round($hslaColor->getLightness()));
} else {
return sprintf('hsla(%s, %s%%, %s%%, %s)', round($hslaColor->getHue()), round($hslaColor->getSaturation()), round($hslaColor->getLightness()), round($hslaColor->getAlpha(), 2));
return sprintf('hsla(%s, %s%%, %s%%, %s)', round($hslaColor->getHue()), round($hslaColor->getSaturation()), round($hslaColor->getLightness()), round($hslaColor->getAlpha()).'%');
}
}

Expand All @@ -77,10 +77,10 @@ public function getHslaString(): string
public function getRgbaString(): string
{
$rgbColor = $this->asRgba();
if ($rgbColor->getAlpha() == 255) {
if ($rgbColor->getAlpha() == 100) {
return sprintf('rgb(%s, %s, %s)', round($rgbColor->getRed()), round($rgbColor->getGreen()), round($rgbColor->getBlue()));
} else {
return sprintf('rgba(%s, %s, %s, %s)', round($rgbColor->getRed()), round($rgbColor->getGreen()), round($rgbColor->getBlue()), $rgbColor->getAlpha());
return sprintf('rgba(%s, %s, %s, %s)', round($rgbColor->getRed()), round($rgbColor->getGreen()), round($rgbColor->getBlue()), round($rgbColor->getAlpha()).'%');
}
}

Expand Down
16 changes: 7 additions & 9 deletions Classes/Domain/ValueObject/HslaColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class HslaColor extends AbstractColor implements ColorInterface
* @param float $lightness
* @param float $alpha
*/
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 1)
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 100)
{
if ($hue < 0 || $hue > 360) {
throw new \InvalidArgumentException('argument hue has to be a float between 0 and 359, '.$hue.' was given.');
Expand All @@ -49,8 +49,8 @@ public function __construct(float $hue, float $saturation, float $lightness, flo
if ($lightness < 0 || $lightness > 100) {
throw new \InvalidArgumentException('argument luminosity has to be a float between 0 and 100, '.$lightness.' was given.');
}
if ($alpha < 0 || $alpha > 1) {
throw new \InvalidArgumentException('argument alpha has to be a float between 0 and 1, '.$alpha.' was given');
if ($alpha < 0 || $alpha > 100) {
throw new \InvalidArgumentException('argument alpha has to be a float between 0 and 100, '.$alpha.' was given');
}

$this->hue = $hue;
Expand Down Expand Up @@ -102,12 +102,11 @@ public function asRgba(): RgbaColor
$h = $this->hue / 360;
$l = $this->lightness / 100;
$s = $this->saturation / 100;
$a = $this->alpha;

if ($s == 0) {
$rgb = $l * 255;

return new RgbaColor($rgb, $rgb, $rgb, $this->alpha * 255);
return new RgbaColor($rgb, $rgb, $rgb, $this->alpha);
}

$q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s;
Expand All @@ -117,7 +116,7 @@ public function asRgba(): RgbaColor
$g = $this->hue2rgb($p, $q, $h);
$b = $this->hue2rgb($p, $q, $h - 1 / 3);

return new RgbaColor($r * 255, $g * 255, $b * 255, $a * 255);
return new RgbaColor($r * 255, $g * 255, $b * 255, $this->alpha);
}

/**
Expand Down Expand Up @@ -155,13 +154,12 @@ private function hue2rgb(float $p, float $q, float $t): float
*/
public function withAdjustedAlpha(float $delta): ColorInterface
{
$delta = $delta / 100;
$alpha = $this->getAlpha() + $delta;
if ($alpha < 0) {
$alpha = 0;
}
if ($alpha > 1) {
$alpha = 1;
if ($alpha > 100) {
$alpha = 100;
}

return new self(
Expand Down
13 changes: 6 additions & 7 deletions Classes/Domain/ValueObject/RgbaColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RgbaColor extends AbstractColor implements ColorInterface
* @param float $blue
* @param float $alpha
*/
public function __construct(float $red, float $green, float $blue, float $alpha = 255)
public function __construct(float $red, float $green, float $blue, float $alpha = 100)
{
if ($red < 0 || $red > 255) {
throw new \InvalidArgumentException('argument red has to be an integer between 0 and 255');
Expand All @@ -50,8 +50,8 @@ public function __construct(float $red, float $green, float $blue, float $alpha
if ($blue < 0 || $blue > 255) {
throw new \InvalidArgumentException('argument blue has to be an integer between 0 and 255');
}
if ($alpha < 0 || $alpha > 255) {
throw new \InvalidArgumentException('argument alpha has to be an integer between 0 and 255');
if ($alpha < 0 || $alpha > 100) {
throw new \InvalidArgumentException('argument alpha has to be an integer between 0 and 100');
}

$this->red = $red;
Expand Down Expand Up @@ -130,7 +130,7 @@ public function asHsla(): HslaColor
$h * 359,
$s * 100,
$l * 100,
$this->alpha / 255
$this->alpha
);
}

Expand All @@ -141,13 +141,12 @@ public function asHsla(): HslaColor
*/
public function withAdjustedAlpha(float $delta): ColorInterface
{
$delta = $delta / 100 * 255;
$alpha = $this->getAlpha() + $delta;
if ($alpha < 0) {
$alpha = 0;
}
if ($alpha > 255) {
$alpha = 255;
if ($alpha > 100) {
$alpha = 100;
}

return new self(
Expand Down
48 changes: 38 additions & 10 deletions Classes/Eel/ColorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,50 @@ class ColorBuilder implements ProtectedContextAwareInterface
* @param float $red 0-255
* @param float $green 0-255
* @param float $blue 0-255
* @param float $alpha 0-255
*
* @return ColorHelper
*/
public function rgb(float $red, float $green, float $blue, float $alpha = 255): ColorHelper
public function rgb(float $red, float $green, float $blue): ColorHelper
{
return $this->rgba($red, $green, $blue, 100);
}

/**
* @param float $red 0-255
* @param float $green 0-255
* @param float $blue 0-255
* @param float $alpha 0-100
*
* @return ColorHelper
*/
public function rgba(float $red, float $green, float $blue, float $alpha = 100): ColorHelper
{
return new ColorHelper(
new RgbaColor($red, $green, $blue, $alpha)
);
}

/**
* @param float $hue
* @param float $saturatiom
* @param float $lightness
*
* @return ColorHelper
*/
public function hsl(float $hue, float $saturatiom, float $lightness): ColorHelper
{
return $this->hsla($hue, $saturatiom, $lightness, 100);
}

/**
* @param float $hue 0-360
* @param float $saturatiom 0-100
* @param float $lightness 0-100
* @param float $alpha 0-1
* @param float $alpha 0-100
*
* @return ColorHelper
*/
public function hsl(float $hue, float $saturatiom, float $lightness, float $alpha = 1): ColorHelper
public function hsla(float $hue, float $saturatiom, float $lightness, float $alpha = 100): ColorHelper
{
return new ColorHelper(
new HslaColor($hue, $saturatiom, $lightness, $alpha)
Expand All @@ -58,7 +82,7 @@ public function hex(string $hex): ?ColorHelper
$red = hexdec($matches['red'].$matches['red']);
$green = hexdec($matches['green'].$matches['green']);
$blue = hexdec($matches['blue'].$matches['blue']);
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) : 255;
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) / 2.55 : 100;

return new ColorHelper(
new RgbaColor($red, $green, $blue, $alpha)
Expand All @@ -67,7 +91,7 @@ public function hex(string $hex): ?ColorHelper
$red = hexdec($matches['red']);
$green = hexdec($matches['green']);
$blue = hexdec($matches['blue']);
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) : 255;
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) / 2.55 : 100;

return new ColorHelper(
new RgbaColor($red, $green, $blue, $alpha)
Expand All @@ -91,7 +115,7 @@ public function css(string $colorString): ?ColorHelper
$red = hexdec($matches['red'].$matches['red']);
$green = hexdec($matches['green'].$matches['green']);
$blue = hexdec($matches['blue'].$matches['blue']);
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) : 255;
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) / 2.55 : 100;

return new ColorHelper(
new RgbaColor($red, $green, $blue, $alpha)
Expand All @@ -100,7 +124,7 @@ public function css(string $colorString): ?ColorHelper
$red = hexdec($matches['red']);
$green = hexdec($matches['green']);
$blue = hexdec($matches['blue']);
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) : 255;
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) / 2.55 : 100;

return new ColorHelper(
new RgbaColor($red, $green, $blue, $alpha)
Expand All @@ -109,7 +133,7 @@ public function css(string $colorString): ?ColorHelper
$red = $this->parseNumber($matches['red'], 255);
$green = $this->parseNumber($matches['red'], 255);
$blue = $this->parseNumber($matches['red'], 255);
$alpha = isset($matches['alpha']) ? $this->parseNumber($matches['alpha'], 255) : 255;
$alpha = isset($matches['alpha']) ? $this->parseNumber($matches['alpha'], 1) * 100 : 100;

return new ColorHelper(
new RgbaColor($red, $green, $blue, $alpha)
Expand All @@ -118,7 +142,7 @@ public function css(string $colorString): ?ColorHelper
$hue = $this->parseNumber($matches['hue'], 360);
$saturation = $this->parseNumber($matches['saturation'], 100);
$lightness = $this->parseNumber($matches['lightness'], 100);
$alpha = isset($matches['alpha']) ? $this->parseNumber($matches['alpha'], 1) : 1;
$alpha = isset($matches['alpha']) ? $this->parseNumber($matches['alpha'], 1) * 100 : 100;

return new ColorHelper(
new HslaColor($hue, $saturation, $lightness, $alpha)
Expand All @@ -142,6 +166,10 @@ protected function parseNumber(string $value, int $max = 255, bool $circle = fal
);

return $max * ($number / 100);
} elseif (substr($value, 0, 2) == '0.') {
$number = (float) $value;

return $max * $number;
} else {
$value = (float) $value;
if ($circle) {
Expand Down
16 changes: 16 additions & 0 deletions Classes/Eel/ColorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function rgb(): string
return $this->color->getRgbaString();
}

/**
* @return string
*/
public function rgba(): string
{
return $this->color->getRgbaString();
}

/**
* @return string
*/
Expand All @@ -54,6 +62,14 @@ public function hsl(): string
return $this->color->getHslaString();
}

/**
* @return string
*/
public function hsla(): string
{
return $this->color->getHslaString();
}

/**
* @return string
*/
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ css based on node properties as shown in the example below.

Colors can be created from hex, rgb and hsl values
- `color = ${ Color.hex('#80e619') }` expects a hex string of 3 or 6 chars
- `color = ${ Color.rgb(100, 0, 255) }` expects three integers each between 0 and 255
- `color = ${ Color.hsl(156, 25, 75) }` expects three integers a degree 0-355 and two percent values 0-100
- `color = ${ Color.rgb(100, 0, 255) }` expects three numbers each between 0 and 255
- `color = ${ Color.hsl(156, 25, 75) }` expects three numbers a degree 0-355 and two percent values 0-100
- `color = ${ Color.rgba(100, 0, 255, 50) }` expects three numbers each between 0 and 255 plus a numbers between 0-100
- `color = ${ Color.hsla(156, 25, 75, 50) }` expects three numbers a degree 0-355 and three percent values 0-100

The methods rgb and hsl allow to specify the alpha as fourth argument
expecting a float between 0 and 1 `color = ${ Color.hsl(156, 25, 75, 0.5) }`
Expand Down
21 changes: 12 additions & 9 deletions Tests/Unit/ColorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,37 @@ public function colorsCanBeCreatedViaRgbFactoryMethodDataProvider(): array
return [
[100, 0, 255, null, new RgbaColor(100, 0, 255)],
[0, 0, 0, null, new RgbaColor(0, 0, 0)],
[100, 0, 255, 128, new RgbaColor(100, 0, 255, 128)],
[100, 0, 255, 50, new RgbaColor(100, 0, 255, 50)],
[100, 0, 255, 100, new RgbaColor(100, 0, 255, 100)],
];
}

/**
* @test
* @dataProvider colorsCanBeCreatedViaRgbFactoryMethodDataProvider
*/
public function colorsCanBeCreatedViaRgbFactoryMethod(float $red, float $green, float $blue, float $alpha = null, ColorInterface $expectation)
public function colorsCanBeCreatedViaRgbFactoryMethod(float $red, float $green, float $blue, ?float $alpha = null, ColorInterface $expectation)
{
$color = $this->builder->rgb($red, $green, $blue, $alpha ?? 255)->getColor();
$color = $this->builder->rgba($red, $green, $blue, $alpha ?? 100)->getColor();
$this->assertSameColor($expectation, $color);
}

public function colorsCanBeCreatedViaHslFactoryMethodDataProvider(): array
{
return [
[100, 25, 75, 1, new HslaColor(100, 25, 75, 1)],
[100, 25, 100, 1, new HslaColor(100, 25, 100, 1)],
[100, 25, 0, 1, new HslaColor(0, 0, 0, 1)],
[100, 25, 75, null, new HslaColor(100, 25, 75, 100)],
[100, 25, 100, 100, new HslaColor(100, 25, 100, 100)],
[100, 25, 0, 100, new HslaColor(0, 0, 0, 100)],
];
}

/**
* @test
* @dataProvider colorsCanBeCreatedViaHslFactoryMethodDataProvider
*/
public function colorsCanBeCreatedViaHslFactoryMethod(float $hue, float $saturation, float $lightness, float $alpha, ColorInterface $expectation)
public function colorsCanBeCreatedViaHslFactoryMethod(float $hue, float $saturation, float $lightness, ?float $alpha = null, ColorInterface $expectation)
{
$color = $this->builder->hsl($hue, $saturation, $lightness, $alpha ?? 1)->getColor();
$color = $this->builder->hsla($hue, $saturation, $lightness, $alpha ?? 100)->getColor();
$this->assertSameColor($expectation, $color);
}

Expand Down Expand Up @@ -83,9 +84,11 @@ public function colorsCanBeCreatedViaCssFactoryMethodDataProvider(): array
['#FFAAEEDD', '#ffaaeedd'],
['#ffeeaa88', '#ffeeaa88'],
['rgb(128,128,128)', '#808080'],
['rgba(128,128,128,255)', '#808080'],
['rgba(128,128,128,100%)', '#808080'],
['rgba(128,128,128,1)', '#808080'],
['hsl(66,100%,75%)', '#f2ff80'],
['hsl(66,100%,75%,1)', '#f2ff80'],
['hsl(66,100%,75%,100%)', '#f2ff80'],
];
}

Expand Down
Loading

0 comments on commit ac6adfe

Please sign in to comment.