Skip to content

Commit c3fdea5

Browse files
committed
TASK: Refactor to colorValueObjects
Those values objects are immutable and tested via unittests that will be run by travis ci. That allowed to remove the previously included color library and implement all functionality locally.
1 parent 8222bfa commit c3fdea5

18 files changed

Lines changed: 797 additions & 1029 deletions

.travis.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: php
2+
php:
3+
- '7.2'
4+
- '7.3'
5+
- '7.4'
6+
script: composer test
7+
cache:
8+
directories:
9+
- Build/Travis/bin
10+
- Build/Travis/Build
11+
- Build/Travis/Configuration
12+
- Build/Travis/Data
13+
- Build/Travis/Packages
14+
- Build/Travis/Web

Build/Travis/.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
indent_type = space
9+
indent_size = 4
10+
11+
[*.yaml]
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

Build/Travis/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/Build/
2+
/Configuration/
3+
/Data/
4+
/Packages/
5+
/Web/
6+
/bin/
7+
/Readme.rst
8+
/Upgrading.rst
9+
/flow
10+
/flow.bat
11+
.composer.lock

Build/Travis/Routes.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# #
2+
# Routes configuration #
3+
# #
4+
# This file contains the configuration for the MVC router. #
5+
# Just add your own modifications as necessary. #
6+
# #
7+
# Please refer to the Flow manual for possible configuration options. #
8+
# #
9+
10+
##
11+
# Flow subroutes
12+
#
13+
14+
-
15+
name: 'Flow'
16+
uriPattern: '<FlowSubroutes>'
17+
defaults:
18+
'@format': 'html'
19+
subRoutes:
20+
FlowSubroutes:
21+
package: Neos.Flow

Build/Travis/composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "packagefactory/atomicfusion-proptypes-test-distribution",
3+
"description": "Flow test distribution for PackageFactory.AtomicFusion.PropTypes",
4+
"license": "MIT",
5+
"config": {
6+
"vendor-dir": "Packages/Libraries",
7+
"bin-dir": "bin"
8+
},
9+
"require": {
10+
"php": "^7.2",
11+
"neos/flow": "~6.0.0",
12+
"neos/fusion": "~5.0.0",
13+
"packagefactory/colorhelper": "*"
14+
},
15+
"require-dev": {
16+
"neos/buildessentials": "6.0.x-dev",
17+
"mikey179/vfsstream": "^1.6",
18+
"phpunit/phpunit": "^8.1",
19+
"symfony/css-selector": "~2.0",
20+
"neos/behat": "@dev"
21+
},
22+
"suggest": {
23+
"ext-pdo_sqlite": "For running functional tests out-of-the-box this is required"
24+
},
25+
"scripts": {
26+
"post-update-cmd": "Neos\\Flow\\Composer\\InstallerScripts::postUpdateAndInstall",
27+
"post-install-cmd": "Neos\\Flow\\Composer\\InstallerScripts::postUpdateAndInstall",
28+
"post-package-update": "Neos\\Flow\\Composer\\InstallerScripts::postPackageUpdateAndInstall",
29+
"post-package-install": "Neos\\Flow\\Composer\\InstallerScripts::postPackageUpdateAndInstall"
30+
}
31+
}

Build/Travis/functional-tests.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
beStrictAboutChangesToGlobalState="true"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
beStrictAboutOutputDuringTests="true"
6+
bootstrap="Build/BuildEssentials/PhpUnit/FunctionalTestBootstrap.php"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
timeoutForSmallTests="0">
11+
<testsuites>
12+
<testsuite name="All tests">
13+
<directory>Packages/Application/PackageFactory.AtomicFusion.PropTypes/Tests/Functional</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist addUncoveredFilesFromWhitelist="false">
18+
<directory>Data/Temporary/Testing/Cache/Flow_Object_Classes</directory>
19+
</whitelist>
20+
</filter>
21+
<logging>
22+
<log type="junit" target="Build/Reports/UnitTests.xml"/>
23+
</logging>
24+
<php>
25+
<ini name="date.timezone" value="Europe/Berlin" />
26+
<env name="FLOW_REWRITEURLS" value="1"/>
27+
</php>
28+
</phpunit>

