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