-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorContrastChecker.html
More file actions
398 lines (349 loc) · 11.9 KB
/
Copy pathColorContrastChecker.html
File metadata and controls
398 lines (349 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>颜色对比度检测 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.checker-container {
max-width: 800px;
margin: 0 auto;
}
.color-inputs {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin: 20px 0;
}
.color-input-group {
flex: 1;
min-width: 250px;
display: flex;
flex-direction: column;
align-items: center;
}
.color-preview {
width: 150px;
height: 100px;
border-radius: var(--border-radius);
border: 1px solid var(--bg-tertiary);
margin: 10px 0;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
color: white;
text-shadow: 1px 1px 2px rgba(0,0,0,0.7);
}
.contrast-ratio {
font-size: 2em;
text-align: center;
margin: 20px 0;
padding: 15px;
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
min-height: 80px;
display: flex;
flex-direction: column;
justify-content: center;
}
.ratio-value {
color: var(--accent-color);
font-weight: bold;
}
.wcag-levels {
display: flex;
justify-content: space-around;
margin: 20px 0;
flex-wrap: wrap;
}
.level-item {
text-align: center;
padding: 10px;
border-radius: var(--border-radius);
background-color: var(--bg-secondary);
min-width: 100px;
margin: 5px;
}
.level-pass {
background-color: rgba(74, 194, 131, 0.2);
border: 1px solid #4ade80;
}
.level-fail {
background-color: rgba(239, 68, 68, 0.2);
border: 1px solid #f87171;
}
.guidelines {
margin: 20px 0;
padding: 15px;
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
}
.sample-texts {
margin: 20px 0;
}
.sample-text {
padding: 15px;
border-radius: var(--border-radius);
margin: 10px 0;
font-size: 1.2em;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
justify-content: center;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>颜色对比度检测</h2>
<p>检查两种颜色之间的对比度是否符合无障碍标准</p>
<div class="info-box">
<p><strong>WCAG对比度标准:</strong></p>
<p>• AA级标准:正常文本≥4.5:1,大文本≥3:1</p>
<p>• AAA级标准:正常文本≥7:1,大文本≥4.5:1</p>
</div>
<div class="checker-container">
<div class="color-inputs">
<div class="color-input-group">
<label for="foregroundColor">前景色 (文字色)</label>
<input
type="color"
id="foregroundColor"
v-model="foregroundColor"
@input="calculateContrast"
>
<div
class="color-preview"
:style="{ backgroundColor: foregroundColor }"
>
{{ foregroundColor.toUpperCase() }}
</div>
<input
type="text"
v-model="foregroundColor"
@input="validateColor('foreground')"
>
</div>
<div class="color-input-group">
<label for="backgroundColor">背景色</label>
<input
type="color"
id="backgroundColor"
v-model="backgroundColor"
@input="calculateContrast"
>
<div
class="color-preview"
:style="{ backgroundColor: backgroundColor }"
>
{{ backgroundColor.toUpperCase() }}
</div>
<input
type="text"
v-model="backgroundColor"
@input="validateColor('background')"
>
</div>
</div>
<div class="controls">
<button @click="swapColors">交换颜色</button>
<button @click="randomColors">随机颜色</button>
<button @click="resetColors">重置颜色</button>
</div>
<div class="contrast-ratio">
<div>对比度比例</div>
<div class="ratio-value">{{ contrastRatio.toFixed(2) }}:1</div>
</div>
<div class="wcag-levels">
<div class="level-item" :class="normalTextAA ? 'level-pass' : 'level-fail'">
<div>AA级 (普通)</div>
<div>{{ normalTextAA ? '通过' : '不通过' }}</div>
</div>
<div class="level-item" :class="largeTextAA ? 'level-pass' : 'level-fail'">
<div>AA级 (大字)</div>
<div>{{ largeTextAA ? '通过' : '不通过' }}</div>
</div>
<div class="level-item" :class="normalTextAAA ? 'level-pass' : 'level-fail'">
<div>AAA级 (普通)</div>
<div>{{ normalTextAAA ? '通过' : '不通过' }}</div>
</div>
<div class="level-item" :class="largeTextAAA ? 'level-pass' : 'level-fail'">
<div>AAA级 (大字)</div>
<div>{{ largeTextAAA ? '通过' : '不通过' }}</div>
</div>
</div>
<div class="guidelines">
<h3>对比度指南</h3>
<ul>
<li><strong>低对比度:</strong>小于3:1 - 不推荐用于正文</li>
<li><strong>最小对比度:</strong>3:1 - 用于大文本</li>
<li><strong>增强对比度:</strong>4.5:1 - 用于普通文本或低视力用户</li>
<li><strong>高级对比度:</strong>7:1 - 用于更佳可读性</li>
</ul>
</div>
<div class="sample-texts">
<h3>示例文本</h3>
<div
class="sample-text"
:style="{ backgroundColor: backgroundColor, color: foregroundColor }"
>
这是一段示例文本,用于预览所选颜色的可读性效果。这段文字可以帮助您直观地感受颜色搭配的实际效果。
</div>
<div
class="sample-text"
:style="{ backgroundColor: backgroundColor, color: foregroundColor, fontSize: '1.5em' }"
>
这是大号字体的示例文本,字号更大,更容易阅读。
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const foregroundColor = ref('#000000');
const backgroundColor = ref('#FFFFFF');
// 计算相对亮度
const getRelativeLuminance = (color) => {
// 将十六进制颜色转换为RGB
const r = parseInt(color.substr(1, 2), 16) / 255;
const g = parseInt(color.substr(3, 2), 16) / 255;
const b = parseInt(color.substr(5, 2), 16) / 255;
// 转换为相对亮度
const rs = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
const gs = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
const bs = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
};
// 计算对比度比率
const calculateContrast = () => {
const lum1 = getRelativeLuminance(foregroundColor.value);
const lum2 = getRelativeLuminance(backgroundColor.value);
// 确保较亮的颜色在分子位置
const brightest = Math.max(lum1, lum2);
const darkest = Math.min(lum1, lum2);
return (brightest + 0.05) / (darkest + 0.05);
};
// 计算对比度比率
const contrastRatio = computed(() => {
return calculateContrast();
});
// 检查是否符合各种WCAG标准
const normalTextAA = computed(() => {
return contrastRatio.value >= 4.5;
});
const largeTextAA = computed(() => {
return contrastRatio.value >= 3.0;
});
const normalTextAAA = computed(() => {
return contrastRatio.value >= 7.0;
});
const largeTextAAA = computed(() => {
return contrastRatio.value >= 4.5;
});
// 交换颜色
const swapColors = () => {
const temp = foregroundColor.value;
foregroundColor.value = backgroundColor.value;
backgroundColor.value = temp;
};
// 随机颜色
const randomColors = () => {
foregroundColor.value = '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0');
backgroundColor.value = '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0');
};
// 重置颜色
const resetColors = () => {
foregroundColor.value = '#000000';
backgroundColor.value = '#FFFFFF';
};
// 验证颜色输入
const validateColor = (type) => {
const color = type === 'foreground' ? foregroundColor.value : backgroundColor.value;
// 确保颜色值是有效的十六进制格式
if (!/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(color)) {
// 如果格式不正确,保持之前的值
return;
}
};
return {
foregroundColor,
backgroundColor,
contrastRatio,
normalTextAA,
largeTextAA,
normalTextAAA,
largeTextAAA,
swapColors,
randomColors,
resetColors,
validateColor
};
}
}).mount('#app');
</script>
</body>
</html>