@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
49 lines • 1.76 kB
JavaScript
import { Black, White, toColor } from './lib/utils.js';
import { ColorSchema, NumberSchema, StringSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
import { setToPrecision } from '../../utils/precision.js';
export const colorSpaces = ['Lab', 'ICtCp', 'Jzazbz'];
export default class NodeDefinition extends Node {
static title = 'Distance';
static type = 'studio.tokens.color.distance';
static description = 'Calculate the distance between two colors. The output is a number representing the difference between the two colors. The lower the number, the closer the colors are to each other. The output is based on the CIEDE2000 color difference formula.';
constructor(props) {
super(props);
this.addInput('colorA', {
type: {
...ColorSchema,
default: White
}
});
this.addInput('colorB', {
type: {
...ColorSchema,
default: Black
}
});
this.addInput('precision', {
type: {
...NumberSchema,
default: 4
}
});
this.addInput('space', {
type: {
...StringSchema,
enum: colorSpaces,
default: 'Lab'
}
});
this.addOutput('value', {
type: NumberSchema
});
}
execute() {
const { colorA, colorB, space, precision } = this.getAllInputs();
const a = toColor(colorA);
const b = toColor(colorB);
const distance = a.distance(b, space);
this.outputs.value.set(setToPrecision(distance, precision));
}
}
//# sourceMappingURL=distance.js.map