<?php declare(strict_types=1); if (!defined('DASHBOARD_ROOT') && !defined('APP_ROOT')) { http_response_code(403); exit; } class ColorMaster { public static function hexToRgb(string $hex): array { $hex = ltrim($hex, '#'); if (strlen($hex) === 3) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } return [hexdec(substr($hex, 0, 2)), hexdec(substr($hex, 2, 2)), hexdec(substr($hex, 4, 2))]; } public static function rgbToHex(int $r, int $g, int $b): string { return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT); } public static function rgbToHsl(int $r, int $g, int $b): array { $rN = $r / 255; $gN = $g / 255; $bN = $b / 255; $max = max($rN, $gN, $bN); $min = min($rN, $gN, $bN); $l = ($max + $min) / 2; $h = 0; $s = 0; if ($max !== $min) { $d = $max - $min; $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); if ($max === $rN) $h = (($gN - $bN) / $d) + ($gN < $bN ? 6 : 0); elseif ($max === $gN) $h = (($bN - $rN) / $d) + 2; else $h = (($rN - $gN) / $d) + 4; $h *= 60; } return [$h, $s, $l]; } public static function hslToRgb(float $h, float $s, float $l): array { if ($s === 0.0) return [(int)round($l * 255), (int)round($l * 255), (int)round($l * 255)]; $hue2rgb = function(float $p, float $q, float $t): float { if ($t < 0) $t += 1; if ($t > 1) $t -= 1; if ($t < 1/6) return $p + ($q - $p) * 6 * $t; if ($t < 1/2) return $q; if ($t < 2/3) return $p + ($q - $p) * (2/3 - $t) * 6; return $p; }; $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; $p = 2 * $l - $q; return [ (int)round($hue2rgb($p, $q, $h / 360 + 1/3) * 255), (int)round($hue2rgb($p, $q, $h / 360) * 255), (int)round($hue2rgb($p, $q, $h / 360 - 1/3) * 255) ]; } public static function lighten(string $hex, float $percent): string { [$r, $g, $b] = self::hexToRgb($hex); [$h, $s, $l] = self::rgbToHsl($r, $g, $b); $l = min(1.0, $l + ($percent / 100)); [$r, $g, $b] = self::hslToRgb($h, $s, $l); return self::rgbToHex($r, $g, $b); } public static function darken(string $hex, float $percent): string { [$r, $g, $b] = self::hexToRgb($hex); [$h, $s, $l] = self::rgbToHsl($r, $g, $b); $l = max(0.0, $l - ($percent / 100)); [$r, $g, $b] = self::hslToRgb($h, $s, $l); return self::rgbToHex($r, $g, $b); } public static function generateCSS(string $hex): string { [$r, $g, $b] = self::hexToRgb($hex); $rgb = "$r, $g, $b"; $light = self::lighten($hex, 40); $dark = self::darken($hex, 30); return ":root{--accent:{$hex};--accent-rgb:{$rgb};--accent-light:{$light};--accent-dark:{$dark};--accent-glow:rgba({$rgb},0.15);--accent-gradient:linear-gradient(135deg,{$hex},{$light})}"; } public static function generateStyleTag(string $hex): string { return '<style>' . self::generateCSS($hex) . '</style>'; } } 