feat(support): add MacroColorConverter utility

This commit is contained in:
Thorsten Bus 2026-05-03 22:31:44 +02:00
parent 846bd12f90
commit a1612dc3ef
2 changed files with 47 additions and 0 deletions

View file

@ -0,0 +1,20 @@
<?php
namespace App\Support;
final class MacroColorConverter
{
public static function fromRgba(?array $rgba): ?string
{
if ($rgba === null) {
return null;
}
return sprintf(
'#%02X%02X%02X',
(int) round(max(0.0, min(1.0, $rgba['r'])) * 255),
(int) round(max(0.0, min(1.0, $rgba['g'])) * 255),
(int) round(max(0.0, min(1.0, $rgba['b'])) * 255),
);
}
}

View file

@ -0,0 +1,27 @@
<?php
use App\Support\MacroColorConverter;
test('converts rgba floats to hex string', function () {
expect(MacroColorConverter::fromRgba(['r' => 1.0, 'g' => 0.0, 'b' => 0.5, 'a' => 1.0]))->toBe('#FF0080');
});
test('converts pure black', function () {
expect(MacroColorConverter::fromRgba(['r' => 0.0, 'g' => 0.0, 'b' => 0.0, 'a' => 1.0]))->toBe('#000000');
});
test('converts pure white', function () {
expect(MacroColorConverter::fromRgba(['r' => 1.0, 'g' => 1.0, 'b' => 1.0, 'a' => 1.0]))->toBe('#FFFFFF');
});
test('converts grey', function () {
expect(MacroColorConverter::fromRgba(['r' => 0.5, 'g' => 0.5, 'b' => 0.5, 'a' => 1.0]))->toBe('#808080');
});
test('clamps out-of-range values', function () {
expect(MacroColorConverter::fromRgba(['r' => 1.5, 'g' => -0.1, 'b' => 0.5, 'a' => 0.0]))->toBe('#FF0080');
});
test('returns null for null input', function () {
expect(MacroColorConverter::fromRgba(null))->toBeNull();
});