Qix- / color-convert

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

Hue component of HCG color is wrong by 120° then converting from RGB or CMYK #73

Closed alex-knyaz closed 5 years ago

alex-knyaz commented 5 years ago

Steps to reproduce

Here we are converting from RGB to HCG and then back. Note color components are very far apart.

var convert = require('color-convert') convert.hcg.rgb.raw(convert.rgb.hcg.raw([250, 0, 255])) [ 0, 255, 249.99999999999991 ]

Description

Then converting color to HCG from RGB or CMYK Hue component of the color appears to be offset by 120°.

Current behavior

var convert = require('color-convert') convert.rgb.hcg.raw([250, 0, 255]) [ 178.8235294117647, 100, 0 ]

Expected behavior

var convert = require('color-convert') convert.rgb.hcg.raw([250, 0, 255]) [ 298.8235294117647, 100, 0 ]

Cause?

https://github.com/Qix-/color-convert/blob/master/conversions.js#L668

4 is added twice. This is most likely it, but I'm not 100% sure.

Qix- commented 5 years ago

Your diagnosis is correct, wonderful catch.

I tested this with the "official" implementation (the creator of the model has sadly abandoned the project - which is sad, because I actually really like this model) and your math is correct.

> hcg = require('./hcg')
{ rgb2hcg: [Function: rgb2hcg], hcg2rgb: [Function: hcg2rgb] }
> hcg.rgb2hcg([250, 0, 255])
[ 0.8300653594771242, 255, 0 ]
> 0.8300653594771242 * 360
298.8235294117647

Pushing a patch now.

Qix- commented 5 years ago

Published as 2.0.1. Thanks again for a well-written bug report - highly appreciated.