@@ -1144,12 +1144,11 @@ public function fileTypeIcon(string $type): string
1144
1144
return 'fas fa-file- ' . $ iconName ;
1145
1145
}
1146
1146
1147
- public function problemBadge (ContestProblem $ problem , bool $ grayedOut = false ): string
1147
+ /**
1148
+ * @return array{string, string}
1149
+ */
1150
+ private function hexToForegroundAndBorder (string $ rgb ): array
1148
1151
{
1149
- $ rgb = Utils::convertToHex ($ problem ->getColor () ?? '#ffffff ' );
1150
- if ($ grayedOut || empty ($ rgb )) {
1151
- $ rgb = Utils::convertToHex ('whitesmoke ' );
1152
- }
1153
1152
$ background = Utils::parseHexColor ($ rgb );
1154
1153
1155
1154
// Pick a border that's a bit darker.
@@ -1159,8 +1158,29 @@ public function problemBadge(ContestProblem $problem, bool $grayedOut = false):
1159
1158
$ darker [2 ] = max ($ darker [2 ] - 64 , 0 );
1160
1159
$ border = Utils::rgbToHex ($ darker );
1161
1160
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
+
1164
1184
if ($ grayedOut ) {
1165
1185
$ foreground = 'silver ' ;
1166
1186
$ border = 'linen ' ;
@@ -1180,17 +1200,9 @@ public function problemBadgeMaybe(ContestProblem $problem, ScoreboardMatrixItem
1180
1200
if (!$ matrixItem ->isCorrect || empty ($ rgb )) {
1181
1201
$ rgb = Utils::convertToHex ('whitesmoke ' );
1182
1202
}
1183
- $ background = Utils::parseHexColor ($ rgb );
1184
1203
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 );
1191
1205
1192
- // Pick the foreground text color based on the background color.
1193
- $ foreground = ($ background [0 ] + $ background [1 ] + $ background [2 ] > 450 ) ? '#000000 ' : '#ffffff ' ;
1194
1206
if (!$ matrixItem ->isCorrect ) {
1195
1207
$ foreground = 'silver ' ;
1196
1208
$ border = 'linen ' ;
0 commit comments