Skip to content

Error when passing a Color object to the procedural api #303

@lloydk

Description

@lloydk

Not sure if this is a bug or working as intended but it looks like the object oriented and procedural APIs are not compatible with each other. Passing a Color object to the to function results in the following exception:

~/work/test$ node index.js
file:///home/lloydk/work/test/node_modules/colorjs.io/src/space.js:316
                throw new TypeError(`${space} is not a valid color space`);
                      ^

TypeError: sRGB (srgb) is not a valid color space
    at ColorSpace.get (file:///home/lloydk/work/test/node_modules/colorjs.io/src/space.js:316:9)
    at getColor (file:///home/lloydk/work/test/node_modules/colorjs.io/src/getColor.js:24:28)
    at to (file:///home/lloydk/work/test/node_modules/colorjs.io/src/to.js:13:10)
    at file:///home/lloydk/work/test/index.js:8:1
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)

Code to reproduce:

package.json

{
  "name": "test",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "colorjs.io": "^0.4.3"
  }
}

index.js

import Color from 'colorjs.io'
import { ColorSpace, sRGB, OKLCH, to } from 'colorjs.io/fn'

ColorSpace.register(OKLCH)
ColorSpace.register(sRGB)

let bg = new Color('white')
to(bg, 'oklch')

I think the issue is that there are two ColorSpace classes being loaded in the above code. One from ./dist/color.js and the other from ./src/index-fn.js. Because the classes are different the instanceof check here fails and eventually the code hits the end of the method and throws an exception.

Why would you want to mix the object oriented API with the procedural API? In my case I was writing some tests for code that used the procedural API and I thought it would be more convenient to use the Color class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions