From 93bc93911e27e5c60880e7f9cd00d8912791105e Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Wed, 4 Dec 2024 21:09:09 +0000 Subject: [PATCH] Fix #fff convert --- lib/colors.ts | 5 ++++- stores/current.ts | 9 +++++++++ stores/visible.ts | 12 ++++++------ test/colors.test.ts | 9 +++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/colors.ts b/lib/colors.ts index 6669348..3164696 100644 --- a/lib/colors.ts +++ b/lib/colors.ts @@ -162,7 +162,10 @@ if (LCH) { export function getSpace(color: Color): Space { let proxyColor = getProxyColor(color) - if (inRGB(proxyColor)) { + // Hot fix until https://github.com/Evercoder/culori/issues/249 is fixed + if (color.mode === 'oklch' && color.l === 1 && color.c === 0) { + return Space.sRGB + } else if (inRGB(proxyColor)) { return Space.sRGB } else if (inP3(proxyColor)) { return Space.P3 diff --git a/stores/current.ts b/stores/current.ts index b6ec1a4..e77ba38 100644 --- a/stores/current.ts +++ b/stores/current.ts @@ -177,6 +177,15 @@ export function setCurrent(code: string, isRgbInput = false): boolean { if (parsed.mode === COLOR_FN) { current.set(colorToValue(parsed as AnyLch)) } else { + if ( + parsed.mode === 'rgb' && + parsed.r === 1 && + parsed.g === 1 && + parsed.b === 1 + ) { + current.set({ a: (parsed.alpha ?? 1) * 100, c: 0, h: 0, l: 100 }) + return true + } let originSpace = getSpace(parsed) let accurate = LCH ? lch(parsed) : oklch(parsed) if (originSpace === Space.sRGB && getSpace(accurate) !== Space.sRGB) { diff --git a/stores/visible.ts b/stores/visible.ts index 58af9a9..4fe5d3f 100644 --- a/stores/visible.ts +++ b/stores/visible.ts @@ -4,10 +4,9 @@ import { computed } from 'nanostores' import { fastFormat, formatRgb, - inP3, - inRec2020, - inRGB, + getSpace, rgb, + Space, toRgb } from '../lib/colors.ts' import { current, valueToColor } from './current.ts' @@ -24,7 +23,8 @@ export let visible = computed( [current, support], (value, { oklch, p3 }): VisibleValue => { let color: Color = valueToColor(value) - if (inRGB(color)) { + let space = getSpace(color) + if (space === Space.sRGB) { let rgbCss = formatRgb(rgb(color)) if (!oklch) color = rgb(color) return { @@ -36,7 +36,7 @@ export let visible = computed( } else { let rgbColor = toRgb(color) let fallback = formatRgb(rgbColor) - if (inP3(color)) { + if (space === Space.P3) { return { color: p3 && oklch ? color : rgbColor, fallback, @@ -48,7 +48,7 @@ export let visible = computed( color: rgbColor, fallback, real: false, - space: inRec2020(color) ? 'rec2020' : 'out' + space: space === Space.Rec2020 ? 'rec2020' : 'out' } } } diff --git a/test/colors.test.ts b/test/colors.test.ts index 219959a..7a0af4b 100644 --- a/test/colors.test.ts +++ b/test/colors.test.ts @@ -59,4 +59,13 @@ test('correctly works with colors on the edges of sRGB', () => { l: 96.79827203267874 }) deepStrictEqual(visible.get().space, 'srgb') + + setCurrent('#ffffff') + deepStrictEqual(current.get(), { + a: 100, + c: 0, + h: 0, + l: 100 + }) + deepStrictEqual(visible.get().space, 'srgb') })