@chestnutlabs/gcode-colors 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/colors.d.ts CHANGED
@@ -12,7 +12,7 @@
12
12
  * that read an optional channel (`feature`, `object`, `feedrate`) must be
13
13
  * capability-gated by the caller before use — the IR is told, not shown nonsense.
14
14
  */
15
- import type { ToolpathIR } from '@chestnutlabs/toolpath-core';
15
+ import { type ToolpathIR } from '@chestnutlabs/toolpath-core';
16
16
  export type RGB = [number, number, number];
17
17
  export type ColorMode = {
18
18
  mode: 'single';
@@ -44,11 +44,24 @@ export type ColorMode = {
44
44
  ramp: RGB[];
45
45
  range?: [number, number];
46
46
  fallback: RGB;
47
+ } | {
48
+ mode: 'power';
49
+ ramp: RGB[];
50
+ range?: [number, number];
51
+ fallback: RGB;
52
+ } | {
53
+ mode: 'moveKind';
54
+ cut: RGB;
55
+ travel: RGB;
56
+ fallback: RGB;
47
57
  };
48
58
  /** Neutral color used when a mode has no palette / the channel value is unknown. */
49
59
  export declare const DEFAULT_FALLBACK: RGB;
50
60
  /** Min/max of the IR's defined (non-NaN) feedrates — the auto-range for color-by-speed (#177). */
51
61
  export declare function feedrateRange(ir: ToolpathIR): [number, number];
62
+ /** Min/max of the IR's captured (non-NaN) tool-power values — the auto-range for color-by-power
63
+ * (#189). Returns `[0, 0]` when the `toolPower` modal channel was not captured. */
64
+ export declare function toolPowerRange(ir: ToolpathIR): [number, number];
52
65
  /**
53
66
  * Per-layer height (mm): the Z-delta from the previous layer, indexed by layer. Layer 0's height is
54
67
  * its absolute Z (its thickness from the bed). Negative deltas (non-monotonic Z, e.g. spiral/vase
@@ -1 +1 @@
1
- {"version":3,"file":"colors.d.ts","sourceRoot":"","sources":["../src/colors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAE9D,MAAM,MAAM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gB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,GAAG,CAWrD;AAkBD,4EAA4E;AAC5E,MAAM,MAAM,cAAc,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,GAAG,CAAC;AAEvD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,GAAG,cAAc,CA+EpF;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,GAAG,CAEnF"}
package/dist/colors.js CHANGED
@@ -1,3 +1,18 @@
1
+ /**
2
+ * Renderer-agnostic per-segment color model (DD-014 D3, extracted from
3
+ * `gcode-renderer-three` so the 3D and 2D renderers single-source it).
4
+ *
5
+ * A color is a pure function of the IR's channels (`tool`/`feature`/`object`/
6
+ * `feedrate`/`colorChanges`) and a consumer palette — no `three`, no canvas, no
7
+ * geometry. Each renderer maps the returned {@link RGB} onto its own surface
8
+ * (a vertex-color buffer for WebGL, a `strokeStyle` for Canvas 2D).
9
+ *
10
+ * Honesty is built in: unknown channel values degrade to the mode's `fallback`
11
+ * (or the neutral default), never a fabricated role/speed/object color. Modes
12
+ * that read an optional channel (`feature`, `object`, `feedrate`) must be
13
+ * capability-gated by the caller before use — the IR is told, not shown nonsense.
14
+ */
15
+ import { MoveKind } from '@chestnutlabs/toolpath-core';
1
16
  /** Neutral color used when a mode has no palette / the channel value is unknown. */
2
17
  export const DEFAULT_FALLBACK = [0.7, 0.7, 0.7];
3
18
  /** Min/max of the IR's defined (non-NaN) feedrates — the auto-range for color-by-speed (#177). */
@@ -16,6 +31,25 @@ export function feedrateRange(ir) {
16
31
  }
17
32
  return min <= max ? [min, max] : [0, 0];
18
33
  }
