gitnet-assets/color-convert/replacer
2023-05-20 20:13:35 +02:00

80 lines
1.8 KiB
Plaintext
Executable file

#!/usr/bin/php8.2
<?php
use Spatie\Color\Hex;
require __DIR__.'/vendor/autoload.php';
$input = $argv[1] ?? null;
$name = $argv[2] ?? null;
$nextPrimaryColor = $argv[3] ?? null;
if (empty($input) || !file_exists($input)) {
exit(1);
}
if (empty($name)) {
exit(1);
}
if (empty($nextPrimaryColor)) {
exit(1);
}
$prefixes = [
'color-primary',
'color-primary-dark',
'color-primary-light',
'color-primary-alpha',
];
$properties = [];
foreach ($prefixes as $prefix) {
$properties[] = sprintf('--%s', $prefix);
for ($u = 1; $u < 100; ++$u) {
$properties[] = sprintf('--%s-%d', $prefix, $u);
}
}
$content = file_get_contents($input);
$currentPrimaryColor = '#fb923c';
$currentPrimaryColorH = (int) preg_replace('/hsl\(([0-9+]+),.+$/', '$1', Hex::fromString($currentPrimaryColor)->toHsl());
$nextPrimaryColorH = (int) preg_replace('/hsl\(([0-9]+),.+$/', '$1', Hex::fromString($nextPrimaryColor)->toHsl());
$hDiff = $nextPrimaryColorH - $currentPrimaryColorH;
foreach ($properties as $property) {
preg_match_all(
sprintf(
'/%s: *((?<color>#[A-F0-9]{6})(?<alpha>[A-F0-9]{2}){0,1});/is',
$property,
),
$content,
$matches,
PREG_SET_ORDER
);
foreach ($matches as $match) {
$match['alpha'] ??= 'ff';
$color = Hex::fromString($match['color']);
$hsl = $color->toHsla(hexdec($match['alpha']) * 1 / 255);
$hsl = preg_replace_callback('/\(([0-9]+),/', function ($m) use ($hDiff) {
return sprintf('(%d,', $m[1] + $hDiff);
}, $hsl);
$content = str_replace(
$match[0],
sprintf('%s: %s;', $property, $hsl),
$content
);
}
}
file_put_contents(str_replace('.css', '-'.$name.'.css', $input), $content);