Skip to content

Commit db2b652

Browse files
committed
Fix contrast
1 parent 5575fb9 commit db2b652

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

src/components/snippet/TagComp.tsx

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,67 @@ export default function TagComp({ tag }: { tag: Tag }) {
88
for (let i = 0; i < id.length; i++) {
99
hash = id.charCodeAt(i) + ((hash << 5) - hash)
1010
}
11+
1112
const hue = hash % 360
12-
const saturation = 70 + (hash % 30)
13-
const lightness = 50 + (hash % 20)
14-
return `hsl(${hue}, ${saturation}%, ${lightness}%)`
13+
14+
const saturation = 60 + (hash % 20)
15+
16+
const lightness = 75 + (hash % 15)
17+
18+
const h = hue / 360
19+
const s = saturation / 100
20+
const l = lightness / 100
21+
22+
const hslToRgb = (h: number, s: number, l: number) => {
23+
let r, g, b
24+
if (s === 0) {
25+
r = g = b = l
26+
} else {
27+
const hue2rgb = (p: number, q: number, t: number) => {
28+
if (t < 0) t += 1
29+
if (t > 1) t -= 1
30+
if (t < 1 / 6) return p + (q - p) * 6 * t
31+
if (t < 1 / 2) return q
32+
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
33+
return p
34+
}
35+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s
36+
const p = 2 * l - q
37+
r = hue2rgb(p, q, h + 1 / 3)
38+
g = hue2rgb(p, q, h)
39+
b = hue2rgb(p, q, h - 1 / 3)
40+
}
41+
return [r * 255, g * 255, b * 255]
42+
}
43+
44+
const [r, g, b] = hslToRgb(h, s, l)
45+
46+
const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255
47+
48+
const isDarkText = luminance > 0.5
49+
50+
return {
51+
bg: `hsl(${hue}, ${saturation}%, ${lightness}%)`,
52+
text: isDarkText ? 'text-gray-900' : 'text-white',
53+
textSecondary: isDarkText ? 'text-gray-700' : 'text-gray-200',
54+
}
1555
}
1656

17-
const bgColor = calculatedColorsFromId(tag.id)
57+
const colors = calculatedColorsFromId(tag.id)
1858

1959
return (
2060
<>
2161
<Link to="/snippets" search={{ tags: [tag.id] }}>
2262
<div
2363
key={tag.id}
24-
style={{ backgroundColor: bgColor }}
25-
className="rounded px-2 py-1 text-xs hover:scale-101 transition-transform cursor-pointer h-full"
64+
style={{ backgroundColor: colors.bg }}
65+
className={`rounded px-2 py-1 text-xs hover:scale-101 transition-transform cursor-pointer h-full ${colors.text}`}
2666
>
27-
<div>{tag.name}</div>
28-
<div className="text-black/75">{tag.description}</div>
29-
<span className="inline-flex items-center gap-1 text-black/75 mt-1">
67+
<div className="font-medium">{tag.name}</div>
68+
<div className={colors.textSecondary}>{tag.description}</div>
69+
<span
70+
className={`inline-flex items-center gap-1 mt-1 ${colors.textSecondary}`}
71+
>
3072
<BookMarked size={14} /> {tag.numberOfSnippets ?? 0} Snippets
3173
</span>
3274
</div>

0 commit comments

Comments
 (0)