39 lines
883 B
PHP
39 lines
883 B
PHP
<?php
|
|
|
|
namespace App\Twig\Extension;
|
|
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFunction;
|
|
|
|
class ColorExtension extends AbstractExtension
|
|
{
|
|
public function getFunctions(): array
|
|
{
|
|
return [
|
|
new TwigFunction('generate_color_by_string', [$this, 'generateColorByString']),
|
|
];
|
|
}
|
|
|
|
public function generateColorByString(string $value)
|
|
{
|
|
srand(crc32($value));
|
|
|
|
$colors = [];
|
|
|
|
for ($i = 0; $i < 3; ++$i) {
|
|
$colors[] = mt_rand(20 * ($i + 1), 50 * ($i + 1));
|
|
}
|
|
|
|
$red = dechex($colors[0]);
|
|
$green = dechex($colors[1]);
|
|
$blue = dechex($colors[2]);
|
|
|
|
return sprintf(
|
|
'#%s%s%s',
|
|
strlen($red) != 2 ? '0'.$red : $red,
|
|
strlen($green) != 2 ? '0'.$green : $green,
|
|
strlen($blue) != 2 ? '0'.$blue : $blue,
|
|
);
|
|
}
|
|
}
|