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

View File

@ -3,19 +3,51 @@
declare(strict_types=1);
return [
"#475569",
"#EA580C",
"#EAB308",
"#CA8A04",
"#84CC16",
"#4D7C0F",
"#10B981",
"#06B6D4",
"#4338CA",
"#A855F7",
"#86198F",
"#FB7185",
"#EC4899",
"#9D174D",
"#F43F5E",
'#fda4af',
'#f43f5e',
'#e11d48',
'#f9a8d4',
'#ec4899',
'#db2777',
'#f0abfc',
'#d946ef',
'#c026d3',
'#d8b4fe',
'#a855f7',
'#9333ea',
'#c4b5fd',
'#8b5cf6',
'#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',
];