Skip to content

Commit de5d3f7

Browse files
committed
change tag colors based on color mode
1 parent 554712c commit de5d3f7

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/components/IssuesList/index.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
import Emoji from "react-emoji-render"
22
import {
33
Avatar,
4+
ColorMode,
45
Flex,
56
HStack,
67
SimpleGrid,
78
SimpleGridProps,
89
Stack,
910
Text,
11+
useColorMode,
1012
} from "@chakra-ui/react"
1113

1214
import type { GHIssue } from "@/lib/types"
1315

16+
import { getContrastYIQ } from "@/lib/utils/getContrastYIQ"
17+
1418
import InlineLink from "../Link"
1519
import Tag from "../Tag"
1620

1721
type IssuesListProps = SimpleGridProps & {
1822
issues: GHIssue[]
1923
}
2024

25+
function pickTagColorsByColorMode(color: string, colorMode: ColorMode) {
26+
if (colorMode === "dark") {
27+
return {
28+
color: `#${color}`,
29+
bgColor: `#${color}22`,
30+
}
31+
} else {
32+
return {
33+
color: getContrastYIQ(`#${color}`),
34+
bgColor: `#${color}`,
35+
}
36+
}
37+
}
38+
2139
const IssuesList = ({ issues, ...props }: IssuesListProps) => {
40+
const { colorMode } = useColorMode()
41+
2242
return (
2343
<SimpleGrid columns={[1, null, 2]} spacing={4} {...props}>
2444
{issues.map((issue) => (
@@ -51,8 +71,7 @@ const IssuesList = ({ issues, ...props }: IssuesListProps) => {
5171
<Tag
5272
key={label.id}
5373
label={<Emoji text={label.name} />}
54-
color={`#${label.color}`}
55-
bgColor={`#${label.color}22`}
74+
{...pickTagColorsByColorMode(label.color, colorMode)}
5675
/>
5776
)
5877
})}

src/lib/utils/getContrastYIQ.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const getContrastYIQ = (hexcolor: string): "black" | "white" => {
2+
// Convert hex to RGB first
3+
let r = 0,
4+
g = 0,
5+
b = 0
6+
if (hexcolor.length == 4) {
7+
r = parseInt(hexcolor[1] + hexcolor[1], 16)
8+
g = parseInt(hexcolor[2] + hexcolor[2], 16)
9+
b = parseInt(hexcolor[3] + hexcolor[3], 16)
10+
} else if (hexcolor.length == 7) {
11+
r = parseInt(hexcolor[1] + hexcolor[2], 16)
12+
g = parseInt(hexcolor[3] + hexcolor[4], 16)
13+
b = parseInt(hexcolor[5] + hexcolor[6], 16)
14+
}
15+
16+
// Calculate luminance
17+
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
18+
19+
// Return black for bright colors, white for dark colors
20+
return luminance > 0.5 ? "black" : "white"
21+
}

0 commit comments

Comments
 (0)