* #75 - fix data leak to ui-avatars * #75 - ecs fix * #75 - new colors * #75 - change font color * #75 - ecs fix * #75 - change colors order * #75 - fix * #75 - fix * #75 - ecs fix Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
This commit is contained in:
33
app/Eloquent/Helpers/ColorGenerator.php
Normal file
33
app/Eloquent/Helpers/ColorGenerator.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Helpers;
|
||||
|
||||
class ColorGenerator
|
||||
{
|
||||
public static function generate(string $text): string
|
||||
{
|
||||
$colors = config("colors");
|
||||
$hash = static::calculateHash($text);
|
||||
|
||||
$index = $hash - count($colors) * floor($hash / count($colors));
|
||||
|
||||
return $colors[$index];
|
||||
}
|
||||
|
||||
protected static function calculateHash(string $text): int
|
||||
{
|
||||
$hash = 0;
|
||||
|
||||
if (empty($text)) {
|
||||
return $hash;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < mb_strlen($text); $i++) {
|
||||
$hash = abs((int)(($hash << 2) - $hash) + mb_ord($text[$i]));
|
||||
}
|
||||
|
||||
return $hash;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user