@@ -1148,12 +1148,11 @@ public function fileTypeIcon(string $type): string
1148
1148
return 'fas fa-file- ' . $ iconName ;
1149
1149
}
1150
1150
1151
- public function problemBadge (ContestProblem $ problem , bool $ grayedOut = false ): string
1151
+ /**
1152
+ * @return array{string, string}
1153
+ */
1154
+ private function hexToForegroundAndBorder (string $ rgb ): array
1152
1155
{
1153
- $ rgb = Utils::convertToHex ($ problem ->getColor () ?? '#ffffff ' );
1154
- if ($ grayedOut || empty ($ rgb )) {
1155
- $ rgb = Utils::convertToHex ('whitesmoke ' );
1156
- }
1157
1156
$ background = Utils::parseHexColor ($ rgb );
1158
1157
1159
1158
// Pick a border that's a bit darker.
@@ -1163,8 +1162,29 @@ public function problemBadge(ContestProblem $problem, bool $grayedOut = false):
1163
1162
$ darker [2 ] = max ($ darker [2 ] - 64 , 0 );
1164
1163
$ border = Utils::rgbToHex ($ darker );
1165
1164
1166
- // Pick the foreground text color based on the background color.
1167
- $ foreground = ($ background [0 ] + $ background [1 ] + $ background [2 ] > 450 ) ? '#000000 ' : '#ffffff ' ;
1165
+ [$ r , $ g , $ b ] = $ background ;
1166
+
1167
+ // Calculate relative luminance
1168
+ $ r = ($ r / 255 <= 0.03928 ) ? ($ r / 255 ) / 12.92 : pow (($ r / 255 + 0.055 ) / 1.055 , 2.4 );
1169
+ $ g = ($ g / 255 <= 0.03928 ) ? ($ g / 255 ) / 12.92 : pow (($ g / 255 + 0.055 ) / 1.055 , 2.4 );
1170
+ $ b = ($ b / 255 <= 0.03928 ) ? ($ b / 255 ) / 12.92 : pow (($ b / 255 + 0.055 ) / 1.055 , 2.4 );
1171
+
1172
+ $ luminance = 0.2126 * $ r + 0.7152 * $ g + 0.0722 * $ b ;
1173
+
1174
+ $ foreground = ($ luminance > 0.179 ) ? '#000000 ' : '#FFFFFF ' ;
1175
+
1176
+ return [$ foreground , $ border ];
1177
+ }
1178
+
1179
+ public function problemBadge (ContestProblem $ problem , bool $ grayedOut = false ): string
1180
+ {
1181
+ $ rgb = Utils::convertToHex ($ problem ->getColor () ?? '#ffffff ' );
1182
+ if ($ grayedOut || empty ($ rgb )) {
1183
+ $ rgb = Utils::convertToHex ('whitesmoke ' );
1184
+ }
1185
+
1186
+ [$ foreground , $ border ] = $ this ->hexToForegroundAndBorder ($ rgb );
1187
+
1168
1188
if ($ grayedOut ) {
1169
1189
$ foreground = 'silver ' ;
1170
1190
$ border = 'linen ' ;
@@ -1184,17 +1204,9 @@ public function problemBadgeMaybe(ContestProblem $problem, ScoreboardMatrixItem
1184
1204
if (!$ matrixItem ->isCorrect || empty ($ rgb )) {
1185
1205
$ rgb = Utils::convertToHex ('whitesmoke ' );
1186
1206
}
1187
- $ background = Utils::parseHexColor ($ rgb );
1188
1207
1189
- // Pick a border that's a bit darker.
1190
- $ darker = $ background ;
1191
- $ darker [0 ] = max ($ darker [0 ] - 64 , 0 );
1192
- $ darker [1 ] = max ($ darker [1 ] - 64 , 0 );
1193
- $ darker [2 ] = max ($ darker [2 ] - 64 , 0 );
1194
- $ border = Utils::rgbToHex ($ darker );
1208
+ [$ foreground , $ border ] = $ this ->hexToForegroundAndBorder ($ rgb );
1195
1209
1196
- // Pick the foreground text color based on the background color.
1197
- $ foreground = ($ background [0 ] + $ background [1 ] + $ background [2 ] > 450 ) ? '#000000 ' : '#ffffff ' ;
1198
1210
if (!$ matrixItem ->isCorrect ) {
1199
1211
$ foreground = 'silver ' ;
1200
1212
$ border = 'linen ' ;
0 commit comments