standard colours with noce UI and lasyout changes
This commit is contained in:
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Modules\Helpdesk\Support;
|
||||
|
||||
use Closure;
|
||||
|
||||
final class Color
|
||||
{
|
||||
/**
|
||||
@@ -41,23 +43,37 @@ final class Color
|
||||
];
|
||||
|
||||
/**
|
||||
* Resolve a stored colour key or supported hex value into a CSS colour.
|
||||
* Resolve a stored palette key or supported custom hex value into a CSS colour.
|
||||
*/
|
||||
public static function css(?string $color): string
|
||||
{
|
||||
$resolved = self::normalise($color);
|
||||
|
||||
if (str_starts_with($resolved, '#')) {
|
||||
if (self::isHex($resolved)) {
|
||||
return $resolved;
|
||||
}
|
||||
|
||||
return self::PALETTE[$resolved] ?? self::PALETTE['gray'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a readable foreground colour for a badge background.
|
||||
*/
|
||||
public static function text(?string $color): string
|
||||
{
|
||||
$hex = ltrim(self::css($color), '#');
|
||||
$red = hexdec(substr($hex, 0, 2));
|
||||
$green = hexdec(substr($hex, 2, 2));
|
||||
$blue = hexdec(substr($hex, 4, 2));
|
||||
$luminance = (($red * 299) + ($green * 587) + ($blue * 114)) / 1000;
|
||||
|
||||
return $luminance >= 150 ? '#111827' : '#ffffff';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return approved palette options for settings dropdowns.
|
||||
*
|
||||
* @return array<string, array{name: string, hex: string}>
|
||||
* @return array<string, array{name: string, hex: string, foreground: string}>
|
||||
*/
|
||||
public static function options(): array
|
||||
{
|
||||
@@ -66,6 +82,7 @@ final class Color
|
||||
$name => [
|
||||
'name' => str($name)->headline()->toString(),
|
||||
'hex' => $hex,
|
||||
'foreground' => self::text($hex),
|
||||
],
|
||||
])
|
||||
->all();
|
||||
@@ -82,16 +99,60 @@ final class Color
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise legacy aliases and supported hex colours.
|
||||
* Normalise legacy aliases and supported custom hex colours.
|
||||
*/
|
||||
public static function normalise(?string $color): string
|
||||
{
|
||||
$value = strtolower(trim((string) $color));
|
||||
|
||||
if (preg_match('/^#[a-f0-9]{6}$/', $value) === 1) {
|
||||
if (self::isHex($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::ALIASES[$value] ?? $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the value is a supported six-character hex colour.
|
||||
*/
|
||||
public static function isHex(?string $color): bool
|
||||
{
|
||||
return preg_match('/^#[a-f0-9]{6}$/', strtolower(trim((string) $color))) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a stored colour is an approved palette key or custom hex colour.
|
||||
*/
|
||||
public static function isAllowed(?string $color): bool
|
||||
{
|
||||
$resolved = self::normalise($color);
|
||||
|
||||
return self::isHex($resolved) || in_array($resolved, self::keys(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Laravel validation rule allowing approved palette keys and six-character hex values.
|
||||
*/
|
||||
public static function validationRule(): Closure
|
||||
{
|
||||
return static function (string $attribute, mixed $value, Closure $fail): void {
|
||||
if (! self::isAllowed(is_string($value) ? $value : null)) {
|
||||
$fail('The '.$attribute.' must be an approved Helpdesk colour or a valid hex colour such as #ffffff.');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Human-readable label for palette and custom hex values.
|
||||
*/
|
||||
public static function label(?string $color): string
|
||||
{
|
||||
$resolved = self::normalise($color);
|
||||
|
||||
if (self::isHex($resolved)) {
|
||||
return strtoupper($resolved);
|
||||
}
|
||||
|
||||
return str($resolved ?: 'gray')->headline()->toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user