304 lines · 11.5 KB
Raw Download
1
<?php
2
// report_template_advanced.php
3
// This template expects $light_mode_bg_colors, $light_mode_fg_colors, $dark_mode_bg_colors, $dark_mode_fg_colors, and $contrast_method to be available from the calling context
4
if (!isset($light_mode_bg_colors) && !isset($dark_mode_bg_colors)) {
5
	die('Error: No color data provided');
6
}
7
8
$current_method = $contrast_method ?? 'wcag';
9
10
// Helper function to render a results section
11
function renderResultsSection($bg_colors, $fg_colors, $current_method, $section_title) {
12
	?>
13
	<h2><?= htmlspecialchars($section_title) ?></h2>
14
	<h3>Summary of Compatible Color Combinations (<?= $current_method === 'both' ? 'Both' : strtoupper($current_method) ?>)</h3>
15
	<table class="checkered">
16
		<thead>
17
			<tr>
18
				<th>Background</th>
19
				<?php if ($current_method === 'wcag'): ?>
20
					<th>AAA ≥ 7.0<br>Best</th>
21
					<th>AA Normal ≥ 4.5<br>Second Best</th>
22
					<th>AA Large ≥ 3.0<br>Third Best</th>
23
				<?php elseif ($current_method === 'apca'): ?>
24
					<th>Perfect ≥ 90</th>
25
					<th>Excellent ≥ 75</th>
26
					<th>Good ≥ 60</th>
27
					<th>Fair ≥ 45</th>
28
				<?php else: // both ?>
29
					<th>Perfect<br>WCAG&nbsp;≥10&nbsp;&<br>APCA&nbsp;≥90</th>
30
					<th>Excellent<br>WCAG&nbsp;≥7&nbsp;&<br>APCA&nbsp;≥75</th>
31
					<th>Good<br>WCAG&nbsp;≥4.5&nbsp;&<br>APCA&nbsp;≥60</th>
32
					<th>Fair<br>WCAG&nbsp;≥3&nbsp;&<br>APCA&nbsp;≥45</th>
33
				<?php endif; ?>
34
			</tr>
35
		</thead>
36
		<tbody>
37
			<?php foreach ($bg_colors as $bg_color => $bg): 
38
				// Calculate all combinations for this background color
39
				$combinations = [];
40
				foreach ($fg_colors as $fg_color => $fg) {
41
					
42
					if ($current_method === 'wcag') {
43
						// Calculate luminance with alpha blending
44
						$fg_lum = getLuminance($fg['rgb'], $fg['alpha'], $bg['rgb']);
45
						$bg_lum = $bg['luminance'];
46
						$contrast = getContrastRatio($bg_lum, $fg_lum);
47
						$level = getWCAGLevel($contrast);
48
						
49
						if ($level !== 'Fail') {
50
							$combinations[] = [
51
								'color' => $fg_color,
52
								'contrast' => $contrast,
53
								'level' => $level
54
							];
55
						}
56
					} elseif ($current_method === 'apca') {
57
						// APCA calculations
58
						$contrast = getAPCAContrast($bg['rgb'], $fg['rgb'], $bg['alpha'], $fg['alpha']);
59
						$level = getAPCALevel($contrast);
60
						
61
						if (!str_contains($level, 'Fail')) {
62
							$combinations[] = [
63
								'color' => $fg_color,
64
								'contrast' => $contrast,
65
								'level' => $level
66
							];
67
						}
68
					} else { // both
69
						// Calculate both WCAG and APCA
70
						$fg_lum = getLuminance($fg['rgb'], $fg['alpha'], $bg['rgb']);
71
						$bg_lum = $bg['luminance'];
72
						$wcag_contrast = getContrastRatio($bg_lum, $fg_lum);
73
						$wcag_level = getWCAGLevel($wcag_contrast);
74
						
75
						$apca_contrast = getAPCAContrast($bg['rgb'], $fg['rgb'], $bg['alpha'], $fg['alpha']);
76
						$apca_level = getAPCALevel($apca_contrast);
77
						
78
						$combined_level = getCombinedLevel($wcag_contrast, $apca_contrast);
79
						
80
						if ($combined_level !== 'Fail') {
81
							$combinations[] = [
82
								'color' => $fg_color,
83
								'wcag_contrast' => $wcag_contrast,
84
								'apca_contrast' => $apca_contrast,
85
								'level' => $combined_level
86
							];
87
						}
88
					}
89
				}
90
91
				if ($current_method === 'wcag') {
92
					// Group by WCAG level
93
					$wcag_groups = [
94
						'AAA' => [],
95
						'AA' => [],
96
						'AA Large' => []
97
					];
98
					
99
					foreach ($combinations as $combo) {
100
						$wcag_groups[$combo['level']][] = $combo;
101
					}
102
					
103
					// Sort each group by contrast ratio
104
					foreach ($wcag_groups as &$group) {
105
						usort($group, function($a, $b) {
106
							return $b['contrast'] <=> $a['contrast'];
107
						});
108
					}
109
110
					$has_valid_combinations = !empty($wcag_groups['AAA']) || 
111
										   !empty($wcag_groups['AA']) || 
112
										   !empty($wcag_groups['AA Large']);
113
				} elseif ($current_method === 'apca') {
114
					// Group by APCA level
115
					$apca_groups = [
116
						'Perfect' => [],
117
						'Excellent' => [],
118
						'Good' => [],
119
						'Fair' => []
120
					];
121
					
122
					foreach ($combinations as $combo) {
123
						$level_key = explode(' - ', $combo['level'])[0];
124
						if (isset($apca_groups[$level_key])) {
125
							$apca_groups[$level_key][] = $combo;
126
						}
127
					}
128
					
129
					// Sort each group by absolute contrast value
130
					foreach ($apca_groups as &$group) {
131
						usort($group, function($a, $b) {
132
							return abs($b['contrast']) <=> abs($a['contrast']);
133
						});
134
					}
135
136
					$has_valid_combinations = !empty($apca_groups['Perfect']) || 
137
										   !empty($apca_groups['Excellent']) || 
138
										   !empty($apca_groups['Good']) || 
139
										   !empty($apca_groups['Fair']);
140
				} else { // both
141
					// Group by combined level
142
					$combined_groups = [
143
						'Perfect' => [],
144
						'Excellent' => [],
145
						'Good' => [],
146
						'Fair' => []
147
					];
148
					
149
					foreach ($combinations as $combo) {
150
						if (isset($combined_groups[$combo['level']])) {
151
							$combined_groups[$combo['level']][] = $combo;
152
						}
153
					}
154
					
155
					// Sort each group by WCAG contrast ratio
156
					foreach ($combined_groups as &$group) {
157
						usort($group, function($a, $b) {
158
							return $b['wcag_contrast'] <=> $a['wcag_contrast'];
159
						});
160
					}
161
162
					$has_valid_combinations = !empty($combined_groups['Perfect']) || 
163
											!empty($combined_groups['Excellent']) || 
164
											!empty($combined_groups['Good']) || 
165
											!empty($combined_groups['Fair']);
166
				}
167
			?>
168
			<tr style="background-color: <?= htmlspecialchars(getCssColor($bg_color)) ?>;">
169
				<td>
170
					<?php 
171
					// Use alpha-aware luminance for text color calculation
172
					$bg_text_lum = getLuminance($bg['rgb'], $bg['alpha']); ?>
173
					<span style="color: <?= $bg_text_lum > 0.5 ? '#000000' : '#FFFFFF' ?>">
174
						<?= htmlspecialchars(getCleanColorName($bg_color)) ?>
175
					</span>
176
				</td>
177
				<?php if ($has_valid_combinations): ?>
178
					<?php if ($current_method === 'wcag'): ?>
179
						<?php foreach (['AAA', 'AA', 'AA Large'] as $level): ?>
180
							<td><?php foreach ($wcag_groups[$level] as $combo): ?>
181
								<div style="color: <?= htmlspecialchars(getCssColor($combo['color'])) ?>;"><?= htmlspecialchars($combo['color']) ?>&nbsp;(Ratio:&nbsp;<?= number_format($combo['contrast'], 2) ?>)</div>
182
							<?php endforeach; ?></td>
183
						<?php endforeach; ?>
184
					<?php elseif ($current_method === 'apca'): ?>
185
						<?php foreach (['Perfect', 'Excellent', 'Good', 'Fair'] as $level): ?>
186
							<td><?php foreach ($apca_groups[$level] as $combo): ?>
187
								<div style="color: <?= htmlspecialchars(getCssColor($combo['color'])) ?>;"><?= htmlspecialchars($combo['color']) ?>&nbsp;(Lc:&nbsp;<?= number_format($combo['contrast'], 1) ?>)</div>
188
							<?php endforeach; ?></td>
189
						<?php endforeach; ?>
190
					<?php else: // both ?>
191
						<?php foreach (['Perfect', 'Excellent', 'Good', 'Fair'] as $level): ?>
192
							<td><?php foreach ($combined_groups[$level] as $combo): ?>
193
								<div style="color: <?= htmlspecialchars(getCssColor($combo['color'])) ?>;"><?= htmlspecialchars($combo['color']) ?>&nbsp;(<?= number_format($combo['wcag_contrast'], 2) ?>)&nbsp;(Lc&nbsp;<?= number_format($combo['apca_contrast'], 1) ?>)</div>
194
							<?php endforeach; ?></td>
195
						<?php endforeach; ?>
196
					<?php endif; ?>
197
				<?php else: ?>
198
					<td colspan="<?= $current_method === 'wcag' ? '3' : '4' ?>" style="text-align: center; color: <?= $bg_text_lum > 0.5 ? '#000000' : '#FFFFFF' ?>">
199
						<?php if ($current_method === 'wcag'): ?>
200
							No valid color combinations found (all contrast ratios below 3.0)
201
						<?php elseif ($current_method === 'apca'): ?>
202
							No valid color combinations found (all APCA values below 45)
203
						<?php else: ?>
204
							No valid color combinations found (failed both WCAG and APCA thresholds)
205
						<?php endif; ?>
206
					</td>
207
				<?php endif; ?>
208
			</tr>
209
			<?php endforeach; ?>
210
		</tbody>
211
	</table>
212
213
	<h3>Complete Contrast Grid (<?= $current_method === 'both' ? 'Both' : strtoupper($current_method) ?>)</h3>
214
	<table class="checkered">
215
		<tr>
216
			<th>BG \ FG</th>
217
			<?php foreach ($fg_colors as $color => $data): ?>
218
				<th><?= htmlspecialchars(getCleanColorName($color)) ?></th>
219
			<?php endforeach; ?>
220
		</tr>
221
		<?php foreach ($bg_colors as $bg_color => $bg_data): ?>
222
			<tr>
223
				<th><?= htmlspecialchars(getCleanColorName($bg_color)) ?></th>
224
				<?php foreach ($fg_colors as $fg_color => $fg_data): 
225
					if ($current_method === 'wcag') {
226
						// Calculate luminance with alpha blending
227
						$fg_lum = getLuminance($fg_data['rgb'], $fg_data['alpha'], $bg_data['rgb']);
228
						$bg_lum = $bg_data['luminance'];
229
						$contrast = getContrastRatio($bg_lum, $fg_lum);
230
						$level = getWCAGLevel($contrast);
231
						$display_value = number_format($contrast, 2);
232
					} elseif ($current_method === 'apca') {
233
						// APCA calculations
234
						$contrast = getAPCAContrast($bg_data['rgb'], $fg_data['rgb'], $bg_data['alpha'], $fg_data['alpha']);
235
						$level = getAPCALevel($contrast);
236
						$display_value = number_format($contrast, 1);
237
					} else { // both
238
						// Calculate both WCAG and APCA
239
						$fg_lum = getLuminance($fg_data['rgb'], $fg_data['alpha'], $bg_data['rgb']);
240
						$bg_lum = $bg_data['luminance'];
241
						$wcag_contrast = getContrastRatio($bg_lum, $fg_lum);
242
						$wcag_level = getWCAGLevel($wcag_contrast);
243
						
244
						$apca_contrast = getAPCAContrast($bg_data['rgb'], $fg_data['rgb'], $bg_data['alpha'], $fg_data['alpha']);
245
						$apca_level = getAPCALevel($apca_contrast);
246
						
247
						$combined_level = getCombinedLevel($wcag_contrast, $apca_contrast);
248
					}
249
				?>
250
					<td style="background-color: <?= htmlspecialchars(getCssColor($bg_color)) ?>;">
251
						<div class="sample-text" style="color: <?= htmlspecialchars(getCssColor($fg_color)) ?>;">
252
							Sample
253
							<div style="font-size: 0.8em;">
254
								<?php if ($current_method === 'wcag'): ?>
255
									<?= $display_value ?><br><?= $level ?>
256
								<?php elseif ($current_method === 'apca'): ?>
257
									Lc&nbsp;<?= $display_value ?><br><?= explode(' - ', $level)[0] ?>
258
								<?php else: // both ?>
259
									(<?= number_format($wcag_contrast, 2) ?>) (Lc&nbsp;<?= number_format($apca_contrast, 1) ?>)<br><?= $combined_level ?>
260
								<?php endif; ?>
261
							</div>
262
						</div>
263
					</td>
264
				<?php endforeach; ?>
265
			</tr>
266
		<?php endforeach; ?>
267
	</table>
268
	<?php
269
}
270
?>
271
<!DOCTYPE html>
272
<html lang="en">
273
<head>
274
	<meta charset="UTF-8">
