From cd14dbf7d7f0d3ab6b55289f1100488ab2591c86 Mon Sep 17 00:00:00 2001 From: Oliver Pan <2216991777@qq.com> Date: Mon, 24 Jun 2024 00:23:22 -0500 Subject: [PATCH] feat: make get color name an action instead of api --- apps/web/app/actions.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/apps/web/app/actions.ts b/apps/web/app/actions.ts index 0b5a9d5..cc8b7ce 100644 --- a/apps/web/app/actions.ts +++ b/apps/web/app/actions.ts @@ -1,5 +1,21 @@ -"use server"; -import { cookies } from "next/headers"; -export const handleToggleReadability = async (value: string) => { - await cookies().set("showReadability", value); -}; +'use server' + +import colorNameList from '@/lib/converted_colors.json' +import { ColorNames } from '@/types/app' +import { differenceEuclidean, nearest } from 'culori' + +interface ColorNameListWithIndex { + [key: string]: string +} + +const indexedColorNameList: ColorNameListWithIndex = colorNameList + +export const getColorNames = async (colors: ColorNames) => { + const colorKeys = Object.keys(indexedColorNameList) + const nearestNamedColors = nearest(colorKeys, differenceEuclidean(), (name) => indexedColorNameList[name]) + let names = colors.map((color) => { + const name = nearestNamedColors(color, 1) + return name?.[0] + }) + return names +}