@@ -1144,12 +1144,11 @@ public function fileTypeIcon(string $type): string
11441144 return 'fas fa-file- ' . $ iconName ;
11451145 }
11461146
1147- public function problemBadge (ContestProblem $ problem , bool $ grayedOut = false ): string
1147+ /**
1148+ * @return array{string, string}
1149+ */
1150+ private function hexToForegroundAndBorder (string $ rgb ): array
11481151 {
1149- $ rgb = Utils::convertToHex ($ problem ->getColor () ?? '#ffffff ' );
1150- if ($ grayedOut || empty ($ rgb )) {
1151- $ rgb = Utils::convertToHex ('whitesmoke ' );
1152- }
11531152 $ background = Utils::parseHexColor ($ rgb );
11541153
11551154 // Pick a border that's a bit darker.
@@ -1159,8 +1158,29 @@ public function problemBadge(ContestProblem $problem, bool $grayedOut = false):
11591158 $ darker [2 ] = max ($ darker [2 ] - 64 , 0 );
11601159 $ border = Utils::rgbToHex ($ darker );
11611160
1162- // Pick the foreground text color based on the background color.
1163- $ foreground = ($ background [0 ] + $ background [1 ] + $ background [2 ] > 450 ) ? '#000000 ' : '#ffffff ' ;
1161+ [$ r , $ g , $ b ] = $ background ;
1162+
1163+ // Calculate relative luminance
1164+ $ r = ($ r / 255 <= 0.03928 ) ? ($ r / 255 ) / 12.92 : pow (($ r / 255 + 0.055 ) / 1.055 , 2.4 );
1165+ $ g = ($ g / 255 <= 0.03928 ) ? ($ g / 255 ) / 12.92 : pow (($ g / 255 + 0.055 ) / 1.055 , 2.4 );
1166+ $ b = ($ b / 255 <= 0.03928 ) ? ($ b / 255 ) / 12.92 : pow (($ b / 255 + 0.055 ) / 1.055 , 2.4 );
1167+
1168+ $ luminance = 0.2126 * $ r + 0.7152 * $ g + 0.0722 * $ b ;
1169+
1170+ $ foreground = ($ luminance > 0.179 ) ? '#000000 ' : '#FFFFFF ' ;
1171+
1172+ return [$ foreground , $ border ];
1173+ }
1174+
1175+ public function problemBadge (ContestProblem $ problem , bool $ grayedOut = false ): string
1176+ {
1177+ $ rgb = Utils::convertToHex ($ problem ->getColor () ?? '#ffffff ' );
1178+ if ($ grayedOut || empty ($ rgb )) {
1179+ $ rgb = Utils::convertToHex ('whitesmoke ' );
1180+ }
1181+
1182+ [$ foreground , $ border ] = $ this ->hexToForegroundAndBorder ($ rgb );
1183+
11641184 if ($ grayedOut ) {
11651185 $ foreground = 'silver ' ;
11661186 $ border = 'linen ' ;
@@ -1180,17 +1200,9 @@ public function problemBadgeMaybe(ContestProblem $problem, ScoreboardMatrixItem
11801200 if (!$ matrixItem ->isCorrect || empty ($ rgb )) {
11811201 $ rgb = Utils::convertToHex ('whitesmoke ' );
11821202 }
1183- $ background = Utils::parseHexColor ($ rgb );
11841203
1185- // Pick a border that's a bit darker.
1186- $ darker = $ background ;
1187- $ darker [0 ] = max ($ darker [0 ] - 64 , 0 );
1188- $ darker [1 ] = max ($ darker [1 ] - 64 , 0 );
1189- $ darker [2 ] = max ($ darker [2 ] - 64 , 0 );
1190- $ border = Utils::rgbToHex ($ darker );
1204+ [$ foreground , $ border ] = $ this ->hexToForegroundAndBorder ($ rgb );
11911205
1192- // Pick the foreground text color based on the background color.
1193- $ foreground = ($ background [0 ] + $ background [1 ] + $ background [2 ] > 450 ) ? '#000000 ' : '#ffffff ' ;
11941206 if (!$ matrixItem ->isCorrect ) {
11951207 $ foreground = 'silver ' ;
11961208 $ border = 'linen ' ;
0 commit comments