Skip to content

Commit 9501ae2

Browse files
committed
make tag colors consistent and readable
1 parent e724bfb commit 9501ae2

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/components/Blog.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ interface IndividualBlogProps {
1111
alt: string;
1212
}
1313

14-
const getRandomColor = () => {
15-
const letters = "0123456789ABCDEF";
16-
let color = "#";
17-
for (let i = 0; i < 6; i++) {
18-
color += letters[Math.floor(Math.random() * 16)];
14+
const getColorFromString = (str: string) => {
15+
let hash = 0;
16+
for (let i = 0; i < str.length; i++) {
17+
hash = str.charCodeAt(i) + ((hash << 5) - hash);
1918
}
20-
return color;
19+
20+
// Generate HSL color with fixed saturation and lightness for better readability
21+
const h = Math.abs(hash % 360);
22+
// Using lightness of 85% for light backgrounds
23+
return `hsl(${h}, 70%, 85%)`;
2124
};
2225

2326
const IndividualBlog: React.FC<IndividualBlogProps> = ({
@@ -36,8 +39,8 @@ const IndividualBlog: React.FC<IndividualBlogProps> = ({
3639
{tag.map((t, index) => (
3740
<span
3841
key={index}
39-
className="rounded-full px-3 py-1 text-xs text-black"
40-
style={{ backgroundColor: getRandomColor() }}
42+
className="rounded-full px-3 py-1 text-xs text-gray-800"
43+
style={{ backgroundColor: getColorFromString(t) }}
4144
>
4245
{t}
4346
</span>

0 commit comments

Comments
 (0)