Skip to content

Commit 1057181

Browse files
Merge pull request #21594 from nextcloud/bugfix/21586/precalculate-primary-element-color-for-dark-mode-too
Precalculate the primary element color for dark mode too
2 parents ec5c305 + f8641a6 commit 1057181

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

apps/theming/lib/Capabilities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function getCapabilities() {
7878
'color' => $color,
7979
'color-text' => $this->theming->getTextColorPrimary(),
8080
'color-element' => $this->util->elementColor($color),
81+
'color-element-bright' => $this->util->elementColor($color),
82+
'color-element-dark' => $this->util->elementColor($color, false),
8183
'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
8284
'background' => $backgroundLogo === 'backgroundColor' || ($backgroundLogo === false && $this->theming->getColorPrimary() !== '#0082c9') ?
8385
$this->theming->getColorPrimary() :

apps/theming/lib/Util.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,23 @@ public function invertTextColor($color) {
7676
/**
7777
* get color for on-page elements:
7878
* theme color by default, grey if theme color is to bright
79-
* @param $color
79+
* @param string $color
80+
* @param bool $brightBackground
8081
* @return string
8182
*/
82-
public function elementColor($color) {
83-
$l = $this->calculateLuminance($color);
84-
if ($l>0.8) {
83+
public function elementColor($color, bool $brightBackground = true) {
84+
$luminance = $this->calculateLuminance($color);
85+
86+
if ($brightBackground && $luminance > 0.8) {
87+
// If the color is too bright in bright mode, we fall back to a darker gray
8588
return '#aaaaaa';
8689
}
90+
91+
if (!$brightBackground && $luminance < 0.2) {
92+
// If the color is too dark in dark mode, we fall back to a brighter gray
93+
return '#555555';
94+
}
95+
8796
return $color;
8897
}
8998

apps/theming/tests/CapabilitiesTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function dataGetCapabilities() {
7878
'color' => '#FFFFFF',
7979
'color-text' => '#000000',
8080
'color-element' => '#aaaaaa',
81+
'color-element-bright' => '#aaaaaa',
82+
'color-element-dark' => '#FFFFFF',
8183
'logo' => 'http://absolute/logo',
8284
'background' => 'http://absolute/background',
8385
'background-plain' => false,
@@ -92,6 +94,8 @@ public function dataGetCapabilities() {
9294
'color' => '#01e4a0',
9395
'color-text' => '#ffffff',
9496
'color-element' => '#01e4a0',
97+
'color-element-bright' => '#01e4a0',
98+
'color-element-dark' => '#01e4a0',
9599
'logo' => 'http://localhost/logo5',
96100
'background' => 'http://localhost/background6',
97101
'background-plain' => false,
@@ -106,6 +110,8 @@ public function dataGetCapabilities() {
106110
'color' => '#000000',
107111
'color-text' => '#ffffff',
108112
'color-element' => '#000000',
113+
'color-element-bright' => '#000000',
114+
'color-element-dark' => '#555555',
109115
'logo' => 'http://localhost/logo5',
110116
'background' => '#000000',
111117
'background-plain' => true,
@@ -120,6 +126,8 @@ public function dataGetCapabilities() {
120126
'color' => '#000000',
121127
'color-text' => '#ffffff',
122128
'color-element' => '#000000',
129+
'color-element-bright' => '#000000',
130+
'color-element-dark' => '#555555',
123131
'logo' => 'http://localhost/logo5',
124132
'background' => '#000000',
125133
'background-plain' => true,
@@ -167,10 +175,12 @@ public function testGetCapabilities($name, $url, $slogan, $color, $textColor, $l
167175
->willReturn($textColor);
168176

169177
$util = new Util($this->config, $this->createMock(IAppManager::class), $this->createMock(IAppData::class));
170-
$this->util->expects($this->once())
178+
$this->util->expects($this->exactly(3))
171179
->method('elementColor')
172180
->with($color)
173-
->willReturn($util->elementColor($color));
181+
->willReturnCallback(static function (string $color, bool $brightBackground = true) use ($util) {
182+
return $util->elementColor($color, $brightBackground);
183+
});
174184

175185
$this->util->expects($this->once())
176186
->method('isBackgroundThemed')

apps/theming/tests/UtilTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public function testElementColorDefault() {
106106
$this->assertEquals('#000000', $elementColor);
107107
}
108108

109+
public function testElementColorOnDarkBackground() {
110+
$elementColor = $this->util->elementColor("#000000", false);
111+
$this->assertEquals('#555555', $elementColor);
112+
}
113+
109114
public function testElementColorOnBrightBackground() {
110115
$elementColor = $this->util->elementColor('#ffffff');
111116
$this->assertEquals('#aaaaaa', $elementColor);

0 commit comments

Comments
 (0)