Skip to content

Commit d5e63f9

Browse files
committed
Temporary move the function to Utils for testing
1 parent ea154e7 commit d5e63f9

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

webapp/src/Twig/TwigExtension.php

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,56 +1155,6 @@ public function fileTypeIcon(string $type): string
11551155
return 'fas fa-file-' . $iconName;
11561156
}
11571157

1158-
private function relativeLuminance(string $rgb): float
1159-
{
1160-
// See https://en.wikipedia.org/wiki/Relative_luminance
1161-
[$r, $g, $b] = Utils::parseHexColor($rgb);
1162-
1163-
[$lr, $lg, $lb] = [
1164-
pow($r / 255, 2.4),
1165-
pow($g / 255, 2.4),
1166-
pow($b / 255, 2.4),
1167-
];
1168-
1169-
return 0.2126 * $lr + 0.7152 * $lg + 0.0722 * $lb;
1170-
}
1171-
1172-
private function apcaContrast(string $fgColor, string $bgColor): float
1173-
{
1174-
// Based on WCAG 3.x (https://www.w3.org/TR/wcag-3.0/)
1175-
$luminanceForeground = $this->relativeLuminance($fgColor);
1176-
$luminanceBackground = $this->relativeLuminance($bgColor);
1177-
1178-
$contrast = ($luminanceBackground > $luminanceForeground)
1179-
? (pow($luminanceBackground, 0.56) - pow($luminanceForeground, 0.57)) * 1.14
1180-
: (pow($luminanceBackground, 0.65) - pow($luminanceForeground, 0.62)) * 1.14;
1181-
1182-
return round($contrast * 100, 2);
1183-
}
1184-
1185-
/**
1186-
* @return array{string, string}
1187-
*/
1188-
private function hexToForegroundAndBorder(string $rgb): array
1189-
{
1190-
$background = Utils::parseHexColor($rgb);
1191-
1192-
// Pick a border that's a bit darker.
1193-
$darker = $background;
1194-
$darker[0] = max($darker[0] - 64, 0);
1195-
$darker[1] = max($darker[1] - 64, 0);
1196-
$darker[2] = max($darker[2] - 64, 0);
1197-
$border = Utils::rgbToHex($darker);
1198-
1199-
// Pick the text color with the biggest absolute contrast.
1200-
$contrastWithWhite = $this->apcaContrast('#ffffff', $rgb);
1201-
$contrastWithBlack = $this->apcaContrast('#000000', $rgb);
1202-
1203-
$foreground = (abs($contrastWithBlack) > abs($contrastWithWhite)) ? '#000000' : '#ffffff';
1204-
1205-
return [$foreground, $border];
1206-
}
1207-
12081158
public function problemBadge(ContestProblem $problem, bool $grayedOut = false): string
12091159
{
12101160
$rgb = Utils::convertToHex($problem->getColor() ?? '#ffffff');
@@ -1238,7 +1188,7 @@ public function problemBadgeMaybe(
12381188
$rgb = Utils::convertToHex('whitesmoke');
12391189
}
12401190

1241-
[$foreground, $border] = $this->hexToForegroundAndBorder($rgb);
1191+
[$foreground, $border] = Utils::hexToForegroundAndBorder($rgb);
12421192

12431193
if (!$matrixItem->isCorrect) {
12441194
$foreground = 'silver';

webapp/src/Utils/Utils.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,56 @@ public static function rgbToHex(array $color): string
378378
return "#" . static::componentToHex($color[0]) . static::componentToHex($color[1]) . static::componentToHex($color[2]);
379379
}
380380

381+
public static function relativeLuminance(string $rgb): float
382+
{
383+
// See https://en.wikipedia.org/wiki/Relative_luminance
384+
[$r, $g, $b] = static::parseHexColor($rgb);
385+
386+
[$lr, $lg, $lb] = [
387+
pow($r / 255, 2.4),
388+
pow($g / 255, 2.4),
389+
pow($b / 255, 2.4),
390+
];
391+
392+
return 0.2126 * $lr + 0.7152 * $lg + 0.0722 * $lb;
393+
}
394+
395+
public static function apcaContrast(string $fgColor, string $bgColor): float
396+
{
397+
// Based on WCAG 3.x (https://www.w3.org/TR/wcag-3.0/)
398+
$luminanceForeground = static::relativeLuminance($fgColor);
399+
$luminanceBackground = static::relativeLuminance($bgColor);
400+
401+
$contrast = ($luminanceBackground > $luminanceForeground)
402+
? (pow($luminanceBackground, 0.56) - pow($luminanceForeground, 0.57)) * 1.14
403+
: (pow($luminanceBackground, 0.65) - pow($luminanceForeground, 0.62)) * 1.14;
404+
405+
return round($contrast * 100, 2);
406+
}
407+
408+
/**
409+
* @return array{string, string}
410+
*/
411+
private function hexToForegroundAndBorder(string $rgb): array
412+
{
413+
$background = Utils::parseHexColor($rgb);
414+
415+
// Pick a border that's a bit darker.
416+
$darker = $background;
417+
$darker[0] = max($darker[0] - 64, 0);
418+
$darker[1] = max($darker[1] - 64, 0);
419+
$darker[2] = max($darker[2] - 64, 0);
420+
$border = Utils::rgbToHex($darker);
421+
422+
// Pick the text color with the biggest absolute contrast.
423+
$contrastWithWhite = static::apcaContrast('#ffffff', $rgb);
424+
$contrastWithBlack = static::apcaContrast('#000000', $rgb);
425+
426+
$foreground = (abs($contrastWithBlack) > abs($contrastWithWhite)) ? '#000000' : '#ffffff';
427+
428+
return [$foreground, $border];
429+
}
430+
381431
/**
382432
* Return a rounded float.
383433
*/

0 commit comments

Comments
 (0)