Skip to content

Commit d235977

Browse files
committed
Warn on non-opaque colors for problems
1 parent e81837d commit d235977

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

webapp/src/Service/CheckConfigService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ public function checkContestsValidate(): ConfigCheckItem
474474
if (empty($cp->getColor())) {
475475
$result = ($result === 'E' ? 'E' : 'W');
476476
$cperrors[$cid] .= " - No color for problem `" . $cp->getShortname() . "` in contest c" . $cid . "\n";
477+
} elseif (Utils::parseHexColor($cp->getColor())[3] !== 255) {
478+
$result = ($result === 'E' ? 'E' : 'W');
479+
$cperrors[$cid] .= " - Semi-transparent color for problem `" . $cp->getShortname() . "` in contest c" . $cid . "\n";
477480
}
478481
}
479482
}

webapp/src/Utils/Utils.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,24 @@ public static function convertToColor(string $hex): ?string
343343
}
344344

345345
/**
346-
* Parse a hex color into it's three RGB values.
346+
* Parse a hex color into it's four RGBA values.
347347
*
348-
* @return array{int, int, int}
348+
* @return array{int, int, int, int}
349349
*/
350350
public static function parseHexColor(string $hex): array
351351
{
352352
// Source: https://stackoverflow.com/a/21966100
353-
$length = (strlen($hex) - 1) / 3;
353+
$length = (strlen($hex) - 1) / 4;
354+
if (((strlen($hex) - 1) % 3) == 0) {
355+
$length = (strlen($hex) - 1) / 3;
356+
$hex .= str_repeat('F', $length);
357+
}
354358
$fact = [17, 1, 0.062272][$length - 1];
355359
return [
356360
(int)round(hexdec(substr($hex, 1, $length)) * $fact),
357361
(int)round(hexdec(substr($hex, 1 + $length, $length)) * $fact),
358-
(int)round(hexdec(substr($hex, 1 + 2 * $length, $length)) * $fact)
362+
(int)round(hexdec(substr($hex, 1 + 2 * $length, $length)) * $fact),
363+
(int)round(hexdec(substr($hex, 1 + 3 * $length, $length)))
359364
];
360365
}
361366

webapp/tests/Unit/Utils/UtilsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,10 @@ public function testConvertToHexConvert(): void
321321

322322
public function testParseHexColor(): void
323323
{
324-
self::assertEquals([255, 255, 255], Utils::parseHexColor('#ffffff'));
325-
self::assertEquals([0, 0, 0], Utils::parseHexColor('#000000'));
326-
self::assertEquals([171, 205, 239], Utils::parseHexColor('#abcdef'));
327-
self::assertEquals([254, 220, 186], Utils::parseHexColor('#FEDCBA'));
324+
self::assertEquals([255, 255, 255, 255], Utils::parseHexColor('#ffffff'));
325+
self::assertEquals([0, 0, 0, 255], Utils::parseHexColor('#000000'));
326+
self::assertEquals([171, 205, 239, 255], Utils::parseHexColor('#abcdef'));
327+
self::assertEquals([254, 220, 186, 255], Utils::parseHexColor('#FEDCBA'));
328328
}
329329

330330
public function testComponentToHex(): void

0 commit comments

Comments
 (0)