*/ public static function hexValues(): array { return [ 'lang' => ['#3B7B09', '#3b7b09'], 'ohne Raute' => ['3b7b09', '#3b7b09'], 'kurz' => ['#fa0', '#ffaa00'], 'Leerzeichen' => [' #FFF ', '#ffffff'], 'zu kurz' => ['#ff', null], 'kein Hex' => ['#gggggg', null], 'zu lang' => ['#1234567', null], 'kein Text' => [123, null], 'leer' => ['', null], ]; } #[DataProvider('hexValues')] public function test_hex_values_are_normalised_or_refused(mixed $input, ?string $expected): void { $this->assertSame($expected, ColorContrast::normalize($input)); } /** * @return array */ public static function pairs(): array { return [ 'Schwarz auf Weiß' => ['#000000', '#ffffff', 21.0], 'gleiche Farbe' => ['#336699', '#336699', 1.0], 'Navy auf Weiß (tokens.css: 15:1)' => ['#032551', '#ffffff', 15.0], 'Weiß auf Markengrün (tokens.css: 5,2:1)' => ['#ffffff', '#3b7b09', 5.2], 'Grau 777 auf Weiß (knapp unter AA)' => ['#777777', '#ffffff', 4.48], 'Grau 767676 auf Weiß (knapp AA)' => ['#767676', '#ffffff', 4.54], ]; } #[DataProvider('pairs')] public function test_contrast_ratio_matches_the_wcag_definition(string $a, string $b, float $expected): void { $this->assertEqualsWithDelta($expected, ColorContrast::ratio($a, $b), 0.25); $this->assertSame(ColorContrast::ratio($a, $b), ColorContrast::ratio($b, $a), 'Die Reihenfolge ändert den Kontrast nicht.'); } public function test_the_thresholds_decide_between_aa_large_text_and_failure(): void { $this->assertTrue(ColorContrast::passes('#767676', '#ffffff')); $this->assertFalse(ColorContrast::passes('#777777', '#ffffff')); $this->assertSame('enhanced', ColorContrast::rating(7.0)); $this->assertSame('aa', ColorContrast::rating(4.5)); $this->assertSame('large', ColorContrast::rating(4.49)); $this->assertSame('large', ColorContrast::rating(3.0)); $this->assertSame('fail', ColorContrast::rating(2.99)); } public function test_the_suggested_text_colour_is_always_readable(): void { foreach (['#ffffff', '#000000', '#3b7b09', '#fcd10a', '#808080', '#ff00ff', '#00ffff', '#7a7a2e', '#5b8625', '#e8f0de'] as $background) { $suggestion = ColorContrast::suggestText($background); $this->assertTrue(ColorContrast::passes($suggestion, $background), "Empfehlung $suggestion auf $background"); } $this->assertSame('#ffffff', ColorContrast::suggestText('#3b7b09'), 'Auf dunklem Grün ist Weiß die Marken-Empfehlung.'); $this->assertSame('#032551', ColorContrast::suggestText('#e8f0de'), 'Auf hellem Grün ist Dunkelblau die Marken-Empfehlung.'); } public function test_a_surface_is_dark_when_the_text_is_lighter_than_the_background(): void { $this->assertTrue(ColorContrast::isDarkSurface('#ffffff', '#032551')); $this->assertFalse(ColorContrast::isDarkSurface('#032551', '#e8f0de')); } public function test_derived_surface_values_keep_their_own_contrast_rules(): void { foreach ([['#032551', '#e8f0de'], ['#ffffff', '#3b7b09'], ['#f4f6f8', '#032551'], ['#000000', '#fcd10a'], ['#ffffff', '#6b3fa0']] as [$text, $background]) { $values = ColorContrast::surfaceProperties($background, $text); $label = "$text auf $background"; $this->assertSame($background, $values['color-surface']); $this->assertSame($text, $values['color-text']); $this->assertTrue(ColorContrast::passes($values['color-text-muted'], $background), "Gedämpfter Text: $label"); $this->assertTrue(ColorContrast::passes($values['color-link-hover'], $background), "Link beim Überfahren: $label"); $this->assertGreaterThanOrEqual(3.0, ColorContrast::ratio($values['color-focus'], $background), "Fokus: $label"); $this->assertGreaterThanOrEqual(3.0, ColorContrast::ratio($values['color-action'], $background), "Schaltfläche: $label"); $this->assertTrue(ColorContrast::passes($values['color-action-text'], $values['color-action']), "Schaltflächentext: $label"); } } public function test_dark_surfaces_use_the_yellow_brand_focus_and_light_surfaces_the_blue_one(): void { $this->assertSame('#fcd10a', ColorContrast::surfaceProperties('#032551', '#ffffff')['color-focus']); $this->assertSame('#1769aa', ColorContrast::surfaceProperties('#e8f0de', '#032551')['color-focus']); } }