Skip to content

Commit b5da903

Browse files
committed
refactor: use predefined color value
1 parent 429ceef commit b5da903

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

app/components/Package/ShareModal.vue

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script setup lang="ts">
2+
import { ACCENT_COLOR_TOKENS } from '#shared/utils/constants'
3+
24
const props = defineProps<{
35
packageName: string
46
resolvedVersion: string
@@ -9,24 +11,12 @@ const props = defineProps<{
911
const { origin } = useRequestURL()
1012
const colorMode = useColorMode()
1113
const theme = computed(() => (colorMode.value === 'dark' ? 'dark' : 'light'))
12-
const { selectedAccentColor, accentColors } = useAccentColor()
13-
14-
function resolveColorToHex(color: string): string {
15-
const canvas = document.createElement('canvas')
16-
canvas.width = canvas.height = 1
17-
const ctx = canvas.getContext('2d')!
18-
ctx.fillStyle = color
19-
ctx.fillRect(0, 0, 1, 1)
20-
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data
21-
return `#${r!.toString(16).padStart(2, '0')}${g!.toString(16).padStart(2, '0')}${b!.toString(16).padStart(2, '0')}`
22-
}
14+
const { selectedAccentColor } = useAccentColor()
2315
2416
const colorParam = computed(() => {
25-
const id = selectedAccentColor.value
26-
if (!id) return ''
27-
const colorValue = accentColors.value.find(c => c.id === id)?.value
28-
if (!colorValue) return ''
29-
const hex = resolveColorToHex(colorValue)
17+
const id = selectedAccentColor.value as keyof typeof ACCENT_COLOR_TOKENS
18+
if (!id || !(id in ACCENT_COLOR_TOKENS)) return ''
19+
const hex = ACCENT_COLOR_TOKENS[id][theme.value].hex
3020
return `&color=${encodeURIComponent(hex)}`
3121
})
3222

0 commit comments

Comments
 (0)