Skip to content

Commit 11434aa

Browse files
committed
fix(theming): Adjust dark high contrast to fulfill WCAG 2.1 AAA contrast
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 734f0dc commit 11434aa

File tree

5 files changed

+192
-15
lines changed

5 files changed

+192
-15
lines changed

apps/theming/lib/Themes/CommonThemeTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ trait CommonThemeTrait {
3838
* This is shared between multiple themes because colorMainBackground and colorMainText
3939
* will change in between.
4040
*/
41-
protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText): array {
41+
protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText, bool $highContrast = false): array {
4242
$isBrightColor = $this->util->isBrightColor($colorMainBackground);
43-
$colorPrimaryElement = $this->util->elementColor($this->primaryColor, $isBrightColor, $colorMainBackground);
43+
$colorPrimaryElement = $this->util->elementColor($this->primaryColor, $isBrightColor, $colorMainBackground, $highContrast);
4444
$colorPrimaryLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
4545
$colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
4646
$invertPrimaryTextColor = $this->util->invertTextColor($colorPrimaryElement);

apps/theming/lib/Themes/DarkHighContrastTheme.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,22 @@ public function getCSSVariables(): array {
5959
$colorMainBackground = '#000000';
6060
$colorMainBackgroundRGB = join(',', $this->util->hexToRGB($colorMainBackground));
6161

62+
$colorError = '#ff6b60';
63+
$colorWarning = '#ffcc00';
64+
$colorSuccess = '#6cc66c';
65+
$colorInfo = '#38c0ff';
66+
6267
return array_merge(
6368
$defaultVariables,
64-
$this->generatePrimaryVariables($colorMainBackground, $colorMainText),
69+
$this->generatePrimaryVariables($colorMainBackground, $colorMainText, true),
6570
[
6671
'--color-main-background' => $colorMainBackground,
6772
'--color-main-background-rgb' => $colorMainBackgroundRGB,
6873
'--color-main-background-translucent' => 'rgba(var(--color-main-background-rgb), 1)',
6974
'--color-main-text' => $colorMainText,
7075

71-
'--color-background-dark' => $this->util->lighten($colorMainBackground, 30),
72-
'--color-background-darker' => $this->util->lighten($colorMainBackground, 30),
76+
'--color-background-dark' => $this->util->lighten($colorMainBackground, 25),
77+
'--color-background-darker' => $this->util->lighten($colorMainBackground, 25),
7378

7479
'--color-main-background-blur' => $colorMainBackground,
7580
'--filter-background-blur' => 'none',
@@ -82,6 +87,26 @@ public function getCSSVariables(): array {
8287
'--color-text-light' => $colorMainText,
8388
'--color-text-lighter' => $colorMainText,
8489

90+
'--color-error' => $colorError,
91+
'--color-error-rgb' => join(',', $this->util->hexToRGB($colorError)),
92+
'--color-error-hover' => $this->util->lighten($colorError, 10),
93+
'--color-error-text' => $this->util->lighten($colorError, 20),
94+
95+
'--color-warning' => $colorWarning,
96+
'--color-warning-rgb' => join(',', $this->util->hexToRGB($colorWarning)),
97+
'--color-warning-hover' => $this->util->lighten($colorWarning, 10),
98+
'--color-warning-text' => $this->util->lighten($colorWarning, 10),
99+
100+
'--color-success' => $colorSuccess,
101+
'--color-success-rgb' => join(',', $this->util->hexToRGB($colorSuccess)),
102+
'--color-success-hover' => $this->util->lighten($colorSuccess, 10),
103+
'--color-success-text' => $this->util->lighten($colorSuccess, 20),
104+
105+
'--color-info' => $colorInfo,
106+
'--color-info-rgb' => join(',', $this->util->hexToRGB($colorInfo)),
107+
'--color-info-hover' => $this->util->lighten($colorInfo, 10),
108+
'--color-info-text' => $this->util->lighten($colorInfo, 20),
109+
85110
'--color-scrollbar' => $this->util->lighten($colorMainBackground, 35),
86111

87112
// used for the icon loading animation

apps/theming/lib/Util.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function isBrightColor(string $color): bool {
8181
* @param ?bool $brightBackground
8282
* @return string
8383
*/
84-
public function elementColor($color, ?bool $brightBackground = null, ?string $backgroundColor = null) {
84+
public function elementColor($color, ?bool $brightBackground = null, ?string $backgroundColor = null, bool $highContrast = false) {
8585
if ($backgroundColor !== null) {
8686
$brightBackground = $brightBackground ?? $this->isBrightColor($backgroundColor);
8787
// Minimal amount that is possible to change the luminance
@@ -93,7 +93,9 @@ public function elementColor($color, ?bool $brightBackground = null, ?string $ba
9393
$contrast = $this->colorContrast($color, $blurredBackground);
9494

9595
// Min. element contrast is 3:1 but we need to keep hover states in mind -> min 3.2:1
96-
while ($contrast < 3.2 && $iteration++ < 100) {
96+
$minContrast = $highContrast ? 5.5 : 3.2;
97+
98+
while ($contrast < $minContrast && $iteration++ < 100) {
9799
$hsl = Color::hexToHsl($color);
98100
$hsl['L'] = max(0, min(1, $hsl['L'] + ($brightBackground ? -$epsilon : $epsilon)));
99101
$color = '#' . Color::hslToHex($hsl);

apps/theming/tests/Themes/AccessibleThemeTestCase.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ class AccessibleThemeTestCase extends TestCase {
3030
protected ITheme $theme;
3131
protected Util $util;
3232

33+
/**
34+
* Set to true to check for WCAG AAA level accessibility
35+
*/
36+
protected bool $WCAGaaa = false;
37+
3338
public function dataAccessibilityPairs() {
39+
$textContrast = $this->WCAGaaa ? 7.0 : 4.5;
40+
$elementContrast = 3.0;
41+
3442
return [
3543
'primary-element on background' => [
3644
[
@@ -44,7 +52,7 @@ public function dataAccessibilityPairs() {
4452
'--color-background-darker',
4553
'--color-main-background-blur',
4654
],
47-
3.0,
55+
$elementContrast,
4856
],
4957
'status color elements on background' => [
5058
[
@@ -64,7 +72,7 @@ public function dataAccessibilityPairs() {
6472
'--color-background-darker',
6573
'--color-main-background-blur',
6674
],
67-
3.0,
75+
$elementContrast,
6876
],
6977
'primary-element-text' => [
7078
[
@@ -75,15 +83,15 @@ public function dataAccessibilityPairs() {
7583
'--color-primary-element',
7684
'--color-primary-element-hover',
7785
],
78-
4.5,
86+
$textContrast,
7987
],
8088
'primary-element-light-text' => [
8189
['--color-primary-element-light-text'],
8290
[
8391
'--color-primary-element-light',
8492
'--color-primary-element-light-hover',
8593
],
86-
4.5,
94+
$textContrast,
8795
],
8896
'main-text' => [
8997
['--color-main-text'],
@@ -94,7 +102,7 @@ public function dataAccessibilityPairs() {
94102
'--color-background-darker',
95103
'--color-main-background-blur',
96104
],
97-
4.5,
105+
$textContrast,
98106
],
99107
'max-contrast-text' => [
100108
['--color-text-maxcontrast'],
@@ -103,14 +111,14 @@ public function dataAccessibilityPairs() {
103111
'--color-background-hover',
104112
'--color-background-dark',
105113
],
106-
4.5,
114+
$textContrast,
107115
],
108116
'max-contrast text-on blur' => [
109117
['--color-text-maxcontrast-background-blur'],
110118
[
111119
'--color-main-background-blur',
112120
],
113-
4.5,
121+
$textContrast,
114122
],
115123
'status-text' => [
116124
[
@@ -125,7 +133,7 @@ public function dataAccessibilityPairs() {
125133
'--color-background-dark',
126134
'--color-main-background-blur',
127135
],
128-
4.5,
136+
$textContrast,
129137
],
130138
];
131139
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 John Molakvoæ <[email protected]>
4+
*
5+
* @author John Molakvoæ <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\Theming\Tests\Themes;
24+
25+
use OCA\Theming\AppInfo\Application;
26+
use OCA\Theming\ImageManager;
27+
use OCA\Theming\ITheme;
28+
use OCA\Theming\Service\BackgroundService;
29+
use OCA\Theming\Themes\DarkHighContrastTheme;
30+
use OCA\Theming\ThemingDefaults;
31+
use OCA\Theming\Util;
32+
use OCP\App\IAppManager;
33+
use OCP\Files\IAppData;
34+
use OCP\IConfig;
35+
use OCP\IL10N;
36+
use OCP\IURLGenerator;
37+
use OCP\IUserSession;
38+
use PHPUnit\Framework\MockObject\MockObject;
39+
40+
class DarkHighContrastThemeTest extends AccessibleThemeTestCase {
41+
/** @var ThemingDefaults|MockObject */
42+
private $themingDefaults;
43+
/** @var IUserSession|MockObject */
44+
private $userSession;
45+
/** @var IURLGenerator|MockObject */
46+
private $urlGenerator;
47+
/** @var ImageManager|MockObject */
48+
private $imageManager;
49+
/** @var IConfig|MockObject */
50+
private $config;
51+
/** @var IL10N|MockObject */
52+
private $l10n;
53+
/** @var IAppManager|MockObject */
54+
private $appManager;
55+
56+
// !! important: Enable WCAG AAA tests
57+
protected bool $WCAGaaa = true;
58+
59+
protected function setUp(): void {
60+
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
61+
$this->userSession = $this->createMock(IUserSession::class);
62+
$this->urlGenerator = $this->createMock(IURLGenerator::class);
63+
$this->imageManager = $this->createMock(ImageManager::class);
64+
$this->config = $this->createMock(IConfig::class);
65+
$this->l10n = $this->createMock(IL10N::class);
66+
$this->appManager = $this->createMock(IAppManager::class);
67+
68+
$this->util = new Util(
69+
$this->config,
70+
$this->appManager,
71+
$this->createMock(IAppData::class),
72+
$this->imageManager
73+
);
74+
75+
$this->themingDefaults
76+
->expects($this->any())
77+
->method('getColorPrimary')
78+
->willReturn('#0082c9');
79+
80+
$this->themingDefaults
81+
->expects($this->any())
82+
->method('getDefaultColorPrimary')
83+
->willReturn('#0082c9');
84+
85+
$this->themingDefaults
86+
->expects($this->any())
87+
->method('getBackground')
88+
->willReturn('/apps/' . Application::APP_ID . '/img/background/' . BackgroundService::DEFAULT_BACKGROUND_IMAGE);
89+
90+
$this->l10n
91+
->expects($this->any())
92+
->method('t')
93+
->willReturnCallback(function ($text, $parameters = []) {
94+
return vsprintf($text, $parameters);
95+
});
96+
97+
$this->urlGenerator
98+
->expects($this->any())
99+
->method('imagePath')
100+
->willReturnCallback(function ($app = 'core', $filename = '') {
101+
return "/$app/img/$filename";
102+
});
103+
104+
$this->theme = new DarkHighContrastTheme(
105+
$this->util,
106+
$this->themingDefaults,
107+
$this->userSession,
108+
$this->urlGenerator,
109+
$this->imageManager,
110+
$this->config,
111+
$this->l10n,
112+
$this->appManager,
113+
);
114+
115+
parent::setUp();
116+
}
117+
118+
119+
public function testGetId() {
120+
$this->assertEquals('dark-highcontrast', $this->theme->getId());
121+
}
122+
123+
public function testGetType() {
124+
$this->assertEquals(ITheme::TYPE_THEME, $this->theme->getType());
125+
}
126+
127+
public function testGetTitle() {
128+
$this->assertEquals('Dark theme with high contrast mode', $this->theme->getTitle());
129+
}
130+
131+
public function testGetEnableLabel() {
132+
$this->assertEquals('Enable dark high contrast mode', $this->theme->getEnableLabel());
133+
}
134+
135+
public function testGetDescription() {
136+
$this->assertEquals('Similar to the high contrast mode, but with dark colours.', $this->theme->getDescription());
137+
}
138+
139+
public function testGetMediaQuery() {
140+
$this->assertEquals('(prefers-color-scheme: dark) and (prefers-contrast: more)', $this->theme->getMediaQuery());
141+
}
142+
}

0 commit comments

Comments
 (0)