Replaced Misuzu Colour library with the Index ones.

This commit is contained in:
flash 2023-01-02 23:48:04 +00:00
parent f9d2ca2bb5
commit 4f3d0c5246
11 changed files with 36 additions and 136 deletions

@ -1 +1 @@
Subproject commit a0b3a08d7f8324ea2ec4e920c38b00646e0a9a6f
Subproject commit 66a35f030f9eec02d8e51710c7de2161d2a1796f

View File

@ -1,6 +1,8 @@
<?php
namespace Misuzu;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
use Misuzu\Users\User;
use Misuzu\Users\UserRole;
use Misuzu\Users\UserRoleNotFoundException;
@ -53,9 +55,9 @@ if(!empty($_POST['role']) && is_array($_POST['role']) && CSRF::validateRequest()
}
if(!empty($_POST['role']['colour']['inherit'])) {
$roleColour = Colour::none();
$roleColour = \Index\Colour\Colour::none();
} else {
$roleColour = Colour::fromRgb(
$roleColour = new ColourRGB(
(int)($_POST['role']['colour']['red'] ?? -1),
(int)($_POST['role']['colour']['green'] ?? -1),
(int)($_POST['role']['colour']['blue'] ?? -1)

View File

@ -1,6 +1,7 @@
<?php
namespace Misuzu;
use Index\Colour\Colour;
use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException;
use Misuzu\Users\UserRole;
@ -138,12 +139,11 @@ if(CSRF::validateRequest() && $canEdit) {
if(!empty($_POST['colour']) && is_array($_POST['colour'])) {
$setColour = null;
if(!empty($_POST['colour']['enable']))
try {
$setColour = Colour::fromHex((string)($_POST['colour']['hex'] ?? ''));
} catch(\Exception $ex) {
$notices[] = $ex->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);

View File

@ -1,96 +0,0 @@
<?php
namespace Misuzu;
use InvalidArgumentException;
class Colour {
private const FLAG_INHERIT = 0x40000000;
private const READABILITY_THRESHOLD = 186;
private const LUMINANCE_WEIGHT_RED = .299;
private const LUMINANCE_WEIGHT_GREEN = .587;
private const LUMINANCE_WEIGHT_BLUE = .114;
private int $raw = 0;
public function __construct(?int $raw = 0) {
$this->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();
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Misuzu\SharpChat;
use Index\Colour\Colour;
use Index\Http\HttpFx;
use Misuzu\Config\IConfig;
use Misuzu\Config\CfgType;
@ -70,7 +71,7 @@ final class SharpChatRoutes {
return $out;
}
public static function login($response, $request): void {
public function login($response, $request): void {
$currentUser = User::getCurrent();
$configKey = $request->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,

View File

@ -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 {

View File

@ -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;

View File

@ -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;
}

View File

@ -46,7 +46,7 @@
<label class="form__label">
<div class="form__label__text">Inherit Colour</div>
<div class="form__label__input">
{{ input_checkbox('role[colour][inherit]', '', role_info is not null ? role_info.colour.inherit : true) }}
{{ input_checkbox('role[colour][inherit]', '', role_info is not null ? role_info.colour.shouldInherit : true) }}
</div>
</label>

View File

@ -99,11 +99,11 @@
<label class="form__label">
<div class="form__label__text">Custom Colour</div>
<div class="form__label__input">
{{ input_checkbox('colour[enable]', '', not user_info.userColour.inherit, '', '', false, null, not can_edit_user) }}
{{ input_checkbox('colour[enable]', '', not user_info.userColour.shouldInherit, '', '', false, null, not can_edit_user) }}
</div>
</label>
{{ input_colour(can_edit_user ? 'colour[hex]' : '', '', '#%s'|format(user_info.userColour.hex)) }}
{{ input_colour(can_edit_user ? 'colour[hex]' : '', '', user_info.userColour) }}
</div>
{# TODO: if the hierarchy of the current user is too low to touch the role then opacity should be lowered and input disabled #}

View File

@ -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;
}