add converter

This commit is contained in:
Simon Vieille 2023-05-20 20:13:35 +02:00
parent 14f30cbed9
commit 37c13e3a13
Signed by: deblan
GPG key ID: 579388D585F70417
3 changed files with 162 additions and 0 deletions

View file

@ -0,0 +1,5 @@
{
"require": {
"spatie/color": "^1.5"
}
}

78
color-convert/composer.lock generated Normal file
View file

@ -0,0 +1,78 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "603b92d7e2cc25b708dbb66ae8ecabd8",
"packages": [
{
"name": "spatie/color",
"version": "1.5.3",
"source": {
"type": "git",
"url": "https://github.com/spatie/color.git",
"reference": "49739265900cabce4640cd26c3266fd8d2cca390"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/color/zipball/49739265900cabce4640cd26c3266fd8d2cca390",
"reference": "49739265900cabce4640cd26c3266fd8d2cca390",
"shasum": ""
},
"require": {
"php": "^7.3|^8.0"
},
"require-dev": {
"pestphp/pest": "^1.22",
"phpunit/phpunit": "^6.5||^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\Color\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Sebastian De Deyne",
"email": "sebastian@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "A little library to handle color conversions",
"homepage": "https://github.com/spatie/color",
"keywords": [
"color",
"conversion",
"rgb",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/color/issues",
"source": "https://github.com/spatie/color/tree/1.5.3"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2022-12-18T12:58:32+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

79
color-convert/replacer Executable file
View file

@ -0,0 +1,79 @@
#!/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);