34
+ /** Min/max of the IR's captured (non-NaN) tool-power values — the auto-range for color-by-power
35
+ * (#189). Returns `[0, 0]` when the `toolPower` modal channel was not captured. */
36
+ export function toolPowerRange(ir) {
37
+ const p = ir.segments.modal?.toolPower;
38
+ if (p === undefined)
39
+ return [0, 0];
40
+ let min = Infinity;
41
+ let max = -Infinity;
42
+ for (let i = 0; i < p.length; i++) {
43
+ const v = p[i];
44
+ if (Number.isNaN(v))
45
+ continue;
46
+ if (v < min)
47
+ min = v;
48
+ if (v > max)
49
+ max = v;
50
+ }
51
+ return min <= max ? [min, max] : [0, 0];
52
+ }
19
53
  /**
20
54
  * Per-layer height (mm): the Z-delta from the previous layer, indexed by layer. Layer 0's height is
21
55
  * its absolute Z (its thickness from the bed). Negative deltas (non-monotonic Z, e.g. spiral/vase
@@ -128,6 +162,31 @@ export function createSegmentColorer(ir, mode) {
128
162
  return rampColor(mode.ramp, span > 0 ? (heights[li] - lo) / span : 0);
129
163
  };
130
164
  }
165
+ if (mode.mode === 'power') {
166
+ const power = seg.modal?.toolPower;
167
+ // The channel wasn't captured (parse didn't request toolPower) → everything is fallback.
168
+ if (power === undefined) {
169
+ const fb = mode.fallback;
170
+ return () => fb;
171
+ }
172
+ const [lo, hi] = mode.range ?? toolPowerRange(ir);
173
+ const span = hi - lo;
174
+ // NaN (tool off / never engaged) → fallback, never a fabricated power color.
175
+ return (i) => {
176
+ const v = power[i];
177
+ return Number.isNaN(v) ? mode.fallback : rampColor(mode.ramp, span > 0 ? (v - lo) / span : 0);
178
+ };
179
+ }
180
+ if (mode.mode === 'moveKind') {
181
+ return (i) => {
182
+ const k = seg.kind[i];
183
+ if ((k & (MoveKind.Extrude | MoveKind.Cut)) !== 0)
184
+ return mode.cut; // productive move
185
+ if ((k & MoveKind.Travel) !== 0)
186
+ return mode.travel; // rapid
187
+ return mode.fallback;
188
+ };
189
+ }
131
190
  // object mode: object 0 = none/unknown → fallback; `only` isolates one object, dimming the rest (#178).
132
191
  return (i) => {
133
192
  const obj = seg.object[i];
package/dist/index.d.ts CHANGED
@@ -8,5 +8,5 @@
8
8
  * `three`, no canvas, no framework.
9
9
  */
10
10
  export type { RGB, ColorMode, SegmentColorer } from './colors.js';
11
- export { DEFAULT_FALLBACK, feedrateRange, layerHeights, layerHeightRange, rampColor, createSegmentColorer, segmentColor } from './colors.js';
11
+ export { DEFAULT_FALLBACK, feedrateRange, toolPowerRange, layerHeights, layerHeightRange, rampColor, createSegmentColorer, segmentColor } from './colors.js';
12
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,YAAY,EACZ,gBAAgB,EAChB,SAAS,EACT,oBAAoB,EACpB,YAAY,EACb,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,SAAS,EACT,oBAAoB,EACpB,YAAY,EACb,MAAM,aAAa,CAAC"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { DEFAULT_FALLBACK, feedrateRange, layerHeights, layerHeightRange, rampColor, createSegmentColorer, segmentColor } from './colors.js';
1
+ export { DEFAULT_FALLBACK, feedrateRange, toolPowerRange, layerHeights, layerHeightRange, rampColor, createSegmentColorer, segmentColor } from './colors.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chestnutlabs/gcode-colors",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Renderer-agnostic per-segment color model for the Chestnut Labs G-code toolpath stack (DD-014 D3): the ColorMode union and honest, capability-gated segment coloring over ToolpathIR channels, shared by the 3D and 2D renderers.",
5
5
  "keywords": [
6
6
  "gcode",
@@ -47,6 +47,6 @@
47
47
  "test": "vitest run"
48
48
  },
49
49
  "dependencies": {
50
- "@chestnutlabs/toolpath-core": "0.3.0"
50
+ "@chestnutlabs/toolpath-core": "0.5.0"
51
51
  }
52
52
  }