Files
thechart/scripts/contrast_check.py

52 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""Calculate the exact contrast ratio for the new white header text."""
def calculate_contrast_ratio():
"""Calculate contrast ratio between dark background and white text."""
def get_luminance(color_str):
"""Calculate relative luminance of a color."""
if not color_str or not color_str.startswith("#"):
return 0.5
try:
rgb = tuple(int(color_str[i : i + 2], 16) for i in (1, 3, 5))
# Calculate relative luminance using sRGB formula
return (0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]) / 255
except (ValueError, IndexError):
return 0.5
# Our new header colors
header_bg = "#1e1e1e" # Very dark gray
header_fg = "#ffffff" # Pure white
bg_lum = get_luminance(header_bg)
fg_lum = get_luminance(header_fg)
# Calculate proper contrast ratio
lighter = max(bg_lum, fg_lum)
darker = min(bg_lum, fg_lum)
contrast_ratio = (lighter + 0.05) / (darker + 0.05)
print("=== HEADER CONTRAST ANALYSIS ===")
print(f"Background: {header_bg} (luminance: {bg_lum:.3f})")
print(f"Foreground: {header_fg} (luminance: {fg_lum:.3f})")
print(f"Contrast ratio: {contrast_ratio:.2f}:1")
print()
# WCAG AA guidelines
if contrast_ratio >= 7.0:
print("✅ EXCELLENT contrast (WCAG AAA compliant)")
elif contrast_ratio >= 4.5:
print("✅ GOOD contrast (WCAG AA compliant)")
elif contrast_ratio >= 3.0:
print("⚠️ FAIR contrast (minimum acceptable)")
else:
print("❌ POOR contrast")
return contrast_ratio
if __name__ == "__main__":
calculate_contrast_ratio()