Skip to content

chroma.js accepts invalid values (and converts them to 0), but not always #356

@bakert

Description

@bakert
> const { default: chroma } = await import("chroma-js");
undefined
> chroma("rgb(127 0 255)").hex()
'#7f00ff' # ok
> chroma("rgb(-1000 0 255)").hex()
'#0000ff' # silently converted my invalid r value to 0
> chroma("rgb(null 0 255)").hex()
Uncaught Error: unknown format: rgb(null 0 255) # correctly detected invalid input
    at new Color (file:///Users/bakert/spectrum.ts/node_modules/chroma-js/src/Color.js:39:19)
    at chroma (file:///Users/bakert/spectrum.ts/node_modules/chroma-js/src/chroma.js:5:12)
> chroma("rgb(nonsense 0 255)").hex()
Uncaught Error: unknown format: rgb(nonsense 0 255) # correctly detected invalid input
    at new Color (file:///Users/bakert/spectrum.ts/node_modules/chroma-js/src/Color.js:39:19)
    at chroma (file:///Users/bakert/spectrum.ts/node_modules/chroma-js/src/chroma.js:5:12)
> chroma(127, 9, 255, 'rgb').hex()
'#7f09ff' # ok
> chroma(-1000, 9, 255, 'rgb').hex()
'#0009ff' # silently converted my invalid r value to 0
> chroma(null, 9, 255, 'rgb').hex()
'#0009ff' # silently converted my invalid r value to 0
> chroma('nonsense', 9, 255, 'rgb').hex()
'#0009ff' # silently converted my invalid r value to 0

Would we consider "silently converted my invalid r value to 0" to be a bug in each case above or is this in some way intended behavior? If I have user input for r, g and b is there a good way to validate it using chroma.js or do I need to do my own typechecking and bounds checking before passing to the library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions