Skip to content

toGamut() throws error "Cannot read properties of undefined (reading '1')" on other modes #261

@drwpow

Description

@drwpow

Problem

The signature of toGamut() allows a mode as the 2nd param, e.g. toGamut('rgb', 'lab'). But on other modes, a runtime error occurs:

  TypeError [Error]: Cannot read properties of undefined (reading '1')
      at /evercoder/culori/src/clamp.js:239:26
      at TestContext.<anonymous> (/evercoder/culori/test/clamp.test.js:184:34)
      at Test.runInAsyncScope (node:async_hooks:214:14)
      at Test.run (node:internal/test_runner/test:1062:25)
      at Test.processPendingSubtests (node:internal/test_runner/test:752:18)
      at Test.postRun (node:internal/test_runner/test:1191:19)
      at Test.run (node:internal/test_runner/test:1119:12)
      at async Test.processPendingSubtests (node:internal/test_runner/test:752:7)

Part of the problem is there are no tests for toGamut() utilizing the 2nd param. The line that throws is here:

    let epsilon = (ranges.c[1] - ranges.c[0]) / 4000; // 0.0001 for oklch()
                          ^
                          Cannot read properties of undefined 

ranges will be an object matching the mode, so anything without a c channel (OKLCH, LCH) will throw an error.

Reproduction

Test the following code out in clamp.test.js:

  assert.equal(
    formatCss(toGamut('rgb', 'lab')({ mode: 'lab', l: 0.97607, a: -15.753, b: 93.388, })),
    'color(srgb 0.4906 0.1387 0.159)',
    'Lab'
  );
  assert.equal(
    formatCss(toGamut('rgb', 'lch')({ mode: 'lch', l: 0.512345, c:21.2, h: 130 })),
    'color(srgb 0.41587, 0.503670, 0.36664)',
    'LCh'
  );

Expectation

Since toGamut() allows specifying a mode, I‘d either expect:

  • All modes to work, or

  • An error to be thrown if a mode is not valid

  • I’m willing to open a PR

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