UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

79 lines 2.5 kB
import { BooleanSchema, NumberSchema } from '../../schemas/index.js'; import { Node } from '../../programmatic/node.js'; import { setToPrecision } from '../../utils/precision.js'; export default class NodeDefinition extends Node { static title = 'Inverse Linear Mapping'; static type = 'studio.tokens.series.inverseLinear'; static description = 'Maps values from one range to another with optional clamping'; constructor(props) { super(props); this.addInput('value', { type: { ...NumberSchema, default: 0.5, description: 'Value to map' } }); this.addInput('inMin', { type: { ...NumberSchema, default: 0, description: 'Input range minimum' } }); this.addInput('inMax', { type: { ...NumberSchema, default: 1, description: 'Input range maximum' } }); this.addInput('outMin', { type: { ...NumberSchema, default: 0, description: 'Output range minimum' } }); this.addInput('outMax', { type: { ...NumberSchema, default: 100, description: 'Output range maximum' } }); this.addInput('clamp', { type: { ...BooleanSchema, default: true, description: 'Clamp output to range' } }); this.addInput('precision', { type: { ...NumberSchema, default: 2, minimum: 0 } }); this.addOutput('value', { type: NumberSchema }); } execute() { const { value, inMin, inMax, outMin, outMax, clamp, precision } = this.getAllInputs(); let result; if (inMax === inMin) { result = outMin; } else { const normalizedValue = (value - inMin) / (inMax - inMin); result = outMin + normalizedValue * (outMax - outMin); if (clamp) { result = Math.min(Math.max(result, Math.min(outMin, outMax)), Math.max(outMin, outMax)); } } this.outputs.value.set(setToPrecision(result, precision)); } } //# sourceMappingURL=inverseLinear.js.map