Skip to content

Commit 57c8de9

Browse files
authored
Merge pull request #1 from PackageFactory/reafactorToColorValueObjects
TASK: Refactor to colorValueObjects
2 parents 8222bfa + 3b88118 commit 57c8de9

19 files changed

Lines changed: 937 additions & 1080 deletions

.styleci.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
preset: psr2
2+
3+
finder:
4+
path:
5+
- "Classes"
6+
- "Tests"
7+
8+
enabled:
9+
- short_array_syntax
10+
- no_unused_imports

.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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
"phpstan/phpstan": "~0.12.0",
20+
"symfony/css-selector": "~2.0",
21+
"neos/behat": "@dev"
22+
},
23+
"suggest": {
24+
"ext-pdo_sqlite": "For running functional tests out-of-the-box this is required"
25+
},
26+
"scripts": {
27+
"post-update-cmd": "Neos\\Flow\\Composer\\InstallerScripts::postUpdateAndInstall",
28+
"post-install-cmd": "Neos\\Flow\\Composer\\InstallerScripts::postUpdateAndInstall",
29+
"post-package-update": "Neos\\Flow\\Composer\\InstallerScripts::postPackageUpdateAndInstall",
30+
"post-package-install": "Neos\\Flow\\Composer\\InstallerScripts::postPackageUpdateAndInstall"
31+
}
32+
}

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: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PackageFactory\ColorHelper\Domain\ValueObject;
6+
7+
abstract class AbstractColor implements ColorInterface
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+
*
66+
* @return bool
67+
*/
68+
public function equals(ColorInterface $color): bool
69+
{
70+
return $this->getHexString() == $color->getHexString();
71+
}
72+
73+
/**
74+
* @param ColorInterface $color
75+
* @param int $weight
76+
*
77+
* @return RgbaColor
78+
*/
79+
public function withMixedColor(ColorInterface $color, int $weight = 50): ColorInterface
80+
{
81+
if ($weight < 0 || $weight > 100) {
82+
throw new \InvalidArgumentException('argument weight has to be an integer between 0 and 100, '.$weight.' was given.');
83+
}
84+
85+
$factorA = $weight / 100;
86+
$factorB = 1 - $factorA;
87+
88+
$rgbaColorA = $this->asRgba();
89+
$rgbaColorB = $color->asRgba();
90+
91+
return new RgbaColor(
92+
(int) round(($rgbaColorA->getRed() * $factorA) + ($rgbaColorB->getRed() * $factorB)),
93+
(int) round(($rgbaColorA->getGreen() * $factorA) + ($rgbaColorB->getGreen() * $factorB)),
94+
(int) round(($rgbaColorA->getBlue() * $factorA) + ($rgbaColorB->getBlue() * $factorB)),
95+
(int) round(($rgbaColorA->getAlpha() * $factorA) + ($rgbaColorB->getAlpha() * $factorB))
96+
);
97+
}
98+
99+
/**
100+
* @param int $delta
101+
*
102+
* @return ColorInterface
103+
*/
104+
public function withAdjustedLightness(int $delta): ColorInterface
105+
{
106+
$hslaColor = $this->asHsla();
107+
$lightness = $hslaColor->getLightness() + $delta;
108+
if ($lightness < 0) {
109+
$lightness = 0;
110+
}
111+
if ($lightness > 100) {
112+
$lightness = 100;
113+
}
114+
115+
return new HslaColor(
116+
$hslaColor->getHue(),
117+
$hslaColor->getSaturation(),
118+
$lightness,
119+
$hslaColor->getAlpha()
120+
);
121+
}
122+
123+
/**
124+
* @param int $delta
125+
*
126+
* @return ColorInterface
127+
*/
128+
public function withAdjustedSaturation(int $delta): ColorInterface
129+
{
130+
$hslaColor = $this->asHsla();
131+
$saturation = $hslaColor->getSaturation() + $delta;
132+
if ($saturation < 0) {
133+
$saturation = 0;
134+
}
135+
if ($saturation > 100) {
136+
$saturation = 100;
137+
}
138+
139+
return new HslaColor(
140+
$hslaColor->getHue(),
141+
$saturation,
142+
$hslaColor->getLightness(),
143+
$hslaColor->getAlpha()
144+
);
145+
}
146+
147+
/**
148+
* @param int $delta
149+
*
150+
* @return ColorInterface
151+
*/
152+
public function withAdjustedHue(int $delta): ColorInterface
153+
{
154+
$hslaColor = $this->asHsla();
155+
$hue = ($hslaColor->getHue() + $delta) % 360;
156+
if ($hue < 0) {
157+
$hue += 360;
158+
}
159+
160+
return new HslaColor(
161+
$hue,
162+
$hslaColor->getSaturation(),
163+
$hslaColor->getLightness(),
164+
$hslaColor->getAlpha()
165+
);
166+
}
167+
}

0 commit comments

Comments
 (0)