Füge die zentrale Farbpalette und die Hintergrundbreite für Abschnitte hinzu (ADR-67)

- DesignPalette: bis zu zehn benannte Farben (Hintergrund und Schrift) in site_settings,
  stabile Kennungen, Kontrastprüfung 4,5:1 auf dem Server, Verwendungssuche
- Seite Website → Design → Farben (Systemverwalter): Kacheln, Dialog mit Live-Vorschau,
  Kontrastwarnung mit Empfehlung, Bestätigung mit betroffenen Seiten, Entfernen gesperrt bei Verwendung
- Hintergrund „Farbe aus der Palette“ als Verweis im bestehenden Feld background; farbige Kacheln im Editor
- Renderer leitet Bedeutungswerte aus Hintergrund und Schrift ab; scope-dark bei heller Schrift
- Hintergrundbreite: volle Browserbreite (Vorgabe) oder auf Abschnittsbreite begrenzt
- Test sichert die Ausgabe aller bisherigen Varianten byte-gleich; Abnahme mit Screenshots

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-29 18:35:15 +01:00
1 parent 62f3a33541
commit 87de5a62d4
41 files changed
+2372 -14

No files matched your search

+222
View File
@@ -0,0 +1,222 @@
<?php
namespace App\Domain\Design;
/**
* Farbrechnung für die Farbpalette (ADR-67): Normalisierung von HEX-Werten, Kontrast nach WCAG 2.2 und die Werte, die ein
* Abschnitt mit eigener Farbe an seine Bausteine weitergibt. Reine Funktionen ohne Zugriff auf Datenbank oder Anfrage.
*
* Die Rechnung steht gleichlautend in resources/views/filament/design/farben.blade.php (Live-Vorschau im Browser); beide
* werden gegen dieselben Vergleichswerte geprüft (tests/Unit/ColorContrastTest.php).
*/
final class ColorContrast
{
/** Mindestkontrast für normalen Text (WCAG 2.2 AA, 1.4.3). */
public const NORMAL_TEXT = 4.5;
/** Mindestkontrast für großen Text und für Bedienelemente (WCAG 2.2 AA, 1.4.3 und 1.4.11). */
public const LARGE_TEXT = 3.0;
/** Erhöhter Kontrast (AAA) für normalen Text. */
public const ENHANCED = 7.0;
public const NAVY = '#032551';
public const WHITE = '#ffffff';
public const YELLOW = '#fcd10a';
public const GREEN_DEEP = '#3b7b09';
public const FOCUS_BLUE = '#1769aa';
public const BLACK = '#000000';
/**
* „#abc“ und „#aabbcc“ (auch ohne Raute, beliebige Schreibweise) → „#aabbcc“ in Kleinbuchstaben; sonst null.
*/
public static function normalize(mixed $value): ?string
{
if (! is_string($value)) {
return null;
}
$value = ltrim(trim($value), '#');
if (preg_match('/^[0-9a-fA-F]{3}$/', $value) === 1) {
$value = $value[0].$value[0].$value[1].$value[1].$value[2].$value[2];
}
return preg_match('/^[0-9a-fA-F]{6}$/', $value) === 1 ? '#'.strtolower($value) : null;
}
/**
* @return array{0: int, 1: int, 2: int}
*/
public static function rgb(string $hex): array
{
$hex = self::normalize($hex) ?? self::BLACK;
return [hexdec(substr($hex, 1, 2)), hexdec(substr($hex, 3, 2)), hexdec(substr($hex, 5, 2))];
}
/** Relative Leuchtdichte nach WCAG (0 = Schwarz, 1 = Weiß). */
public static function luminance(string $hex): float
{
[$r, $g, $b] = array_map(static function (int $channel): float {
$c = $channel / 255;
return $c <= 0.03928 ? $c / 12.92 : (($c + 0.055) / 1.055) ** 2.4;
}, self::rgb($hex));
return 0.2126 * $r + 0.7152 * $g + 0.0722 * $b;
}
/** Kontrastverhältnis zweier Farben (1 bis 21), auf zwei Stellen gerundet. */
public static function ratio(string $a, string $b): float
{
$la = self::luminance($a);
$lb = self::luminance($b);
return round((max($la, $lb) + 0.05) / (min($la, $lb) + 0.05), 2);
}
/**
* Prüfstatus: „enhanced“ (ab 7:1), „aa“ (ab 4,5:1), „large“ (ab 3:1, nur große Schrift) oder „fail“.
*/
public static function rating(float $ratio): string
{
return match (true) {
$ratio >= 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<string, string> 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<string> $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<string> $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;
}
}