Skip to content

[interpolation] Fix steps using deltaEMethod inconsistently - #740

Merged
svgeesus merged 1 commit into
color-js:mainfrom
TimonLukas:fix/632-steps-delta-e-method
Jul 17, 2026
Merged

svgeesus merged 1 commit into
color-js:mainfrom
TimonLukas:fix/632-steps-delta-e-method

Conversation

@TimonLukas

Copy link
Copy Markdown
Contributor

Fixes #632

I stumbled over the same issue. Due to my limited experience with color stuff I'm not sure this fix is correct, especially since this comment mentions that some parts might already have been fixed, which I can't find - but this change fixes the error, the resulting colors look roughly as I'd expect them to, and the same amount of tests pass with and without.

Hope this is useful!

@netlify

netlify Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

βœ… Deploy Preview for colorjs ready!

Name Link
πŸ”¨ Latest commit 5962b8f
πŸ” Latest deploy log https://app.netlify.com/projects/colorjs/deploys/6a3292b0a5bdff0008b6dd05
😎 Deploy Preview https://deploy-preview-740--colorjs.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
πŸ€– Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@facelessuser

Copy link
Copy Markdown
Collaborator

I feel like this should have a test representing what previously failed but is now working due to the fix.

@TimonLukas

Copy link
Copy Markdown
Contributor Author

Makes sense! I've been trying to write one, but I need some advice regarding the test runner.

Since the steps() issue happens when using the procedural API while the Lab space is not registered, the test needs to modify the ColorSpace.registry. To keep the global state changes minimal I've been trying to swap out the existing registry, but this makes the tests somewhat brittle, as it causes any parallel tests to fail. The test suite passes if this test is running on levels 1 or 5 ("Interpolation Tests" and "first" below), and the suite fails while it's running on a level between them.

I couldn't find an existing solution in the other tests, or an intended approach in the htest.dev docs. Do you maybe have any tips?

WIP test code
Details
// test/interpolation.js
import { ColorSpace, OKLCH, steps } from "../src/index-fn.js";

let originalRegistry;

export default {
	name: "Interpolation Tests",
	tests: [
		{
			name: "steps",
			tests: [
				{
					name: "One more level",
					tests: [
						{
							name: "No error",
							beforeAll () {
								originalRegistry = ColorSpace.registry;
								ColorSpace.registry = {};
							},
							afterAll () {
								ColorSpace.registry = originalRegistry;
							},
							beforeEach () {
								ColorSpace.registry = {};
								this.data.colorSpaces.forEach(space => {
									ColorSpace.register(space);
								});
							},
							run (first, second) {
								steps(first, second);
							},
							tests: [
								{
									name: "first",
									data: {
										colorSpaces: [OKLCH],
									},
									args: [
										{ space: "oklch", coords: [1, 0, 360], alpha: 1 },
										{ space: "oklch", coords: [0, 0, 0], alpha: 1 },
									],
								},
							],
						},
					],
				},
			],
		},
	],
};

@facelessuser

Copy link
Copy Markdown
Collaborator

I'd probably just use something like this:

// test/interpolation.js
import Color from "../src/index.js";
import * as check from "../node_modules/htest.dev/src/check.js";

export default {
	name: "Interpolation Tests",
	check: check.deep(check.proximity({ epsilon: 0.00001 })),
	tests: [
		{
			name: "steps",
			tests: [
				{
					name: "One more level",
					tests: [
						{
							name: "No error",
							run (first, second, options) {
								const colors = new Color(first).steps(first, options).map(c => {return c.coords});
								return colors;
							},
							tests: [
								{
									name: "first",
									args: [
										{ space: "oklch", coords: [1, 0, 360], alpha: 1 },
										{ space: "oklch", coords: [1, 0, 0], alpha: 1 },
										{ space: "oklch", steps: 3, hue: "longer", deltaEMethod: "OK" }
									],
									expect: [[1, 0, 0], [1, 0, 180], [1, 0, 360]]
								},
							],
						},
					],
				},
			],
		},
	],
};

@TimonLukas

Copy link
Copy Markdown
Contributor Author

That unfortunately doesn't test what this PR fixes. As I said, the issue only happens while the Lab colorspace isn't registered, but it has already been registered when the test executes, so your suggestion doesn't hit the problematic path in the current main branch. The registry has to be cleared before the test (and presumably should be restored after).

Here's a minimal reproduction:

// example.mjs
import { ColorSpace, OKLCH, steps } from "colorjs.io/fn"

ColorSpace.register(OKLCH)

steps({ space: "oklch", coords: [1, 0, 360], alpha: 1 }, { space: "oklch", coords: [0, 0, 0], alpha: 1 }, {
  deltaEMethod: "OK2"
})

If you save this as example.mjs and execute pnpm init && pnpm add colorjs.io && node example.mjs, the steps() function throws:

file://node_modules/.pnpm/colorjs.io@0.6.1/node_modules/colorjs.io/src/ColorSpace.js:313
                                throw new TypeError(`No color space found with id = "${space}"`);
                                      ^

TypeError: No color space found with id = "lab"
    at ColorSpace.get (file://node_modules/.pnpm/colorjs.io@0.6.1/node_modules/colorjs.io/src/ColorSpace.js:313:11)
    at distance (file://node_modules/.pnpm/colorjs.io@0.6.1/node_modules/colorjs.io/src/distance.js:14:21)
    at Object.deltaE76 (file://node_modules/.pnpm/colorjs.io@0.6.1/node_modules/colorjs.io/src/deltaE/deltaE76.js:11:9)
    at deltaE (file://node_modules/.pnpm/colorjs.io@0.6.1/node_modules/colorjs.io/src/deltaE.js:28:27)
    at steps (file://node_modules/.pnpm/colorjs.io@0.6.1/node_modules/colorjs.io/src/interpolation.js:81:19)
    at file://example.mjs:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:439:25)
    at async node:internal/modules/esm/loader:633:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

Node.js v24.16.0

@facelessuser

facelessuser commented Jun 17, 2026 •

Copy link
Copy Markdown
Collaborator

Ah, I guess I didn't look super close at the issue it's fixing.

@TimonLukas

Copy link
Copy Markdown
Contributor Author

No problem! :) I still think a regression test would make sense if there's some clean way to do the override, but relying on nesting levels seems too brittle.

If someone can recommend a solution, I'll be happy to add the test case!

@facelessuser

Copy link
Copy Markdown
Collaborator

Yeah, I'll take a closer look when I get a chance, assuming someone doesn't answer your question before I do.

@svgeesus
svgeesus merged commit 9ab13a9 into color-js:main Jul 17, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

steps() uses deltaEMethod inconsistently

3 participants