= self::ENHANCED => 'enhanced', $ratio >= self::NORMAL_TEXT => 'aa', $ratio >= self::LARGE_TEXT => 'large', default => 'fail', }; } /** Ob die Schrift auf dem Hintergrund für normalen Text ausreicht. */ public static function passes(string $text, string $background): bool { return self::ratio($text, $background) >= self::NORMAL_TEXT; } /** * Empfohlene Schriftfarbe: bevorzugt Marken-Dunkelblau oder Weiß, sonst Schwarz oder Weiß, jeweils die mit dem * höchsten Kontrast, die für normalen Text ausreicht. Nur eine Bedienhilfe: Sie wird nie von selbst gesetzt. */ public static function suggestText(string $background): string { $brand = self::best($background, [self::NAVY, self::WHITE]); if (self::passes($brand, $background)) { return $brand; } return self::best($background, [self::BLACK, self::WHITE]); } /** * Mischt zwei Farben; $share ist der Anteil von $b (0 bis 1). */ public static function mix(string $a, string $b, float $share): string { $ra = self::rgb($a); $rb = self::rgb($b); $out = '#'; foreach ([0, 1, 2] as $i) { $out .= str_pad(dechex((int) round($ra[$i] * (1 - $share) + $rb[$i] * $share)), 2, '0', STR_PAD_LEFT); } return $out; } /** * Ob die Fläche eine dunkle Fläche im Sinn der Bausteine ist: Die Schrift ist heller als der Hintergrund. Dann gelten * die Bedeutungswerte der dunklen Fläche (scope-dark), sonst die der hellen. */ public static function isDarkSurface(string $text, string $background): bool { return self::luminance($text) > self::luminance($background); } /** * Bedeutungswerte (CSS-Eigenschaften), die ein Abschnitt mit eigener Farbe an seine Bausteine weitergibt. Alle * abgeleiteten Farben werden gegen den Hintergrund geprüft: Fließtext 4,5:1, Fokus und Schaltfläche 3:1. * * @return array Eigenschaft (ohne „--“) => Wert */ public static function surfaceProperties(string $background, string $text): array { $background = self::normalize($background) ?? self::WHITE; $text = self::normalize($text) ?? self::NAVY; $dark = self::isDarkSurface($text, $background); // Zurückhaltender Text: ein Stück zum Hintergrund hin, aber nie unter 4,5:1. $muted = self::mix($text, $background, 0.15); if (! self::passes($muted, $background)) { $muted = $text; } // Fokus: Marken-Gelb auf dunklen, Marken-Blau auf hellen Flächen; reicht der Kontrast nicht, Weiß oder Schwarz. $focus = self::firstWithRatio($background, $dark ? [self::YELLOW, self::WHITE, self::BLACK] : [self::FOCUS_BLUE, self::NAVY, self::BLACK, self::WHITE], self::LARGE_TEXT); // Link beim Überfahren: Markenton, wenn er als Text lesbar bleibt, sonst die Schriftfarbe. $linkHover = self::firstWithRatio($background, $dark ? [self::YELLOW] : [self::GREEN_DEEP], self::NORMAL_TEXT) ?? $text; // Schaltfläche: Fläche mit 3:1 gegen den Hintergrund, Beschriftung mit 4,5:1 gegen die Schaltfläche. $action = self::firstWithRatio($background, $dark ? [self::YELLOW, self::WHITE, self::BLACK] : [self::GREEN_DEEP, self::NAVY, self::BLACK, self::WHITE], self::LARGE_TEXT) ?? $text; $actionText = self::best($action, [self::NAVY, self::WHITE]); if (! self::passes($actionText, $action)) { $actionText = self::best($action, [self::BLACK, self::WHITE]); } // Beim Überfahren etwas vom Beschriftungston wegmischen: Der Kontrast zur Beschriftung wächst. $actionHover = self::mix($action, self::luminance($actionText) > 0.5 ? self::BLACK : self::WHITE, 0.16); [$r, $g, $b] = self::rgb($text); return [ 'color-surface' => $background, 'color-text' => $text, 'color-text-muted' => $muted, 'color-heading' => $text, 'color-link' => $text, 'color-link-hover' => $linkHover, 'color-border' => "rgb($r $g $b / 32%)", 'color-border-strong' => "rgb($r $g $b / 60%)", 'color-focus' => $focus ?? $text, 'color-action' => $action, 'color-action-hover' => $actionHover, 'color-action-text' => $actionText, 'color-action-secondary-text' => $text, ]; } /** * @param list $candidates */ private static function best(string $background, array $candidates): string { usort($candidates, static fn (string $a, string $b): int => self::ratio($b, $background) <=> self::ratio($a, $background)); return $candidates[0]; } /** * @param list $candidates */ private static function firstWithRatio(string $background, array $candidates, float $minimum): ?string { foreach ($candidates as $candidate) { if (self::ratio($candidate, $background) >= $minimum) { return $candidate; } } return null; } }