gka / chroma.js

JavaScript library for all kinds of color manipulations
https://gka.github.io/chroma.js/
Other
10.13k stars 545 forks source link

HCL / LCH plain js object not constructing the correct colors. #315

Closed asp55 closed 1 month ago

asp55 commented 1 year ago

Constructing a color using chroma(h, c, l, 'hcl') format is resulting in a different (correct) value from constructing with plain js object chroma({h:h, c:c, l:l})

See screenshot of using following constructors inside documentation page.

chroma({ l:80, c:25, h:200 });

chroma(200, 25, 80, 'hcl');
chroma({ h:200, c:25, l:80, });
DemonstratedInsideDocumentation
prjctimg commented 1 year ago

I also noticed the same bug. It may be that the constructor is not passing the mode parameter internally thus it returns default black. A work around would be passing the mode parameter as a property of the destructured color object like so:

chroma({ l:80, c:25, h:200,mode:'lch' });

Or the code could be internally modified to extract the mode from stringifying the objects enumerable properties like so (pseudocode):

// We could add a type check to see if the passed in color token is an object and if it doesn't have the mode property explicitly defined then we extract it from the object keys
let color = { l:80, c:25, h:200 }
let mode = color.keys.toString()
gka commented 1 month ago

this bug seems to have been fixed already. I added these tests to our test suite to confirm this:

it('create lch color from object', () => {
    expect(new Color({ l: 80, c: 25, h: 200 }).hex()).toBe('#85d4d5');
    expect(chroma({ l: 80, c: 25, h: 200 }).hex()).toBe('#85d4d5');
    expect(chroma.lch(80, 25, 200).hex()).toBe('#85d4d5');
    expect(chroma(80, 25, 200, 'lch').hex()).toBe('#85d4d5');
});

it('create hcl color from object', () => {
    expect(new Color({ h: 200, c: 25, l: 80 }).hex()).toBe('#85d4d5');
    expect(chroma({ h: 200, c: 25, l: 80 }).hex()).toBe('#85d4d5');
    expect(chroma.hcl(200, 25, 80).hex()).toBe('#85d4d5');
    expect(chroma(200, 25, 80, 'hcl').hex()).toBe('#85d4d5');
});
prjctimg commented 1 month ago

Thanks again! 💯