colorkt icon indicating copy to clipboard operation
colorkt copied to clipboard

Color conversion precision error

Open FilippoVigani opened this issue 2 years ago • 0 comments

I was playing around with the library - great work by the way - and encountered some issues when converting back to sRGB.

I noticed them when creating an awt.Color after a few conversions using the library and noticed the sRGB float value was out of range. You can easily reproduce the issue with the following code:

    val srgbColor = Srgb(testColor)
    val oklabColor = srgbColor.convert<Oklab>()
    val computedSrgbColor = oklabColor.convert<Srgb>()
    val awtColor = java.awt.Color(computedSrgbColor.r.toFloat(), computedSrgbColor.g.toFloat(), computedSrgbColor.b.toFloat())

The value of computedSrgbColor has a value of r which is 1.0000000001775198, when converted back to Float it rounds back to 1.0. However both the value of g and b, even when converter to float fall out of the range [0,1], and are in fact negative (respectively -6.415840168402798E-8 and -3.9362722935143425E-7).

I was wondering if they are both just expected rounding errors which are safe to be coerced in the range [0,1], or I am missing something which isn't obvious.

For clarification I'm just using the conversion to awt.Color to easily preview them in the debugger :)

FilippoVigani avatar Apr 11 '22 16:04 FilippoVigani