Skip to content

Commit 9508ead

Browse files
committed
Reapply "Allow alpha in RGBA strings"
This reverts commit 4360517.
1 parent 3ee3d42 commit 9508ead

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

webapp/src/Utils/Utils.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,15 @@ public static function componentToHex(int $component): string
371371
/**
372372
* Convert an RGB triple into a CSS hex color.
373373
*
374-
* @param array{int, int, int} $color
374+
* @param array{int, int, int}|array{int, int, int, int} $color
375375
*/
376376
public static function rgbToHex(array $color): string
377377
{
378-
return "#" . static::componentToHex($color[0]) . static::componentToHex($color[1]) . static::componentToHex($color[2]);
378+
$result = '#';
379+
for (int i = 0; i<count($color); i++) {
380+
$result += static::componentToHex($color[$i]);
381+
}
382+
return $result;
379383
}
380384

381385
public static function relativeLuminance(string $rgb): float

webapp/tests/Unit/Utils/UtilsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ public function testRgbToHex(): void
341341
self::assertEquals('#000000', Utils::rgbToHex([0, 0, 0]));
342342
self::assertEquals('#abcdef', Utils::rgbToHex([171, 205, 239]));
343343
self::assertEquals('#fedcba', Utils::rgbToHex([254, 220, 186]));
344+
self::assertEquals('#fedcbaff', Utils::rgbToHex([254, 220, 186, 255]));
345+
self::assertEquals('#fedcba00', Utils::rgbToHex([254, 220, 186, 0]));
344346
}
345347

346348
public function testRelativeLuminance(): void

0 commit comments

Comments
 (0)