#75 - new colors

This commit is contained in:
Adrian Hopek 2022-03-16 09:42:17 +01:00
parent 894a1468b2
commit 7261c82c57
3 changed files with 82 additions and 18 deletions

View 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(($hash << 2) - $hash) + mb_ord($text[$i]);
}
return $hash;
}
}

View File

@ -16,6 +16,7 @@ use Illuminate\Support\Collection;
use Rackbeat\UIAvatars\HasAvatar; use Rackbeat\UIAvatars\HasAvatar;
use Toby\Domain\Enums\EmploymentForm; use Toby\Domain\Enums\EmploymentForm;
use Toby\Domain\Enums\Role; use Toby\Domain\Enums\Role;
use Toby\Eloquent\Helpers\ColorGenerator;
/** /**
* @property int $id * @property int $id
@ -84,10 +85,8 @@ class User extends Authenticatable
public function getAvatar(): string public function getAvatar(): string
{ {
$colors = config("colors");
return $this->getAvatarGenerator() return $this->getAvatarGenerator()
->backgroundColor($colors[strlen($this->fullname) % count($colors)]) ->backgroundColor(ColorGenerator::generate($this->fullName))
->image(); ->image();
} }

View File

@ -3,19 +3,51 @@
declare(strict_types=1); declare(strict_types=1);
return [ return [
"#475569", '#fda4af',
"#EA580C", '#f43f5e',
"#EAB308", '#e11d48',
"#CA8A04", '#f9a8d4',
"#84CC16", '#ec4899',
"#4D7C0F", '#db2777',
"#10B981", '#f0abfc',
"#06B6D4", '#d946ef',
"#4338CA", '#c026d3',
"#A855F7", '#d8b4fe',
"#86198F", '#a855f7',
"#FB7185", '#9333ea',
"#EC4899", '#c4b5fd',
"#9D174D", '#8b5cf6',
"#F43F5E", '#7c3aed',
'#a5b4fc',
'#6366f1',
'#4f46e5',
'#93c5fd',
'#3b82f6',
'#2563eb',
'#7dd3fc',
'#0ea5e9',
'#0284c7',
'#67e8f9',
'#06b6d4',
'#0891b2',
'#5eead4',
'#14b8a6',
'#0d9488',
'#86efac',
'#22c55e',
'#16a34a',
'#bef264',
'#84cc16',
'#65a30d',
'#fde047',
'#eab308',
'#ca8a04',
'#fdba74',
'#f97316',
'#ea580c',
'#fca5a5',
'#ef4444',
'#dc2626',
'#4b5563',
'#64748b',
]; ];