Qix- / color-convert

Plain color conversion functions in JavaScript
MIT License
746 stars 96 forks source link

loss of accuracy #22

Closed OEvgeny closed 8 years ago

OEvgeny commented 9 years ago

Just try this:

conv.hsl(conv.rgb(223, 240, 216).hsl()).rgb()

It returns

[ 222, 239, 215 ]

I think it is because hsl values are integers.

Qix- commented 8 years ago

Not sure why I'm just seeing this (Usually on top of issues; sorry!)


I'm not entirely sure how this would be fixed if it's a rounding issue. Floating point math is rather dodgy in computer science given the standard's nature (IEEE Std 754-2008 is a beast). I can look into it a bit further.

Keep in mind not all conversions are lossless!

OEvgeny commented 8 years ago

I think it can be.. This function produces:

rgb2hsl([ 223, 240, 216 ])
>[102.5, 44.444444444444436, 89.41176470588235]

But:

var converter = require("color-convert")();

converter.rgb(223, 240, 216).hsl()

Returns to me:

[103, 44, 89]
Qix- commented 8 years ago

I can't seem to reproduce this now that I look at it.

screen shot 2015-12-17 at 01 28 12

What version of color-convert are you using? Have you tried using rgb2hslRaw()?

OEvgeny commented 8 years ago

I simply copied This function to my devtools. and call it.

Qix- commented 8 years ago

Because the library wraps functions with a rounding operation because decimal numbers in most color values don't usually work. For instance, you can't specify a decimal 256-based RGB color in most technologies (only exception I can think of is with Apple technologies, which use a 0.0-1.0 floating point number for color since the retina screens support it).

The hash right now doesn't have the xRaw() variations I don't think; not that I can see anyway. Use rgb2hslRaw() instead.

OEvgeny commented 8 years ago

Thanks. Didn't know about that.

Qix- commented 8 years ago

I'll think about adding in something like rgb.hsl.raw(). Might be helpful. :+1: Thanks for the report!

Qix- commented 8 years ago

rgb.hsl.raw() is added in #27. :+1:

OEvgeny commented 8 years ago

Thnks a lot! :+1: