Skip to content

Commit d5c5d38

Browse files
committed
FEATURE: Support alpha values for colors
1 parent 30be0a0 commit d5c5d38

File tree

3 files changed

+179
-46
lines changed

3 files changed

+179
-46
lines changed

Classes/Eel/Color.php

Lines changed: 146 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ class Color implements ProtectedContextAwareInterface
1010
/**
1111
* Default adjustments for color manipulations
1212
*/
13-
const DEFAULT_ADJUST = 10;
13+
const DEFAULT_ADJUSTMENT = 10;
1414

15-
private $red;
16-
private $green;
17-
private $blue;
15+
private $red = 0;
16+
private $green = 0;
17+
private $blue = 0;
1818

19-
private $hue;
20-
private $saturation;
21-
private $lightness;
19+
private $hue = 0;
20+
private $saturation = 0;
21+
private $lightness = 0;
22+
23+
private $alpha = 1;
2224

2325
/**
2426
* @param int $hue
2527
* @param int $saturation
2628
* @param int $lightness
29+
* @param float $alpha
2730
* @return Color
2831
* @throws \Exception
2932
*/
30-
public static function createFromHSL(int $hue, int $saturation, int $lightness): self
33+
public static function createFromHSL(int $hue, int $saturation, int $lightness, float $alpha = 1): self
3134
{
3235
$hue = $hue % 360;
3336
if ($hue < 0) {
@@ -36,6 +39,7 @@ public static function createFromHSL(int $hue, int $saturation, int $lightness):
3639

3740
$saturation = self::limitNumber($saturation, 0, 100);
3841
$lightness = self::limitNumber($lightness, 0, 100);
42+
$alpha = self::limitNumber($alpha, 0, 1);
3943

4044
$hsl = [
4145
'H' => $hue,
@@ -52,22 +56,24 @@ public static function createFromHSL(int $hue, int $saturation, int $lightness):
5256
$color->hue = $hue;
5357
$color->saturation = $saturation;
5458
$color->lightness = $lightness;
55-
59+
$color->alpha = $alpha;
5660
return $color;
5761
}
5862

5963
/**
6064
* @param int $red
6165
* @param int $green
6266
* @param int $blue
67+
* @param float $alpha
6368
* @return Color
6469
* @throws \Exception
6570
*/
66-
public static function createFromRgb(int $red, int $green, int $blue): self
71+
public static function createFromRgb(int $red, int $green, int $blue, float $alpha = 1): self
6772
{
6873
$red = self::limitNumber($red, 0, 255);
6974
$green = self::limitNumber($green, 0, 255);
7075
$blue = self::limitNumber($blue, 0, 255);
76+
$alpha = self::limitNumber($alpha, 0, 1);
7177

7278
$rgb = [
7379
'R' => $red,
@@ -84,6 +90,7 @@ public static function createFromRgb(int $red, int $green, int $blue): self
8490
$color->hue = round($hsl['H']);
8591
$color->saturation = round($hsl['S'] * 100);
8692
$color->lightness = round($hsl['L'] * 100);
93+
$color->alpha = $alpha;
8794

8895
return $color;
8996
}
@@ -108,81 +115,149 @@ public static function createFromHex(string $hex): self
108115
return $color;
109116
}
110117

111-
public function __toString()
118+
/**
119+
* @return int
120+
*/
121+
public function getRed(): int
112122
{
113-
return '#' . ColorLibrary::rgbToHex([
114-
'R' => $this->red,
115-
'G' => $this->green,
116-
'B' => $this->blue
117-
]);
123+
return $this->red;
118124
}
119125

120-
public function getRgb(): array
126+
/**
127+
* @return int
128+
*/
129+
public function getGreen(): int
121130
{
122-
return [$this->red, $this->green, $this->blue];
131+
return $this->green;
123132
}
124133

125-
public function getHsl(): array
134+
/**
135+
* @return int
136+
*/
137+
public function getBlue(): int
126138
{
127-
return [$this->hue, $this->saturation, $this->lightness];
139+
return $this->blue;
128140
}
129141

130-
public function getHex(): array
142+
/**
143+
* @return int
144+
*/
145+
public function getHue(): int
131146
{
132-
return $this->__toString();
147+
return $this->hue;
133148
}
134149

135150
/**
136-
* @param Color $colorA
137-
* @param Color $colorB
151+
* @return int
152+
*/
153+
public function getSaturation(): int
154+
{
155+
return $this->saturation;
156+
}
157+
158+
/**
159+
* @return int
160+
*/
161+
public function getLightness(): int
162+
{
163+
return $this->lightness;
164+
}
165+
166+
/**
167+
* @return int
168+
*/
169+
public function getAlpha(): float
170+
{
171+
return $this->alpha;
172+
}
173+
174+
public function __toString(): string
175+
{
176+
if ($this->alpha == 1) {
177+
return $this->hex();
178+
} else {
179+
return $this->rgb();
180+
}
181+
}
182+
183+
public function rgb(): string
184+
{
185+
if ($this->alpha == 1) {
186+
return sprintf('rgb( %s, %s, %s)', $this->red, $this->green, $this->blue);
187+
} else {
188+
return sprintf('rgba( %s, %s, %s, %s)', $this->red, $this->green, $this->blue, $this->alpha);
189+
}
190+
}
191+
192+
public function hsl(): string
193+
{
194+
if ($this->alpha == 1) {
195+
return sprintf('hsl( %s, %s%%, %s%%)', $this->hue, $this->saturation, $this->lightness);
196+
} else {
197+
return sprintf('hsla( %s, %s%%, %s%%, %s)', $this->hue, $this->saturation, $this->lightness, $this->alpha);
198+
}
199+
}
200+
201+
public function hex(): string
202+
{
203+
return '#' . ColorLibrary::rgbToHex([
204+
'R' => $this->red,
205+
'G' => $this->green,
206+
'B' => $this->blue
207+
]);
208+
}
209+
210+
/**
211+
* @param Color $color
138212
* @param int $weight between 0 and 100
139213
* @return string
140214
*/
141-
public function mix(Color $color, int $weight = 50): Color
215+
public function mix(Color $color, int $weight = 50): self
142216
{
143-
list($red, $green, $blue) = $color->getRgb();
144217
$weight = $this->limitNumber($weight, 0, 100);
145218
$factorA = $weight / 100;
146219
$factorB = 1 - $factorA;
147220

148221
return self::createFromRgb(
149-
round(($this->red * $factorA) + ($red * $factorB)),
150-
round(($this->green * $factorA) + ($green * $factorB)),
151-
round(($this->blue * $factorA) + ($blue * $factorB)),
222+
round(($this->red * $factorA) + ($color->getRed() * $factorB)),
223+
round(($this->green * $factorA) + ($color->getGreen() * $factorB)),
224+
round(($this->blue * $factorA) + ($color->getBlue() * $factorB)),
225+
round(($this->alpha * $factorA) + ($color->getAlpha() * $factorB))
152226
);
153227
}
154228

155229
/**
156230
* @param int $amount between 0 and 100
157231
* @return string
158232
*/
159-
public function lighten(int $amount = self::DEFAULT_ADJUST ): self
233+
public function lighten(int $amount = self::DEFAULT_ADJUSTMENT ): self
160234
{
161235
return self::createFromHSL(
162236
$this->hue,
163237
$this->saturation,
164-
$this->lightness + $amount
238+
$this->lightness + $amount,
239+
$this->alpha
165240
);
166241
}
167242

168243
/**
169244
* @param int $amount between 0 and 100
170245
* @return string
171246
*/
172-
public function darken(int $amount = self::DEFAULT_ADJUST ): self
247+
public function darken(int $amount = self::DEFAULT_ADJUSTMENT ): self
173248
{
174249
return self::createFromHSL(
175250
$this->hue,
176251
$this->saturation,
177-
$this->lightness - $amount
252+
$this->lightness - $amount,
253+
$this->alpha
178254
);
179255
}
180256

181257

182258
/**
183259
* Adjust the value by rotating the hue angle of a color in either direction.
184260
*
185-
* @param string $color
186261
* @param int $amount degrees to rotate the color
187262
* @return string
188263
*/
@@ -191,34 +266,65 @@ public function spin(int $amount): self
191266
return self::createFromHSL(
192267
$this->hue + $amount,
193268
$this->saturation,
194-
$this->lightness
269+
$this->lightness,
270+
$this->alpha
195271
);
196272
}
197273

198274
/**
199275
* @param int $amount to saturate the color
200276
* @return string
201277
*/
202-
public function saturate(int $amount = self::DEFAULT_ADJUST): self
278+
public function saturate(int $amount = self::DEFAULT_ADJUSTMENT): self
203279
{
204280
return self::createFromHSL(
205281
$this->hue,
206282
self::limitNumber($this->saturation + $amount, 0, 100),
207-
$this->lightness
283+
$this->lightness,
284+
$this->alpha
208285
);
209286
}
210287

211288
/**
212-
* @param string $color
213289
* @param int $amount to desaturate the color
214290
* @return string
215291
*/
216-
public function desaturate(int $amount = self::DEFAULT_ADJUST): self
292+
public function desaturate(int $amount = self::DEFAULT_ADJUSTMENT): self
217293
{
218294
return self::createFromHSL(
219295
$this->hue,
220296
self::limitNumber($this->saturation - $amount, 0, 100),
221-
$this->lightness
297+
$this->lightness,
298+
$this->alpha
299+
);
300+
}
301+
302+
/**
303+
* @param int $amount to desaturate the color
304+
* @return string
305+
*/
306+
public function fadein(int $amount = self::DEFAULT_ADJUSTMENT): self
307+
{
308+
return self::createFromRgb(
309+
$this->red,
310+
$this->green,
311+
$this->blue,
312+
$this->alpha + ($amount / 100)
313+
);
314+
}
315+
316+
317+
/**
318+
* @param int $amount to desaturate the color
319+
* @return string
320+
*/
321+
public function fadeout(int $amount = self::DEFAULT_ADJUSTMENT): self
322+
{
323+
return self::createFromRgb(
324+
$this->red,
325+
$this->green,
326+
$this->blue,
327+
$this->alpha - ($amount / 100)
222328
);
223329
}
224330

Classes/Eel/ColorHelper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ class ColorHelper implements ProtectedContextAwareInterface
1010
* @param $red
1111
* @param $green
1212
* @param $blue
13+
* @param $alpha
1314
* @return Color
1415
*/
15-
public function rgb($red, $green, $blue): Color
16+
public function rgb($red, $green, $blue, $alpha = 1): Color
1617
{
17-
return Color::createFromRgb($red, $green, $blue);
18+
return Color::createFromRgb($red, $green, $blue, $alpha);
1819
}
1920

2021
/**
2122
* @param $hue
2223
* @param $saturatiom
2324
* @param $lightness
25+
* @param $alpha
2426
* @return Color
2527
*/
26-
public function hsl($hue, $saturatiom, $lightness): Color
28+
public function hsl($hue, $saturatiom, $lightness, $alpha = 1): Color
2729
{
28-
return Color::createFromHSL($hue, $saturatiom, $lightness);
30+
return Color::createFromHSL($hue, $saturatiom, $lightness, $alpha);
2931
}
3032

3133
/**

0 commit comments

Comments
 (0)