|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React, { useState, useEffect } from 'react' |
| 4 | +import { Palette, Info, CheckCircle2, AlertCircle, RefreshCw } from 'lucide-react' |
| 5 | + |
| 6 | +export default function ColorContrast() { |
| 7 | + const [foreground, setForeground] = useState('#2563EB') |
| 8 | + const [background, setBackground] = useState('#FFFFFF') |
| 9 | + const [ratio, setRatio] = useState(0) |
| 10 | + const [results, setResults] = useState({ |
| 11 | + aaNormal: false, |
| 12 | + aaLarge: false, |
| 13 | + aaaNormal: false, |
| 14 | + aaaLarge: false |
| 15 | + }) |
| 16 | + |
| 17 | + const getLuminance = (hex: string) => { |
| 18 | + const rgb = hex.replace(/^#/, '').match(/.{2}/g)?.map(x => { |
| 19 | + let v = parseInt(x, 16) / 255 |
| 20 | + return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4) |
| 21 | + }) || [0, 0, 0] |
| 22 | + return 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2] |
| 23 | + } |
| 24 | + |
| 25 | + const calculateContrast = () => { |
| 26 | + const l1 = getLuminance(foreground) |
| 27 | + const l2 = getLuminance(background) |
| 28 | + const lighter = Math.max(l1, l2) |
| 29 | + const darker = Math.min(l1, l2) |
| 30 | + const currentRatio = (lighter + 0.05) / (darker + 0.05) |
| 31 | + |
| 32 | + setRatio(Number(currentRatio.toFixed(2))) |
| 33 | + setResults({ |
| 34 | + aaNormal: currentRatio >= 4.5, |
| 35 | + aaLarge: currentRatio >= 3, |
| 36 | + aaaNormal: currentRatio >= 7, |
| 37 | + aaaLarge: currentRatio >= 4.5 |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + calculateContrast() |
| 43 | + }, [foreground, background]) |
| 44 | + |
| 45 | + const ResultCard = ({ title, passed, sub }: { title: string, passed: boolean, sub: string }) => ( |
| 46 | + <div className={`p-4 rounded-xl border-2 flex items-center justify-between ${passed ? 'border-green-100 bg-green-50 text-green-800' : 'border-red-100 bg-red-50 text-red-800'}`}> |
| 47 | + <div> |
| 48 | + <div className="font-bold text-sm">{title}</div> |
| 49 | + <div className="text-xs opacity-75">{sub}</div> |
| 50 | + </div> |
| 51 | + {passed ? <CheckCircle2 className="w-6 h-6" /> : <AlertCircle className="w-6 h-6" />} |
| 52 | + </div> |
| 53 | + ) |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className="py-12 px-4 sm:px-6 lg:px-8 bg-gray-50/50 min-h-screen font-sans"> |
| 57 | + <div className="max-w-4xl mx-auto"> |
| 58 | + <div className="text-center mb-12"> |
| 59 | + <div className="inline-flex items-center justify-center p-3 bg-pink-100 rounded-2xl mb-4"> |
| 60 | + <Palette className="w-8 h-8 text-pink-600" /> |
| 61 | + </div> |
| 62 | + <h1 className="text-4xl font-extrabold text-gray-900 mb-4 tracking-tight"> |
| 63 | + Color Contrast Checker |
| 64 | + </h1> |
| 65 | + <p className="text-lg text-gray-500 max-w-2xl mx-auto"> |
| 66 | + Ensure your US-based enterprise application meets WCAG accessibility standards for high-readability and professional design. |
| 67 | + </p> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div className="grid grid-cols-1 lg:grid-cols-2 gap-8"> |
| 71 | + {/* Inputs */} |
| 72 | + <div className="bg-white rounded-2xl border border-gray-100 p-8 shadow-sm"> |
| 73 | + <h2 className="text-xl font-bold text-gray-900 mb-6 flex items-center gap-2"> |
| 74 | + <RefreshCw className="w-5 h-5 text-pink-500" /> |
| 75 | + Test Colors |
| 76 | + </h2> |
| 77 | + |
| 78 | + <div className="space-y-6"> |
| 79 | + <div> |
| 80 | + <label className="block text-sm font-semibold text-gray-700 mb-2">Foreground (Text) Color</label> |
| 81 | + <div className="flex gap-2"> |
| 82 | + <input |
| 83 | + type="color" |
| 84 | + value={foreground} |
| 85 | + onChange={(e) => setForeground(e.target.value.toUpperCase())} |
| 86 | + className="w-12 h-12 p-1 rounded-lg border border-gray-200 cursor-pointer" |
| 87 | + /> |
| 88 | + <input |
| 89 | + type="text" |
| 90 | + value={foreground} |
| 91 | + onChange={(e) => setForeground(e.target.value.toUpperCase())} |
| 92 | + className="flex-grow px-4 py-2 rounded-xl border border-gray-200 focus:ring-2 focus:ring-pink-500 outline-none font-mono" |
| 93 | + /> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div> |
| 98 | + <label className="block text-sm font-semibold text-gray-700 mb-2">Background Color</label> |
| 99 | + <div className="flex gap-2"> |
| 100 | + <input |
| 101 | + type="color" |
| 102 | + value={background} |
| 103 | + onChange={(e) => setBackground(e.target.value.toUpperCase())} |
| 104 | + className="w-12 h-12 p-1 rounded-lg border border-gray-200 cursor-pointer" |
| 105 | + /> |
| 106 | + <input |
| 107 | + type="text" |
| 108 | + value={background} |
| 109 | + onChange={(e) => setBackground(e.target.value.toUpperCase())} |
| 110 | + className="flex-grow px-4 py-2 rounded-xl border border-gray-200 focus:ring-2 focus:ring-pink-500 outline-none font-mono" |
| 111 | + /> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + |
| 115 | + {/* Preview Box */} |
| 116 | + <div |
| 117 | + className="mt-8 p-12 rounded-2xl border border-gray-200 flex flex-col items-center justify-center text-center transition-all shadow-inner" |
| 118 | + style={{ backgroundColor: background, color: foreground }} |
| 119 | + > |
| 120 | + <div className="text-3xl font-bold mb-2">Preview Text</div> |
| 121 | + <div className="text-sm">The quick brown fox jumps over the lazy dog.</div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + |
| 126 | + {/* Results */} |
| 127 | + <div className="bg-white rounded-2xl border border-gray-100 p-8 shadow-sm"> |
| 128 | + <div className="text-center mb-8 pb-8 border-b border-gray-50"> |
| 129 | + <div className="text-sm font-bold text-gray-400 uppercase tracking-widest mb-1">Contrast Ratio</div> |
| 130 | + <div className="text-6xl font-black text-gray-900">{ratio}:1</div> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div className="grid grid-cols-1 gap-4"> |
| 134 | + <ResultCard title="WCAG AA Normal" passed={results.aaNormal} sub="Required ratio: 4.5:1" /> |
| 135 | + <ResultCard title="WCAG AA Large" passed={results.aaLarge} sub="Required ratio: 3:1" /> |
| 136 | + <ResultCard title="WCAG AAA Normal" passed={results.aaaNormal} sub="Required ratio: 7:1" /> |
| 137 | + <ResultCard title="WCAG AAA Large" passed={results.aaaLarge} sub="Required ratio: 4.5:1" /> |
| 138 | + </div> |
| 139 | + |
| 140 | + <div className="mt-8 p-4 bg-blue-50 rounded-xl border border-blue-100 flex gap-3"> |
| 141 | + <Info className="w-5 h-5 text-blue-500 flex-shrink-0 mt-0.5" /> |
| 142 | + <p className="text-xs text-blue-700 leading-relaxed"> |
| 143 | + <strong>WCAG Standards:</strong> AA is the standard requirement for most business sites. Large text is defined as 18pt+ or 14pt+ bold. |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + ) |
| 151 | +} |
0 commit comments