Skip to content

Commit e8275ab

Browse files
authored
Merge pull request #42329 from nextcloud/fix/highcontrast
fix(theming): Adjust dark high contrast to fulfill WCAG 2.1 AAA contrast
2 parents c3d5b46 + acfb8ef commit e8275ab

File tree

8 files changed

+215
-27
lines changed

8 files changed

+215
-27
lines changed

apps/theming/css/default.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
/** @deprecated use `--color-text-maxcontrast` instead */
2222
--color-text-lighter: var(--color-text-maxcontrast);
2323
--color-scrollbar: rgba(34,34,34, .15);
24-
--color-error: #C00505;
25-
--color-error-rgb: 192,5,5;
26-
--color-error-hover: #c72424;
27-
--color-error-text: #C00505;
24+
--color-error: #DB0606;
25+
--color-error-rgb: 219,6,6;
26+
--color-error-hover: #df2525;
27+
--color-error-text: #c20505;
2828
--color-warning: #A37200;
2929
--color-warning-rgb: 163,114,0;
3030
--color-warning-hover: #8a6000;

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 = '#ff5252';
63+
$colorWarning = '#ffcc00';
64+
$colorSuccess = '#42a942';
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, 25),
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, 35),
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/Themes/DarkTheme.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ public function getDescription(): string {
5252
public function getCSSVariables(): array {
5353
$defaultVariables = parent::getCSSVariables();
5454

55-
$colorMainText = '#D8D8D8';
55+
$colorMainText = '#EBEBEB';
5656
$colorMainBackground = '#171717';
5757
$colorMainBackgroundRGB = join(',', $this->util->hexToRGB($colorMainBackground));
58-
$colorTextMaxcontrast = $this->util->darken($colorMainText, 28);
58+
$colorTextMaxcontrast = $this->util->darken($colorMainText, 32);
5959

6060
$colorBoxShadow = $this->util->darken($colorMainBackground, 70);
6161
$colorBoxShadowRGB = join(',', $this->util->hexToRGB($colorBoxShadow));
6262

63-
$colorError = '#FF5252';
63+
$colorError = '#FF3333';
6464
$colorWarning = '#FFCC00';
65-
$colorSuccess = '#50BB50';
65+
$colorSuccess = '#3B973B';
6666
$colorInfo = '#00AEFF';
6767

6868
return array_merge(
@@ -92,15 +92,15 @@ public function getCSSVariables(): array {
9292
'--color-error' => $colorError,
9393
'--color-error-rgb' => join(',', $this->util->hexToRGB($colorError)),
9494
'--color-error-hover' => $this->util->lighten($colorError, 10),
95-
'--color-error-text' => $this->util->lighten($colorError, 10),
95+
'--color-error-text' => $this->util->lighten($colorError, 15),
9696
'--color-warning' => $colorWarning,
9797
'--color-warning-rgb' => join(',', $this->util->hexToRGB($colorWarning)),
9898
'--color-warning-hover' => $this->util->lighten($colorWarning, 10),
9999
'--color-warning-text' => $colorWarning,
100100
'--color-success' => $colorSuccess,
101101
'--color-success-rgb' => join(',', $this->util->hexToRGB($colorSuccess)),
102102
'--color-success-hover' => $this->util->lighten($colorSuccess, 10),
103-
'--color-success-text' => $colorSuccess,
103+
'--color-success-text' => $this->util->lighten($colorSuccess, 15),
104104
'--color-info' => $colorInfo,
105105
'--color-info-rgb' => join(',', $this->util->hexToRGB($colorInfo)),
106106
'--color-info-hover' => $this->util->lighten($colorInfo, 10),

apps/theming/lib/Themes/DefaultTheme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getCSSVariables(): array {
111111
$colorBoxShadow = $this->util->darken($colorMainBackground, 70);
112112
$colorBoxShadowRGB = join(',', $this->util->hexToRGB($colorBoxShadow));
113113

114-
$colorError = '#C00505';
114+
$colorError = '#DB0606';
115115
$colorWarning = '#A37200';
116116
$colorSuccess = '#2d7b41';
117117
$colorInfo = '#0071ad';
@@ -148,7 +148,7 @@ public function getCSSVariables(): array {
148148
'--color-error' => $colorError,
149149
'--color-error-rgb' => join(',', $this->util->hexToRGB($colorError)),
150150
'--color-error-hover' => $this->util->mix($colorError, $colorMainBackground, 75),
151-
'--color-error-text' => $colorError,
151+
'--color-error-text' => $this->util->darken($colorError, 5),
152152
'--color-warning' => $colorWarning,
153153
'--color-warning-rgb' => join(',', $this->util->hexToRGB($colorWarning)),
154154
'--color-warning-hover' => $this->util->darken($colorWarning, 5),

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: 27 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,18 @@ public function dataAccessibilityPairs() {
6472
'--color-background-darker',
6573
'--color-main-background-blur',
6674
],
67-
3.0,
75+
$elementContrast,
76+
],
77+
// Those two colors are used for borders which will be `color-main-text` on focussed state, thus need 3:1 contrast to it
78+
'success-error-border-colors' => [
79+
[
80+
'--color-error',
81+
'--color-success',
82+
],
83+
[
84+
'--color-main-text',
85+
],
86+
$elementContrast,
6887
],
6988
'primary-element-text' => [
7089
[
@@ -75,15 +94,15 @@ public function dataAccessibilityPairs() {
7594
'--color-primary-element',
7695
'--color-primary-element-hover',
7796
],
78-
4.5,
97+
$textContrast,
7998
],
8099
'primary-element-light-text' => [
81100
['--color-primary-element-light-text'],
82101
[
83102
'--color-primary-element-light',
84103
'--color-primary-element-light-hover',
85104
],
86-
4.5,
105+
$textContrast,
87106
],
88107
'main-text' => [
89108
['--color-main-text'],
@@ -94,7 +113,7 @@ public function dataAccessibilityPairs() {
94113
'--color-background-darker',
95114
'--color-main-background-blur',
96115
],
97-
4.5,
116+
$textContrast,
98117
],
99118
'max-contrast-text' => [
100119
['--color-text-maxcontrast'],
@@ -103,14 +122,14 @@ public function dataAccessibilityPairs() {
103122
'--color-background-hover',
104123
'--color-background-dark',
105124
],
106-
4.5,
125+
$textContrast,
107126
],
108127
'max-contrast text-on blur' => [
109128
['--color-text-maxcontrast-background-blur'],
110129
[
111130
'--color-main-background-blur',
112131
],
113-
4.5,
132+
$textContrast,
114133
],
115134
'status-text' => [
116135
[
@@ -125,7 +144,7 @@ public function dataAccessibilityPairs() {
125144
'--color-background-dark',
126145
'--color-main-background-blur',
127146
],
128-
4.5,
147+
$textContrast,
129148
],
130149
];
131150
}
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æ <skjnldsv@protonmail.com>
4+
*
5+
* @author John Molakvoæ <skjnldsv@protonmail.com>
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)