275
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
276
	<title>Jeff's Color Contrast Analyzer Report - Advanced Mode (<?= $current_method === 'both' ? 'Both' : strtoupper($current_method) ?>) - <?= date('Y-m-d H:i:s') ?></title>
277
	<style>
278
		:root { color-scheme: light dark; }
279
		body { font-family: Arial, sans-serif; margin: 20px; }
280
		table { border-collapse: collapse; margin: 20px 0; }
281
		th, td { border: 1px solid #000; padding: 8px; text-align: left; vertical-align: top; }
282
		td div { padding: 0 0.5em 0.5em 0; }
283
		.checkered { background: conic-gradient(hsla(0, 0%, 50%, 20%) 90deg, transparent 90deg 180deg, hsla(0, 0%, 50%, 20%) 180deg 270deg, transparent 270deg); background-repeat: repeat; background-size: 40px 40px; }
284
		.footer { padding-top: 1em; }
285
	</style>
286
</head>
287
<body>
288
	<h1>Jeff's Color Contrast Analyzer - Advanced Mode Report</h1>
289
	<p>Generated on: <?= date('Y-m-d H:i:s T') ?> from <a href="https://contrast.jefftml.com/">contrast.jefftml.com</a></p>
290
	
291
	<?php if (!empty($light_mode_bg_colors)): ?>
292
		<?php renderResultsSection($light_mode_bg_colors, $light_mode_fg_colors ?? [], $current_method, 'Light Mode Results'); ?>
293
	<?php endif; ?>
294
	
295
	<?php if (!empty($dark_mode_bg_colors)): ?>
296
		<?php renderResultsSection($dark_mode_bg_colors, $dark_mode_fg_colors ?? [], $current_method, 'Dark Mode Results'); ?>
297
	<?php endif; ?>
298
	
299
	<div class="footer">
300
		<?php echo getCopyrightYears(2024); ?>
301
	</div>
302
</body>
303
</html>
304