Skip to content

Commit ac6adfe

Browse files
committed
TASK: Unify handling of alpha values
rbg and hsl values now work with alpha values between 0 and 100 The methods rgb, rgba, hsl and hsla are created as synonyms.
1 parent 46df29b commit ac6adfe

File tree

10 files changed

+6743
-66
lines changed

10 files changed

+6743
-66
lines changed

Build/Travis/.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

Build/Travis/composer.lock

Lines changed: 6630 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Classes/Domain/ValueObject/AbstractColor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __toString(): string
4444
public function getHexString(): string
4545
{
4646
$rgba = $this->asRgba();
47-
if ($rgba->getAlpha() == 255) {
47+
if ($rgba->getAlpha() == 100) {
4848
return '#'
4949
.str_pad(dechex((int) round($rgba->getRed())), 2, '0')
5050
.str_pad(dechex((int) round($rgba->getGreen())), 2, '0')
@@ -54,7 +54,7 @@ public function getHexString(): string
5454
.str_pad(dechex((int) round($rgba->getRed())), 2, '0')
5555
.str_pad(dechex((int) round($rgba->getGreen())), 2, '0')
5656
.str_pad(dechex((int) round($rgba->getBlue())), 2, '0')
57-
.str_pad(dechex((int) round($rgba->getAlpha())), 2, '0');
57+
.str_pad(dechex((int) round($rgba->getAlpha() * 2.55)), 2, '0');
5858
}
5959
}
6060

@@ -64,10 +64,10 @@ public function getHexString(): string
6464
public function getHslaString(): string
6565
{
6666
$hslaColor = $this->asHsla();
67-
if ($hslaColor->getAlpha() == 1) {
67+
if ($hslaColor->getAlpha() == 100) {
6868
return sprintf('hsl(%s, %s%%, %s%%)', round($hslaColor->getHue()), round($hslaColor->getSaturation()), round($hslaColor->getLightness()));
6969
} else {
70-
return sprintf('hsla(%s, %s%%, %s%%, %s)', round($hslaColor->getHue()), round($hslaColor->getSaturation()), round($hslaColor->getLightness()), round($hslaColor->getAlpha(), 2));
70+
return sprintf('hsla(%s, %s%%, %s%%, %s)', round($hslaColor->getHue()), round($hslaColor->getSaturation()), round($hslaColor->getLightness()), round($hslaColor->getAlpha()).'%');
7171
}
7272
}
7373

@@ -77,10 +77,10 @@ public function getHslaString(): string
7777
public function getRgbaString(): string
7878
{
7979
$rgbColor = $this->asRgba();
80-
if ($rgbColor->getAlpha() == 255) {
80+
if ($rgbColor->getAlpha() == 100) {
8181
return sprintf('rgb(%s, %s, %s)', round($rgbColor->getRed()), round($rgbColor->getGreen()), round($rgbColor->getBlue()));
8282
} else {
83-
return sprintf('rgba(%s, %s, %s, %s)', round($rgbColor->getRed()), round($rgbColor->getGreen()), round($rgbColor->getBlue()), $rgbColor->getAlpha());
83+
return sprintf('rgba(%s, %s, %s, %s)', round($rgbColor->getRed()), round($rgbColor->getGreen()), round($rgbColor->getBlue()), round($rgbColor->getAlpha()).'%');
8484
}
8585
}
8686

Classes/Domain/ValueObject/HslaColor.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class HslaColor extends AbstractColor implements ColorInterface
3838
* @param float $lightness
3939
* @param float $alpha
4040
*/
41-
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 1)
41+
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 100)
4242
{
4343
if ($hue < 0 || $hue > 360) {
4444
throw new \InvalidArgumentException('argument hue has to be a float between 0 and 359, '.$hue.' was given.');
@@ -49,8 +49,8 @@ public function __construct(float $hue, float $saturation, float $lightness, flo
4949
if ($lightness < 0 || $lightness > 100) {
5050
throw new \InvalidArgumentException('argument luminosity has to be a float between 0 and 100, '.$lightness.' was given.');
5151
}
52-
if ($alpha < 0 || $alpha > 1) {
53-
throw new \InvalidArgumentException('argument alpha has to be a float between 0 and 1, '.$alpha.' was given');
52+
if ($alpha < 0 || $alpha > 100) {
53+
throw new \InvalidArgumentException('argument alpha has to be a float between 0 and 100, '.$alpha.' was given');
5454
}
5555

5656
$this->hue = $hue;
@@ -102,12 +102,11 @@ public function asRgba(): RgbaColor
102102
$h = $this->hue / 360;
103103
$l = $this->lightness / 100;
104104
$s = $this->saturation / 100;
105-
$a = $this->alpha;
106105

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

110-
return new RgbaColor($rgb, $rgb, $rgb, $this->alpha * 255);
109+
return new RgbaColor($rgb, $rgb, $rgb, $this->alpha);
111110
}
112111

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

120-
return new RgbaColor($r * 255, $g * 255, $b * 255, $a * 255);
119+
return new RgbaColor($r * 255, $g * 255, $b * 255, $this->alpha);
121120
}
122121

123122
/**
@@ -155,13 +154,12 @@ private function hue2rgb(float $p, float $q, float $t): float
155154
*/
156155
public function withAdjustedAlpha(float $delta): ColorInterface
157156
{
158-
$delta = $delta / 100;
159157
$alpha = $this->getAlpha() + $delta;
160158
if ($alpha < 0) {
161159
$alpha = 0;
162160
}
163-
if ($alpha > 1) {
164-
$alpha = 1;
161+
if ($alpha > 100) {
162+
$alpha = 100;
165163
}
166164

167165
return new self(

Classes/Domain/ValueObject/RgbaColor.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class RgbaColor extends AbstractColor implements ColorInterface
3939
* @param float $blue
4040
* @param float $alpha
4141
*/
42-
public function __construct(float $red, float $green, float $blue, float $alpha = 255)
42+
public function __construct(float $red, float $green, float $blue, float $alpha = 100)
4343
{
4444
if ($red < 0 || $red > 255) {
4545
throw new \InvalidArgumentException('argument red has to be an integer between 0 and 255');
@@ -50,8 +50,8 @@ public function __construct(float $red, float $green, float $blue, float $alpha
5050
if ($blue < 0 || $blue > 255) {
5151
throw new \InvalidArgumentException('argument blue has to be an integer between 0 and 255');
5252
}
53-
if ($alpha < 0 || $alpha > 255) {
54-
throw new \InvalidArgumentException('argument alpha has to be an integer between 0 and 255');
53+
if ($alpha < 0 || $alpha > 100) {
54+
throw new \InvalidArgumentException('argument alpha has to be an integer between 0 and 100');
5555
}
5656

5757
$this->red = $red;
@@ -130,7 +130,7 @@ public function asHsla(): HslaColor
130130
$h * 359,
131131
$s * 100,
132132
$l * 100,
133-
$this->alpha / 255
133+
$this->alpha
134134
);
135135
}
136136

@@ -141,13 +141,12 @@ public function asHsla(): HslaColor
141141
*/
142142
public function withAdjustedAlpha(float $delta): ColorInterface
143143
{
144-
$delta = $delta / 100 * 255;
145144
$alpha = $this->getAlpha() + $delta;
146145
if ($alpha < 0) {
147146
$alpha = 0;
148147
}
149-
if ($alpha > 255) {
150-
$alpha = 255;
148+
if ($alpha > 100) {
149+
$alpha = 100;
151150
}
152151

153152
return new self(

Classes/Eel/ColorBuilder.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,50 @@ class ColorBuilder implements ProtectedContextAwareInterface
2020
* @param float $red 0-255
2121
* @param float $green 0-255
2222
* @param float $blue 0-255
23-
* @param float $alpha 0-255
2423
*
2524
* @return ColorHelper
2625
*/
27-
public function rgb(float $red, float $green, float $blue, float $alpha = 255): ColorHelper
26+
public function rgb(float $red, float $green, float $blue): ColorHelper
27+
{
28+
return $this->rgba($red, $green, $blue, 100);
29+
}
30+
31+
/**
32+
* @param float $red 0-255
33+
* @param float $green 0-255
34+
* @param float $blue 0-255
35+
* @param float $alpha 0-100
36+
*
37+
* @return ColorHelper
38+
*/
39+
public function rgba(float $red, float $green, float $blue, float $alpha = 100): ColorHelper
2840
{
2941
return new ColorHelper(
3042
new RgbaColor($red, $green, $blue, $alpha)
3143
);
3244
}
3345

46+
/**
47+
* @param float $hue
48+
* @param float $saturatiom
49+
* @param float $lightness
50+
*
51+
* @return ColorHelper
52+
*/
53+
public function hsl(float $hue, float $saturatiom, float $lightness): ColorHelper
54+
{
55+
return $this->hsla($hue, $saturatiom, $lightness, 100);
56+
}
57+
3458
/**
3559
* @param float $hue 0-360
3660
* @param float $saturatiom 0-100
3761
* @param float $lightness 0-100
38-
* @param float $alpha 0-1
62+
* @param float $alpha 0-100
3963
*
4064
* @return ColorHelper
4165
*/
42-
public function hsl(float $hue, float $saturatiom, float $lightness, float $alpha = 1): ColorHelper
66+
public function hsla(float $hue, float $saturatiom, float $lightness, float $alpha = 100): ColorHelper
4367
{
4468
return new ColorHelper(
4569
new HslaColor($hue, $saturatiom, $lightness, $alpha)
@@ -58,7 +82,7 @@ public function hex(string $hex): ?ColorHelper
5882
$red = hexdec($matches['red'].$matches['red']);
5983
$green = hexdec($matches['green'].$matches['green']);
6084
$blue = hexdec($matches['blue'].$matches['blue']);
61-
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) : 255;
85+
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) / 2.55 : 100;
6286

6387
return new ColorHelper(
6488
new RgbaColor($red, $green, $blue, $alpha)
@@ -67,7 +91,7 @@ public function hex(string $hex): ?ColorHelper
6791
$red = hexdec($matches['red']);
6892
$green = hexdec($matches['green']);
6993
$blue = hexdec($matches['blue']);
70-
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) : 255;
94+
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) / 2.55 : 100;
7195

7296
return new ColorHelper(
7397
new RgbaColor($red, $green, $blue, $alpha)
@@ -91,7 +115,7 @@ public function css(string $colorString): ?ColorHelper
91115
$red = hexdec($matches['red'].$matches['red']);
92116
$green = hexdec($matches['green'].$matches['green']);
93117
$blue = hexdec($matches['blue'].$matches['blue']);
94-
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) : 255;
118+
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha'].$matches['alpha']) / 2.55 : 100;
95119

96120
return new ColorHelper(
97121
new RgbaColor($red, $green, $blue, $alpha)
@@ -100,7 +124,7 @@ public function css(string $colorString): ?ColorHelper
100124
$red = hexdec($matches['red']);
101125
$green = hexdec($matches['green']);
102126
$blue = hexdec($matches['blue']);
103-
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) : 255;
127+
$alpha = isset($matches['alpha']) ? hexdec($matches['alpha']) / 2.55 : 100;
104128

105129
return new ColorHelper(
106130
new RgbaColor($red, $green, $blue, $alpha)
@@ -109,7 +133,7 @@ public function css(string $colorString): ?ColorHelper
109133
$red = $this->parseNumber($matches['red'], 255);
110134
$green = $this->parseNumber($matches['red'], 255);
111135
$blue = $this->parseNumber($matches['red'], 255);
112-
$alpha = isset($matches['alpha']) ? $this->parseNumber($matches['alpha'], 255) : 255;
136+
$alpha = isset($matches['alpha']) ? $this->parseNumber($matches['alpha'], 1) * 100 : 100;
113137

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

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

144168
return $max * ($number / 100);
169+
} elseif (substr($value, 0, 2) == '0.') {
170+
$number = (float) $value;
171+
172+
return $max * $number;
145173
} else {
146174
$value = (float) $value;
147175
if ($circle) {

Classes/Eel/ColorHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function rgb(): string
4646
return $this->color->getRgbaString();
4747
}
4848

49+
/**
50+
* @return string
51+
*/
52+
public function rgba(): string
53+
{
54+
return $this->color->getRgbaString();
55+
}
56+
4957
/**
5058
* @return string
5159
*/
@@ -54,6 +62,14 @@ public function hsl(): string
5462
return $this->color->getHslaString();
5563
}
5664

65+
/**
66+
* @return string
67+
*/
68+
public function hsla(): string
69+
{
70+
return $this->color->getHslaString();
71+
}
72+
5773
/**
5874
* @return string
5975
*/

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ css based on node properties as shown in the example below.
5757

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

6365
The methods rgb and hsl allow to specify the alpha as fourth argument
6466
expecting a float between 0 and 1 `color = ${ Color.hsl(156, 25, 75, 0.5) }`

Tests/Unit/ColorBuilderTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,37 @@ public function colorsCanBeCreatedViaRgbFactoryMethodDataProvider(): array
2121
return [
2222
[100, 0, 255, null, new RgbaColor(100, 0, 255)],
2323
[0, 0, 0, null, new RgbaColor(0, 0, 0)],
24-
[100, 0, 255, 128, new RgbaColor(100, 0, 255, 128)],
24+
[100, 0, 255, 50, new RgbaColor(100, 0, 255, 50)],
25+
[100, 0, 255, 100, new RgbaColor(100, 0, 255, 100)],
2526
];
2627
}
2728

2829
/**
2930
* @test
3031
* @dataProvider colorsCanBeCreatedViaRgbFactoryMethodDataProvider
3132
*/
32-
public function colorsCanBeCreatedViaRgbFactoryMethod(float $red, float $green, float $blue, float $alpha = null, ColorInterface $expectation)
33+
public function colorsCanBeCreatedViaRgbFactoryMethod(float $red, float $green, float $blue, ?float $alpha = null, ColorInterface $expectation)
3334
{
34-
$color = $this->builder->rgb($red, $green, $blue, $alpha ?? 255)->getColor();
35+
$color = $this->builder->rgba($red, $green, $blue, $alpha ?? 100)->getColor();
3536
$this->assertSameColor($expectation, $color);
3637
}
3738

3839
public function colorsCanBeCreatedViaHslFactoryMethodDataProvider(): array
3940
{
4041
return [
41-
[100, 25, 75, 1, new HslaColor(100, 25, 75, 1)],
42-
[100, 25, 100, 1, new HslaColor(100, 25, 100, 1)],
43-
[100, 25, 0, 1, new HslaColor(0, 0, 0, 1)],
42+
[100, 25, 75, null, new HslaColor(100, 25, 75, 100)],
43+
[100, 25, 100, 100, new HslaColor(100, 25, 100, 100)],
44+
[100, 25, 0, 100, new HslaColor(0, 0, 0, 100)],
4445
];
4546
}
4647

4748
/**
4849
* @test
4950
* @dataProvider colorsCanBeCreatedViaHslFactoryMethodDataProvider
5051
*/
51-
public function colorsCanBeCreatedViaHslFactoryMethod(float $hue, float $saturation, float $lightness, float $alpha, ColorInterface $expectation)
52+
public function colorsCanBeCreatedViaHslFactoryMethod(float $hue, float $saturation, float $lightness, ?float $alpha = null, ColorInterface $expectation)
5253
{
53-
$color = $this->builder->hsl($hue, $saturation, $lightness, $alpha ?? 1)->getColor();
54+
$color = $this->builder->hsla($hue, $saturation, $lightness, $alpha ?? 100)->getColor();
5455
$this->assertSameColor($expectation, $color);
5556
}
5657

@@ -83,9 +84,11 @@ public function colorsCanBeCreatedViaCssFactoryMethodDataProvider(): array
8384
['#FFAAEEDD', '#ffaaeedd'],
8485
['#ffeeaa88', '#ffeeaa88'],
8586
['rgb(128,128,128)', '#808080'],
86-
['rgba(128,128,128,255)', '#808080'],
87+
['rgba(128,128,128,100%)', '#808080'],
88+
['rgba(128,128,128,1)', '#808080'],
8789
['hsl(66,100%,75%)', '#f2ff80'],
8890
['hsl(66,100%,75%,1)', '#f2ff80'],
91+
['hsl(66,100%,75%,100%)', '#f2ff80'],
8992
];
9093
}
9194

0 commit comments

Comments
 (0)