simonwep / pickr

🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap, Materialize. Supports alpha channel, rgba, hsla, hsva and more!
https://simonwep.github.io/pickr
MIT License
4.29k stars 287 forks source link

Lengthy Floating Point: Converting to RGBA #293

Closed phuze closed 2 years ago

phuze commented 3 years ago

What is the current behavior?

When you convert a color to it's RGBA equivalent, it results in a rather lengthy floating point:

pickr.setColor('#9C27B0')

console.log('themeHex', pickr.getColor().toHEXA().toString())
// returns #9C27B0

console.log('themeRgb', pickr.getColor().toRGBA().toString())
// returns rgba(156.00000000000006, 38.99999999999999, 176, 1)

Please provide the steps to reproduce and create a JSFiddle.

Look at the console in this fiddle: https://jsfiddle.net/urp52z60/

What is the expected behavior?

A well-formed value that adheres to the expected RGBA color model. Expected output: rgba(156, 39, 176, 1)

Your environment:

Version: 1.8.1 Used bundle: modern Used theme: classic Browser-version: Edge 92.0.902.67 Operating-system: Windows 10

phuze commented 3 years ago

I'll follow this up by noting for anyone else reading, you can resolve this by mapping the array yourself:

pickr.setColor('#9C27B0')

let fixedRgba =  `rgba(${pickr.getColor().toRGBA().map( num => { return Math.round(num) }).join(', ')})`;

console.log('fixedRgba', fixedRgba)
// returns rgba(156, 39, 176, 1)
saeednazarix commented 2 years ago

also you can use .toString(0) pickr.getColor().toRGBA().toString(0) it is documented on readme.

simonwep commented 2 years ago

Correct way of solving this, thanks @saeedxnazari :)