From 3be854f5216602f973880a055d28284e3530fa77 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Wed, 14 Feb 2024 18:43:17 -0800 Subject: [PATCH] temp --- src/app/topic/[slug]/page.tsx | 6 +++++- src/app/utils/string2hex.ts | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/app/utils/string2hex.ts diff --git a/src/app/topic/[slug]/page.tsx b/src/app/topic/[slug]/page.tsx index 5cf7449..60567c6 100644 --- a/src/app/topic/[slug]/page.tsx +++ b/src/app/topic/[slug]/page.tsx @@ -1,6 +1,7 @@ import { Zilla_Slab } from 'next/font/google' import { topics } from '@/app/db/data' import { notFound } from 'next/navigation' +import string2hex from '@/app/utils/string2hex' const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500'] }) @@ -17,7 +18,10 @@ export default function Page({ const { name, description, wiki } = topic return ( -
+

{name}

{description}

diff --git a/src/app/utils/string2hex.ts b/src/app/utils/string2hex.ts new file mode 100644 index 0000000..f722262 --- /dev/null +++ b/src/app/utils/string2hex.ts @@ -0,0 +1,22 @@ +export default function string2hex(str: string): string { + // Hash function to convert string to a number + let hash = 0 + for (let i = 0; i < str.length; i++) { + const char = str.charCodeAt(i) + hash = (hash << 5) - hash + char + hash = hash & hash // Convert to 32bit integer + } + + // Convert the hash to a hex color code + let color = '#' + for (let i = 0; i < 3; i++) { + // Ensuring minimum brightness for each component to make text stand out + // Adjust the minimum brightness as needed (e.g., 0x80 for brighter colors) + const minBrightness = 0x80 + const value = + (((hash >> (i * 8)) & 0xff) % (0xff - minBrightness)) + minBrightness + color += ('00' + value.toString(16)).substr(-2) + } + + return color +}