Skip to content

[ColorPicker] ColorPicker alters manually entered hex value due to precision loss in colortranslator #3948

@HADB

Description

@HADB

Environment

  • Operating System: Darwin
  • Node Version: v22.13.1
  • Nuxt Version: 3.16.2
  • CLI Version: 3.24.1
  • Nitro Version: 2.11.8
  • Package Manager: pnpm@10.8.1
  • Builder: -
  • User Config: app, compatibilityDate, css, devtools, devServer, eslint, imports, modules, runtimeConfig, sourcemap, ssr, icon, ui, colorMode
  • Runtime Modules: @nuxt/eslint@1.3.0, @nuxt/ui@3.0.2, @vueuse/nuxt@13.0.0
  • Build Modules: -

Is this bug related to Nuxt or Vue?

Nuxt

Version

v3.0.2

Reproduction

https://codesandbox.io/p/devbox/condescending-mcnulty-k5jgtf

Description

Hi 👋,

I’ve noticed an issue with the ColorPicker component related to color precision.

🐛 Bug Description

When I manually input the hex value #123456 into the ColorPicker, it gets changed to #123354 immediately.

After digging into the source code, I found that the component uses colortranslator and internally converts colors via HSL using HSLObject. It appears that during the RGB → HSL → RGB round-trip, there’s a slight loss of precision, which results in the hex value being altered.

It seems that the issue might be caused by the decimals option being set to 2, while the default value in colortranslator is 6.

const color = new ColorTranslator(HSVtoHSL(value), {
decimals: 2,
labUnit: 'percent',
cmykUnit: 'percent',
cmykFunction: 'cmyk'
})

Or Math.round here:

function HSVtoHSL(hsv: HSVColor): HSLObject {
const x = (200 - hsv.s) * hsv.v / 100
return {
H: hsv.h,
S: x === 0 || x === 200 ? 0 : Math.round(hsv.s * hsv.v / (x <= 100 ? x : 200 - x)),
L: Math.round(x / 2)
}
}

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingv3#1289

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions