#!/usr/bin/php8.2 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: *((?#[A-F0-9]{6})(?[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);