Build/Travis/unit-tests.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
beStrictAboutChangesToGlobalState="true"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
beStrictAboutOutputDuringTests="true"
6+
bootstrap="Build/BuildEssentials/PhpUnit/UnitTestBootstrap.php"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
timeoutForSmallTests="0">
11+
<testsuites>
12+
<testsuite name="All tests">
13+
<directory>Packages/Application/PackageFactory.ColorHelper/Tests/Unit</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist addUncoveredFilesFromWhitelist="true">
18+
<directory>Packages/Application/PackageFactory.ColorHelper/Classes</directory>
19+
</whitelist>
20+
</filter>
21+
<logging>
22+
<log type="junit" target="Build/Reports/UnitTests.xml"/>
23+
</logging>
24+
<php>
25+
<ini name="date.timezone" value="Europe/Berlin" />
26+
</php>
27+
</phpunit>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PackageFactory\ColorHelper\Domain\ValueObject;
5+
6+
abstract class AbstractColor implements ColorInterface
7+
{
8+
9+
/**
10+
* @return string
11+
*/
12+
public function __toString(): string
13+
{
14+
return $this->getHexString();
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getHexString():string
21+
{
22+
$rgba = $this->asRgba();
23+
if ($rgba->getAlpha() == 255) {
24+
return '#'
25+
. str_pad(dechex($rgba->getRed()), 2, '0')
26+
. str_pad(dechex($rgba->getGreen()), 2, '0')
27+
. str_pad(dechex($rgba->getBlue()), 2, '0');
28+
} else {
29+
return '#'
30+
. str_pad(dechex($rgba->getRed()), 2, '0')
31+
. str_pad(dechex($rgba->getGreen()), 2, '0')
32+
. str_pad(dechex($rgba->getBlue()), 2, '0')
33+
. str_pad(dechex($rgba->getAlpha()), 2, '0');
34+
}
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function getHslaString(): string
41+
{
42+
$hslaColor = $this->asHsla();
43+
if ($hslaColor->getAlpha() == 255) {
44+
return sprintf('hsl(%s, %s%%, %s%%)', $hslaColor->getHue(), $hslaColor->getSaturation(), $hslaColor->getLightness());
45+
} else {
46+
return sprintf('hsla(%s, %s%%, %s%%, %s)', $hslaColor->getHue(), $hslaColor->getSaturation(), $hslaColor->getLightness(), round($hslaColor->getAlpha() / 255, 2));
47+
}
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getRgbaString(): string
54+
{
55+
$rgbColor = $this->asRgba();
56+
if ($rgbColor->getAlpha() == 255) {
57+
return sprintf('rgb(%s, %s, %s)', $rgbColor->getRed(), $rgbColor->getGreen(), $rgbColor->getBlue());
58+
} else {
59+
return sprintf('rgba(%s, %s, %s, %s)', $rgbColor->getRed(), $rgbColor->getGreen(), $rgbColor->getBlue(), $rgbColor->getAlpha());
60+
}
61+
}
62+
63+
/**
64+
* @param ColorInterface $color
65+
* @return bool
66+
*/
67+
public function equals(ColorInterface $color): bool
68+
{
69+
return $this->getHexString() == $color->getHexString();
70+
}
71+
72+
/**
73+
* @param ColorInterface $color
74+
* @param int $weight
75+
* @return RgbaColor
76+
*/
77+
public function withMixedColor (ColorInterface $color, int $weight = 50): ColorInterface
78+
{
79+
if ($weight < 0 || $weight > 100 ) {
80+
throw new \InvalidArgumentException('argument weight has to be an integer between 0 and 100, ' . $weight . ' was given.' );
81+
}
82+
83+
$factorA = $weight / 100;
84+
$factorB = 1 - $factorA;
85+
86+
$rgbaColorA = $this->asRgba();
87+
$rgbaColorB = $color->asRgba();
88+
89+
return new RgbaColor(
90+
(int)round(($rgbaColorA->getRed() * $factorA) + ($rgbaColorB->getRed() * $factorB)),
91+
(int)round(($rgbaColorA->getGreen() * $factorA) + ($rgbaColorB->getGreen() * $factorB)),
92+
(int)round(($rgbaColorA->getBlue() * $factorA) + ($rgbaColorB->getBlue() * $factorB)),
93+
(int)round(($rgbaColorA->getAlpha() * $factorA) + ($rgbaColorB->getAlpha() * $factorB))
94+
);
95+
}
96+
97+
/**
98+
* @param int $delta
99+
* @return ColorInterface
100+
*/
101+
public function withAdjustedLightness(int $delta): ColorInterface
102+
{
103+
$hslaColor = $this->asHsla();
104+
$lightness = $hslaColor->getLightness() + $delta;
105+
if ($lightness < 0) $lightness = 0;
106+
if ($lightness > 100) $lightness = 100;
107+
108+
return new HslaColor(
109+
$hslaColor->getHue(),
110+
$hslaColor->getSaturation(),
111+
$lightness,
112+
$hslaColor->getAlpha()
113+
);
114+
}
115+
116+
/**
117+
* @param int $delta
118+
* @return ColorInterface
119+
*/
120+
public function withAdjustedSaturation(int $delta): ColorInterface
121+
{
122+
$hslaColor = $this->asHsla();
123+
$saturation = $hslaColor->getSaturation() + $delta;
124+
if ($saturation < 0) $saturation = 0;
125+
if ($saturation > 100) $saturation = 100;
126+
127+
return new HslaColor(
128+
$hslaColor->getHue(),
129+
$saturation,
130+
$hslaColor->getLightness(),
131+
$hslaColor->getAlpha()
132+
);
133+
}
134+
135+
/**
136+
* @param int $delta
137+
* @return ColorInterface
138+
*/
139+
public function withAdjustedHue(int $delta): ColorInterface
140+
{
141+
$hslaColor = $this->asHsla();
142+
$hue = ($hslaColor->getHue() + $delta ) % 360;
143+
if ($hue < 0) $hue += 360;
144+
145+
return new HslaColor(
146+
$hue,
147+
$hslaColor->getSaturation(),
148+
$hslaColor->getLightness(),
149+
$hslaColor->getAlpha()
150+
);
151+
}
152+
}

Classes/Domain/ValueObject/ColorInterface.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace PackageFactory\ColorHelper\Domain\ValueObject;
35

46
interface ColorInterface {
@@ -12,4 +14,61 @@ public function asRgba(): RgbaColor;
1214
* @return HslaColor
1315
*/
1416
public function asHsla(): HslaColor;
17+
18+
/**
19+
* @param ColorInterface $color
20+
* @return bool
21+
*/
22+
public function equals(ColorInterface $color): bool;
23+
24+
/**
25+
* @return string
26+
*/
27+
public function __toString(): string;
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getHexString(): string;
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getHslaString(): string;
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getRgbaString(): string;
43+
44+
/**
45+
* @param ColorInterface $color
46+
* @param int $weight 0 ... 100
47+
* @return ColorInterface
48+
*/
49+
public function withMixedColor(ColorInterface $color, int $weight): ColorInterface;
50+
51+
/**
52+
* @param int $delta
53+
* @return ColorInterface
54+
*/
55+
public function withAdjustedLightness(int $delta): ColorInterface;
56+
57+
/**
58+
* @param int $delta
59+
* @return ColorInterface
60+
*/
61+
public function withAdjustedSaturation(int $delta): ColorInterface;
62+
63+
/**
64+
* @param int $delta
65+
* @return ColorInterface
66+
*/
67+
public function withAdjustedHue(int $delta): ColorInterface;
68+
69+
/**
70+
* @param int $delta
71+
* @return ColorInterface
72+
*/
73+
public function withAdjustedAlpha(int $delta): ColorInterface;
1574
}

0 commit comments

Comments
 (0)