Skip to content

Commit 3237cf1

Browse files
committed
generate palette based on primary color
1 parent e3aab3d commit 3237cf1

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

colors.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function interpolateColor(color1: string, color2: string, ratio: number) {
2+
const r1 = parseInt(color1.substring(1, 3), 16)
3+
const g1 = parseInt(color1.substring(3, 5), 16)
4+
const b1 = parseInt(color1.substring(5, 7), 16)
5+
6+
const r2 = parseInt(color2.substring(1, 3), 16)
7+
const g2 = parseInt(color2.substring(3, 5), 16)
8+
const b2 = parseInt(color2.substring(5, 7), 16)
9+
10+
const r = Math.round(r1 + (r2 - r1) * ratio)
11+
.toString(16)
12+
.padStart(2, '0')
13+
const g = Math.round(g1 + (g2 - g1) * ratio)
14+
.toString(16)
15+
.padStart(2, '0')
16+
const b = Math.round(b1 + (b2 - b1) * ratio)
17+
.toString(16)
18+
.padStart(2, '0')
19+
20+
return `#${r}${g}${b}`.toUpperCase()
21+
}
22+
23+
export function generatePalette(color: string, extra = 0.05) {
24+
const palette: { [key: number]: string } = {
25+
...[50, 100, 200, 300, 400].reduce(
26+
(prev, x) => ({
27+
...prev,
28+
[x]: interpolateColor(color, '#FFFFFF', 1 - x / 500 + extra),
29+
}),
30+
{},
31+
),
32+
500: color,
33+
...[600, 700, 800, 900, 950].reduce(
34+
(prev, x) => ({
35+
...prev,
36+
[x]: interpolateColor(color, '#000000', x / 500 - 1 - extra),
37+
}),
38+
{},
39+
),
40+
}
41+
42+
return {
43+
...palette,
44+
black: palette[950],
45+
}
46+
}

environment.ts

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ export type Environment = {
4040
// (Optional) Bugsnag API Key, you can get one at https://www.bugsnag.com/
4141
BUGSNAG_API_KEY?: string
4242

43+
/**
44+
* Theme configuration
45+
*/
46+
47+
// URL of the logo to place in the header
48+
LOGO: string
49+
50+
// URL of the favicon
51+
FAVICON: string
52+
53+
// Brand color to use for the theme
54+
BRAND_COLOR: string
55+
4356
/**
4457
* Wallet/chain configuration
4558
*/
@@ -170,6 +183,10 @@ const getEnvironment = async (): Promise<Environment> => {
170183
BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
171184
MAX_ROYALTIES: maxRoyaltiesPerTenThousand / 100,
172185
BUGSNAG_API_KEY: process.env.NEXT_PUBLIC_BUGSNAG_API_KEY,
186+
// Theme configuration
187+
LOGO: metadata.LOGO || '/logo.svg',
188+
FAVICON: metadata.FAVICON || '/favicon.svg',
189+
BRAND_COLOR: metadata.BRAND_COLOR || '#245BFF',
173190
// Wallet/chain configuration
174191
CHAINS: [
175192
ethereumMainnet,

pages/_app.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import connectors from '../connectors'
3535
import getEnvironment, { Environment, EnvironmentContext } from '../environment'
3636
import useAccount, { COOKIES, COOKIE_JWT_TOKEN } from '../hooks/useAccount'
3737
import useEnvironment from '../hooks/useEnvironment'
38-
import { theme } from '../styles/theme'
38+
import { getTheme } from '../styles/theme'
3939
import Error from './_error'
4040
require('dayjs/locale/ja')
4141
require('dayjs/locale/zh-cn')
@@ -215,6 +215,9 @@ function MyApp({ Component, pageProps }: AppProps<MyAppProps>): JSX.Element {
215215
() => connectors(environment),
216216
[environment],
217217
)
218+
219+
const theme = useMemo(() => getTheme(environment.BRAND_COLOR), [environment])
220+
218221
return (
219222
<ErrorBoundary>
220223
<EnvironmentContext.Provider value={environment}>

pages/_document.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import invariant from 'ts-invariant'
1717
import getClient from '../client'
1818
import getEnvironment from '../environment'
1919
import { COOKIES, COOKIE_JWT_TOKEN } from '../hooks/useAccount'
20-
import { theme } from '../styles/theme'
20+
import { baseTheme } from '../styles/theme'
2121
import { MyAppProps } from './_app'
2222

2323
type MyDocumentProps = { apolloState: NormalizedCacheObject }
@@ -40,7 +40,9 @@ class MyDocument extends Document {
4040
/>
4141
</Head>
4242
<body>
43-
<ColorModeScript initialColorMode={theme.config.initialColorMode} />
43+
<ColorModeScript
44+
initialColorMode={baseTheme.config.initialColorMode}
45+
/>
4446
<Main />
4547
<NextScript />
4648
</body>

styles/theme.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { extendTheme } from '@chakra-ui/react'
2+
import { generatePalette } from '../colors'
23

3-
export const theme = extendTheme({
4+
export const baseTheme = extendTheme({
45
styles: {
56
global: {
67
'*::placeholder': {
@@ -241,19 +242,6 @@ export const theme = extendTheme({
241242
500: '#3B82F6',
242243
600: '#2563EB',
243244
},
244-
brand: {
245-
50: '#F5F7FF',
246-
100: '#D3E2FF',
247-
200: '#A7C4FF',
248-
300: '#7BA3FF',
249-
400: '#5A88FF',
250-
500: '#245BFF',
251-
600: '#1A45DB',
252-
700: '#1233B7',
253-
800: '#0B2393',
254-
900: '#06177A',
255-
black: '#060F27',
256-
},
257245
secondary: {
258246
100: '#C9FBCB',
259247
500: '#02B14F',
@@ -300,3 +288,10 @@ export const theme = extendTheme({
300288
full: '9999px',
301289
},
302290
})
291+
292+
export const getTheme = (brandColor: string) =>
293+
extendTheme(baseTheme, {
294+
colors: {
295+
brand: generatePalette(brandColor),
296+
},
297+
})

0 commit comments

Comments
 (0)