UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

82 lines 2.84 kB
import { BooleanSchema, ColorSchema, NumberSchema, StringSchema } from '../../schemas/index.js'; import { Node } from '../../programmatic/node.js'; import { Poline, positionFunctions } from 'poline'; import { arrayOf } from '../../schemas/utils.js'; import { toColor } from './lib/utils.js'; const positionFuncs = Object.keys(positionFunctions); export default class NodeDefinition extends Node { static title = 'Poline'; static type = 'studio.tokens.color.poline'; static description = ''; constructor(props) { super(props); this.addInput('anchorColors', { type: arrayOf(ColorSchema) }); this.addInput('numPoints', { type: { ...NumberSchema, default: 4 } }); this.addInput('invertedLightness', { type: BooleanSchema }); this.addInput('positionFnX', { type: { ...StringSchema, enum: positionFuncs } }); this.addInput('positionFnY', { type: { ...StringSchema, enum: positionFuncs } }); this.addInput('positionFnZ', { type: { ...StringSchema, enum: positionFuncs } }); this.addInput('hueShift', { type: NumberSchema }); this.addOutput('value', { type: arrayOf(ColorSchema) }); } execute() { const { numPoints, hueShift, anchorColors, positionFnX, positionFnY, positionFnZ, invertedLightness } = this.getAllInputs(); if (!anchorColors || anchorColors.length < 2) { throw new Error('Not enough color inputs'); } //Poline only deals in hsl const hsl = anchorColors.map(col => { const hslColor = toColor(col).to('hsl'); return [hslColor.h ?? 0, hslColor.s, hslColor.l]; }); const polineOptions = { invertedLightness, numPoints, anchorColors: hsl, positionFunctionX: positionFunctions[positionFnX ? positionFnX : 'sinusoidalPosition'], positionFunctionY: positionFunctions[positionFnY ? positionFnY : 'sinusoidalPosition'], positionFunctionZ: positionFunctions[positionFnZ ? positionFnZ : 'sinusoidalPosition'] }; const poline = new Poline(polineOptions); if (hueShift) { poline.shiftHue(hueShift); } //Note that we lose the alpha channels here const colors = poline.colors.map(([h, s, l]) => { return { space: 'hsl', channels: [h, s, l] }; }); this.outputs.value.set(colors); } } //# sourceMappingURL=poline.js.map