diff --git a/lib/index b/lib/index index a0b3a08..66a35f0 160000 --- a/lib/index +++ b/lib/index @@ -1 +1 @@ -Subproject commit a0b3a08d7f8324ea2ec4e920c38b00646e0a9a6f +Subproject commit 66a35f030f9eec02d8e51710c7de2161d2a1796f diff --git a/public/manage/users/role.php b/public/manage/users/role.php index b384432..dce7841 100644 --- a/public/manage/users/role.php +++ b/public/manage/users/role.php @@ -1,6 +1,8 @@ getMessage(); - } + if(!empty($_POST['colour']['enable'])) { + $setColour = \Index\Colour\Colour::parse((string)($_POST['colour']['hex'] ?? '')); + if($setColour->shouldInherit()) + $notices[] = 'Invalid colour specified.'; + } if(empty($notices)) $userInfo->setColour($setColour); diff --git a/src/Colour.php b/src/Colour.php deleted file mode 100644 index bd4285c..0000000 --- a/src/Colour.php +++ /dev/null @@ -1,96 +0,0 @@ -raw = ($raw ?? 0) & 0x7FFFFFFF; - } - - public static function none(): self { - return new Colour(self::FLAG_INHERIT); - } - - public static function fromRgb(int $red, int $green, int $blue): self { - $raw = (($red & 0xFF) << 16) - | (($green & 0xFF) << 8) - | ($blue & 0xFF); - return new Colour($raw); - } - public static function fromHex(string $hex): self { - if($hex[0] === '#') - $hex = mb_substr($hex, 1); - - if(!ctype_xdigit($hex)) - throw new InvalidArgumentException('Argument contains invalid characters.'); - - $length = mb_strlen($hex); - - if($length === 3) { - $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; - } elseif($length !== 6) { - throw new InvalidArgumentException('Argument is not a hex string.'); - } - - return new Colour(hexdec($hex)); - } - - public function getRaw(): int { - return $this->raw; - } - - public function getInherit(): bool { - return ($this->getRaw() & self::FLAG_INHERIT) > 0; - } - - public function getRed(): int { - return ($this->getRaw() & 0xFF0000) >> 16; - } - - public function getGreen(): int { - return ($this->getRaw() & 0xFF00) >> 8; - } - - public function getBlue(): int { - return ($this->getRaw() & 0xFF); - } - - public function getLuminance(): float { - return self::LUMINANCE_WEIGHT_RED * $this->getRed() - + self::LUMINANCE_WEIGHT_GREEN * $this->getGreen() - + self::LUMINANCE_WEIGHT_BLUE * $this->getBlue(); - } - - public function getHex(): string { - return str_pad(dechex($this->getRaw() & 0xFFFFFF), 6, '0', STR_PAD_LEFT); - } - - public function getCSS(): string { - if($this->getInherit()) - return 'inherit'; - return '#' . $this->getHex(); - } - - public function extractCSSContract( - string $dark = 'dark', string $light = 'light', bool $inheritIsDark = true - ): string { - if($this->getInherit()) - return $inheritIsDark ? $dark : $light; - - return $this->getLuminance() > self::READABILITY_THRESHOLD ? $dark : $light; - } - - public function __toString() { - return $this->getCSS(); - } -} diff --git a/src/SharpChat/SharpChatRoutes.php b/src/SharpChat/SharpChatRoutes.php index 7fe8413..9f233ad 100644 --- a/src/SharpChat/SharpChatRoutes.php +++ b/src/SharpChat/SharpChatRoutes.php @@ -1,6 +1,7 @@ hasParam('legacy') ? 'chatPath.legacy' : 'chatPath.normal'; $chatPath = $this->config->getValue($configKey, CfgType::T_STR, '/'); @@ -147,7 +148,7 @@ final class SharpChatRoutes { return [ 'user_id' => $userInfo->getId(), 'username' => $userInfo->getUsername(), - 'colour_raw' => $userInfo->getColour()->getRaw(), + 'colour_raw' => Colour::toMisuzu($userInfo->getColour()), 'rank' => $rank = $userInfo->getRank(), 'perms' => SharpChatPerms::convert($userInfo), ]; @@ -238,7 +239,7 @@ final class SharpChatRoutes { 'success' => true, 'user_id' => $userInfo->getId(), 'username' => $userInfo->getUsername(), - 'colour_raw' => $userInfo->getColour()->getRaw(), + 'colour_raw' => Colour::toMisuzu($userInfo->getColour()), 'rank' => $rank = $userInfo->getRank(), 'hierarchy' => $rank, 'is_silenced' => date('c', $userInfo->isSilenced() || $userInfo->isBanned() ? ($userInfo->isActiveWarningPermanent() ? strtotime('10 years') : $userInfo->getActiveWarningExpiration()) : 0), @@ -267,7 +268,7 @@ final class SharpChatRoutes { 'user_id' => $userInfo->getId(), 'id' => $userInfo->getId(), 'username' => $userInfo->getUsername(), - 'colour_raw' => $userInfo->getColour()->getRaw(), + 'colour_raw' => Colour::toMisuzu($userInfo->getColour()), 'rank' => $rank = $userInfo->getRank(), 'ip' => $warning->getUserRemoteAddress(), 'is_permanent' => $isPermanent, diff --git a/src/Users/User.php b/src/Users/User.php index 7348f8d..fb3bd79 100644 --- a/src/Users/User.php +++ b/src/Users/User.php @@ -4,7 +4,7 @@ namespace Misuzu\Users; use DateTime; use DateTimeZone; use JsonSerializable; -use Misuzu\Colour; +use Index\Colour\Colour; use Misuzu\DB; use Misuzu\HasRankInterface; use Misuzu\Memoizer; @@ -140,17 +140,17 @@ class User implements HasRankInterface, JsonSerializable { public function getColour(): Colour { // Swaps role colour in if user has no personal colour if($this->realColour === null) { $this->realColour = $this->getUserColour(); - if($this->realColour->getInherit()) + if($this->realColour->shouldInherit()) $this->realColour = $this->getDisplayRole()->getColour(); } return $this->realColour; } public function setColour(?Colour $colour): self { - return $this->setColourRaw($colour === null ? null : $colour->getRaw()); + return $this->setColourRaw($colour === null ? null : Colour::toMisuzu($colour)); } public function getUserColour(): Colour { // Only ever gets the user's actual colour if($this->userColour === null) - $this->userColour = new Colour($this->getColourRaw()); + $this->userColour = Colour::fromMisuzu($this->getColourRaw()); return $this->userColour; } public function getColourRaw(): int { diff --git a/src/Users/UserInfo.php b/src/Users/UserInfo.php index e5bc47e..bf8a54a 100644 --- a/src/Users/UserInfo.php +++ b/src/Users/UserInfo.php @@ -2,6 +2,7 @@ namespace Misuzu\Users; use Index\DateTime; +use Index\Colour\Colour; abstract class UserInfo { abstract public function getId(): string; @@ -14,15 +15,14 @@ abstract class UserInfo { abstract public function getDisplayRoleId(): string; - // need Index colour type - //abstract public function getColour(): ?int; + abstract public function getColour(): Colour; abstract public function getCreatedDate(): DateTime; abstract public function getLastActiveDate(): DateTime; abstract public function getDeletedDate(): DateTime; public function hasColour(): bool { - return $this->getColour() !== null; + return !$this->getColour()->shouldInherit(); } private static DateTime $epoch; diff --git a/src/Users/UserRole.php b/src/Users/UserRole.php index d0aeac9..875e4a2 100644 --- a/src/Users/UserRole.php +++ b/src/Users/UserRole.php @@ -2,7 +2,7 @@ namespace Misuzu\Users; use ArrayAccess; -use Misuzu\Colour; +use Index\Colour\Colour; use Misuzu\DB; use Misuzu\HasRankInterface; use Misuzu\Memoizer; @@ -98,12 +98,12 @@ class UserRole implements ArrayAccess, HasRankInterface { } public function getColour(): Colour { - if($this->colour === null || ($this->getColourRaw() ?? 0x40000000) !== $this->colour->getRaw()) - $this->colour = new Colour($this->role_colour ?? 0x40000000); + if($this->colour === null || ($this->role_colour ?? 0x40000000) !== Colour::toMisuzu($this->colour)) + $this->colour = Colour::fromMisuzu($this->role_colour ?? 0x40000000); return $this->colour; } public function setColour(Colour $colour): self { - $this->role_colour = $colour->getInherit() ? null : $colour->getRaw(); + $this->role_colour = $colour->shouldInherit() ? null : Colour::toMisuzu($colour); $this->colour = $this->colour; return $this; } diff --git a/templates/manage/users/role.twig b/templates/manage/users/role.twig index b13616b..1f3491e 100644 --- a/templates/manage/users/role.twig +++ b/templates/manage/users/role.twig @@ -46,7 +46,7 @@ diff --git a/templates/manage/users/user.twig b/templates/manage/users/user.twig index 58cbfaa..c9f42e6 100644 --- a/templates/manage/users/user.twig +++ b/templates/manage/users/user.twig @@ -99,11 +99,11 @@ - {{ input_colour(can_edit_user ? 'colour[hex]' : '', '', '#%s'|format(user_info.userColour.hex)) }} + {{ input_colour(can_edit_user ? 'colour[hex]' : '', '', user_info.userColour) }} {# TODO: if the hierarchy of the current user is too low to touch the role then opacity should be lowered and input disabled #} diff --git a/utility.php b/utility.php index b7c011e..afc6dc8 100644 --- a/utility.php +++ b/utility.php @@ -118,27 +118,20 @@ function render_info(?string $message, int $httpCode, string $template = 'errors } function html_colour(?int $colour, $attribs = '--user-colour'): string { - $colour = $colour == null ? \Misuzu\Colour::none() : new \Misuzu\Colour($colour); + $colour = (string)\Index\Colour\Colour::fromMisuzu($colour ?? 0x40000000); - if(is_string($attribs)) { - $attribs = [ - $attribs => '%s', - ]; - } + if(is_string($attribs)) + $attribs = [ $attribs => '%s' ]; - if(!$attribs) { + if(!$attribs) $attribs = [ 'color' => '%s', '--user-colour' => '%s', ]; - } $css = ''; - $value = $colour->getCSS(); - - foreach($attribs as $name => $format) { - $css .= $name . ':' . sprintf($format, $value) . ';'; - } + foreach($attribs as $name => $format) + $css .= $name . ':' . sprintf($format, $colour) . ';'; return $css; }