Skip to content

Commit

Permalink
Fix #fff convert
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Dec 4, 2024
1 parent 26eb02a commit 93bc939
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions stores/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions stores/visible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -48,7 +48,7 @@ export let visible = computed(
color: rgbColor,
fallback,
real: false,
space: inRec2020(color) ? 'rec2020' : 'out'
space: space === Space.Rec2020 ? 'rec2020' : 'out'
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/colors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

0 comments on commit 93bc939

Please sign in to comment.