graypegg / chromatism

:rainbow: A simple set of utility functions for colours.
1.78k stars 37 forks source link

Weird result when doing Lab -> hsv with specific values #44

Open FruitieX opened 5 years ago

FruitieX commented 5 years ago

Hi,

Great library, I'm using this for color calculations in my home automation lighting setup. :)

Notably, I'm using Lab colorspace for tweaking the "color temperature" of my lights based on time of day (yeah, probably not the best way, but anyway). My lights expect hsv, so I'm doing the conversion between these using chromatism.

I noticed that at a particular time every evening, my lights would briefly flash bright white as the color temperature slowly adjusts towards an orange color. I started digging into this, and got this far as to have a consistent repro of the bug:

$ node
> require('chromatism').convert({"L":50,"a":10.3777,"b":56.74016}).hsv
{ h: 214.9506072233,
  s: -226243866.03568426,
  v: 0.00001916702582205332 }
> require('chromatism').convert({"L":50,"a":10.3777,"b":56.7401}).hsv
{ h: 42.32831591829547,
  s: 99.99979165015888,
  v: 61.46835916922344 }

Notice how the first example gives totally crazy values, but tweaking any of the input params just slightly in any direction gives me a "sane" color again. My guess would be a division by zero occurring somewhere in chromatism. Converting to RGB (instead of HSV) works fine with these input parameters.

TehShrike commented 5 years ago

Great reproduction, and possibly the best "how I discovered this bug" story I've heard!