|
|
<?php |
|
|
|
|
|
namespace Kanboard\User\Avatar; |
|
|
|
|
|
use Kanboard\Core\Base; |
|
|
use Kanboard\Core\User\Avatar\AvatarProviderInterface; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LetterAvatarProvider extends Base implements AvatarProviderInterface |
|
|
{ |
|
|
protected $lightness = array(0.35, 0.5, 0.65); |
|
|
protected $saturation = array(0.35, 0.5, 0.65); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function render(array $user, $size) |
|
|
{ |
|
|
$initials = $this->helper->user->getInitials($user['name'] ?: $user['username']); |
|
|
$rgb = $this->getBackgroundColor($user['name'] ?: $user['username']); |
|
|
$name = $this->helper->text->e($user['name'] ?: $user['username']); |
|
|
|
|
|
return sprintf( |
|
|
'<div class="avatar-letter" style="background-color: rgb(%d, %d, %d)" title="%s" role="img" aria-label="%s">%s</div>', |
|
|
$rgb[0], |
|
|
$rgb[1], |
|
|
$rgb[2], |
|
|
$name, |
|
|
$name, |
|
|
$this->helper->text->e($initials) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function isActive(array $user) |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getBackgroundColor($str) |
|
|
{ |
|
|
$hsl = $this->getHSL($str); |
|
|
return $this->getRGB($hsl[0], $hsl[1], $hsl[2]); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function getRGB($hue, $saturation, $lightness) |
|
|
{ |
|
|
$hue /= 360; |
|
|
$q = $lightness < 0.5 ? $lightness * (1 + $saturation) : $lightness + $saturation - $lightness * $saturation; |
|
|
$p = 2 * $lightness - $q; |
|
|
|
|
|
return array_map(function ($color) use ($q, $p) { |
|
|
if ($color < 0) { |
|
|
$color++; |
|
|
} |
|
|
|
|
|
if ($color > 1) { |
|
|
$color--; |
|
|
} |
|
|
|
|
|
if ($color < 1/6) { |
|
|
$color = $p + ($q - $p) * 6 * $color; |
|
|
} else if ($color < 0.5) { |
|
|
$color = $q; |
|
|
} else if ($color < 2/3) { |
|
|
$color = $p + ($q - $p) * 6 * (2/3 - $color); |
|
|
} else { |
|
|
$color = $p; |
|
|
} |
|
|
|
|
|
return round($color * 255); |
|
|
}, array($hue + 1/3, $hue, $hue - 1/3)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function getHSL($str) |
|
|
{ |
|
|
$hash = $this->hash($str); |
|
|
$hue = $hash % 359; |
|
|
|
|
|
$hash = intval($hash / 360); |
|
|
$saturation = $this->saturation[$hash % count($this->saturation)]; |
|
|
|
|
|
$hash = intval($hash / count($this->saturation)); |
|
|
$lightness = $this->lightness[$hash % count($this->lightness)]; |
|
|
|
|
|
return array($hue, $saturation, $lightness); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function hash($str) |
|
|
{ |
|
|
$seed = 131; |
|
|
$seed2 = 137; |
|
|
$hash = 0; |
|
|
|
|
|
|
|
|
$str .= 'x'; |
|
|
$max = intval(PHP_INT_MAX / $seed2); |
|
|
|
|
|
for ($i = 0, $ilen = mb_strlen($str, 'UTF-8'); $i < $ilen; $i++) { |
|
|
if ($hash > $max) { |
|
|
$hash = intval($hash / $seed2); |
|
|
} |
|
|
|
|
|
$hash = $hash * $seed + $this->getCharCode(mb_substr($str, $i, 1, 'UTF-8')); |
|
|
} |
|
|
|
|
|
return $hash; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function getCharCode($c) |
|
|
{ |
|
|
list(, $ord) = unpack('N', mb_convert_encoding($c, 'UCS-4BE', 'UTF-8')); |
|
|
return $ord; |
|
|
} |
|
|
} |
|
|
|