darkings_Obsidian-MonokaiSy.../scripts/check-contrast.js

56 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const pairs = [
["深色正文", "#f8f8f2", "#272822", 4.5],
["深色链接", "#66d9ef", "#272822", 4.5],
["深色图标", "#c1c1bd", "#1e1f1c", 3],
["深色高亮文本", "#f8f8f2", "#5b542f", 4.5],
["浅色正文", "#3d3d3d", "#fdf9f3", 4.5],
["浅色链接", "#14748a", "#fdf9f3", 4.5],
["浅色图标", "#666666", "#f5f0e6", 3],
["浅色高亮文本", "#3d3d3d", "#f7e7b3", 4.5],
];
function toRgb(hex) {
const value = hex.replace("#", "");
return [
Number.parseInt(value.slice(0, 2), 16),
Number.parseInt(value.slice(2, 4), 16),
Number.parseInt(value.slice(4, 6), 16),
];
}
function channel(value) {
const normalized = value / 255;
return normalized <= 0.03928
? normalized / 12.92
: ((normalized + 0.055) / 1.055) ** 2.4;
}
function luminance(hex) {
const [red, green, blue] = toRgb(hex).map(channel);
return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
}
function contrast(foreground, background) {
const foregroundLuminance = luminance(foreground);
const backgroundLuminance = luminance(background);
const lighter = Math.max(foregroundLuminance, backgroundLuminance);
const darker = Math.min(foregroundLuminance, backgroundLuminance);
return (lighter + 0.05) / (darker + 0.05);
}
let hasFailure = false;
for (const [label, foreground, background, minimum] of pairs) {
const ratio = contrast(foreground, background);
const passed = ratio >= minimum;
const formattedRatio = ratio.toFixed(2);
console.log(`${label}: ${formattedRatio}:1要求 >= ${minimum}:1`);
if (!passed) {
hasFailure = true;
}
}
if (hasFailure) {
process.exitCode = 1;
}