A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
Replace pigment engine with clean-room 8-band spectral Kubelka-Munk model
sora209ccfe2b45a
diff --git a/src/components/brushStudio/BrushStudioScratchpad.tsx b/src/components/brushStudio/BrushStudioScratchpad.tsxindex 8411e6d..49ca4a9 100644--- a/src/components/brushStudio/BrushStudioScratchpad.tsx+++ b/src/components/brushStudio/BrushStudioScratchpad.tsx@@ -27,7 +27,7 @@ import {import {compositePigmentOverPixelInto,isPigmentCompatibleCombineMode,-} from '../../utils/pigmentEngine';+} from '../../utils/spectralPigmentEngine';type ScratchpadBg = 'dark' | 'gray' | 'white';diff --git a/src/engine/NaturalPaintRenderer.ts b/src/engine/NaturalPaintRenderer.tsindex 4d62920..7d36d56 100644--- a/src/engine/NaturalPaintRenderer.ts+++ b/src/engine/NaturalPaintRenderer.ts@@ -33,7 +33,7 @@ import {SurfaceGradientField,} from '../utils/proceduralBrushTips';import { LayerPixelSampler } from '../utils/colorMixingEngine';-import { PigmentRgb, pigmentMixRgbOut } from '../utils/pigmentEngine';+import { PigmentRgb, pigmentMixRgbOut } from '../utils/spectralPigmentEngine';import {BRISTLE_SEG_STRIDE,BristleRibbonEngine,diff --git a/src/engine/WebGlBrushEngine.ts b/src/engine/WebGlBrushEngine.tsindex 2c9ed04..bf9fd61 100644--- a/src/engine/WebGlBrushEngine.ts+++ b/src/engine/WebGlBrushEngine.ts@@ -9,11 +9,7 @@ import {} from '../types/editor';import { getSurfaceTextureHeightField, SURFACE_TEXTURE_SIZE } from '../utils/proceduralBrushTips';import { performanceManager } from '../utils/performanceManager';-import {- PIGMENT_GLSL_WEBGL1,- getPigmentLutTexture,- invalidatePigmentLutTexture,-} from '../utils/pigmentEngine';+import { SPECTRAL_PIGMENT_GLSL } from '../utils/spectralPigmentEngine';import { dualBrushCompositeModeToIndex } from './DualBrushEngine';export function paperTextureModeToIndex(mode?: CspPaperTextureMode): number {@@ -177,7 +173,6 @@ uniform vec2 u_paperTexSize;uniform bool u_colorMixing;uniform bool u_perceptualMixing;uniform float u_pigmentStrength;-uniform sampler2D u_pigmentLut;uniform bool u_hasCanvasTexture;uniform sampler2D u_canvasTexture;uniform vec2 u_canvasResolution;@@ -239,7 +234,7 @@ float samplePaperTooth(vec2 worldPos) {return clamp(g, 0.0, 1.0);}-${PIGMENT_GLSL_WEBGL1}+${SPECTRAL_PIGMENT_GLSL}void main() {float dist = length(v_localPos);@@ -913,7 +908,6 @@ export class WebGlBrushEngine {private uColorMixingLoc: WebGLUniformLocation | null = null;private uPerceptualMixingLoc: WebGLUniformLocation | null = null;private uPigmentStrengthLoc: WebGLUniformLocation | null = null;- private uPigmentLutLoc: WebGLUniformLocation | null = null;private uHasCanvasTextureLoc: WebGLUniformLocation | null = null;private uCanvasTextureLoc: WebGLUniformLocation | null = null;private uAmountOfPaintLoc: WebGLUniformLocation | null = null;@@ -1067,7 +1061,6 @@ export class WebGlBrushEngine {}private clearTextureMapsState() {- invalidatePigmentLutTexture(this.gl, false);this.tipTexturesMap.clear();this.multiTipAtlasCache.clear();this.surfaceTexturesMap.clear();@@ -1279,7 +1272,6 @@ export class WebGlBrushEngine {this.uColorMixingLoc = gl.getUniformLocation(program, 'u_colorMixing');this.uPerceptualMixingLoc = gl.getUniformLocation(program, 'u_perceptualMixing');this.uPigmentStrengthLoc = gl.getUniformLocation(program, 'u_pigmentStrength');- this.uPigmentLutLoc = gl.getUniformLocation(program, 'u_pigmentLut');this.uHasCanvasTextureLoc = gl.getUniformLocation(program, 'u_hasCanvasTexture');this.uCanvasTextureLoc = gl.getUniformLocation(program, 'u_canvasTexture');this.uAmountOfPaintLoc = gl.getUniformLocation(program, 'u_amountOfPaint');@@ -2627,14 +2619,6 @@ export class WebGlBrushEngine {const pStr = settings.perceptualMixing === false ? 0.0 : (settings.naturalPaint?.pigmentMixingStrength ?? 1.0);gl.uniform1f(this.uPigmentStrengthLoc, Math.max(0, Math.min(1, pStr)));}- if (this.uPigmentLutLoc) {- const lutTex = getPigmentLutTexture(gl);- if (lutTex) {- gl.activeTexture(gl.TEXTURE2);- gl.bindTexture(gl.TEXTURE_2D, lutTex);- gl.uniform1i(this.uPigmentLutLoc, 2);- }- }if (enableColorMix && underlyingCanvas) {const canvasTex = this.uploadCanvasTexture(underlyingCanvas, {@@ -3131,14 +3115,6 @@ export class WebGlBrushEngine {const pStr = settings.perceptualMixing === false ? 0.0 : (settings.naturalPaint?.pigmentMixingStrength ?? 1.0);gl.uniform1f(this.uPigmentStrengthLoc, Math.max(0, Math.min(1, pStr)));}- if (this.uPigmentLutLoc) {- const lutTex = getPigmentLutTexture(gl);- if (lutTex) {- gl.activeTexture(gl.TEXTURE2);- gl.bindTexture(gl.TEXTURE_2D, lutTex);- gl.uniform1i(this.uPigmentLutLoc, 2);- }- }if (enableColorMix && underlyingCanvas) {const canvasTex = this.uploadCanvasTexture(underlyingCanvas, {diff --git a/src/hooks/drawing/brushStrokeSession/pointerDownHandler.ts b/src/hooks/drawing/brushStrokeSession/pointerDownHandler.tsindex f9fc484..e54355a 100644--- a/src/hooks/drawing/brushStrokeSession/pointerDownHandler.ts+++ b/src/hooks/drawing/brushStrokeSession/pointerDownHandler.ts@@ -2,7 +2,7 @@ import React from 'react';import { Point, isBrushLikeTool } from '../../../types/editor';import { calculateStrokeWidth, getGpu2DContext } from '../../../utils/canvasUtils';import { PaintReservoir, LayerPixelSampler } from '../../../utils/colorMixingEngine';-import { detectWhitePaperGround } from '../../../utils/pigmentEngine';+import { detectWhitePaperGround } from '../../../utils/spectralPigmentEngine';import {isNaturalPaintBrush,NaturalPaintStrokeSession,diff --git a/src/hooks/drawing/brushStrokeSession/pointerUpRasterCommit.ts b/src/hooks/drawing/brushStrokeSession/pointerUpRasterCommit.tsindex 9e113f2..9efb1aa 100644--- a/src/hooks/drawing/brushStrokeSession/pointerUpRasterCommit.ts+++ b/src/hooks/drawing/brushStrokeSession/pointerUpRasterCommit.ts@@ -28,7 +28,7 @@ import {} from '../../../engine/BrushEngine';import { isRibbonBrush } from '../../../engine/RibbonBrushEngine';import { LayerPixelSampler } from '../../../utils/colorMixingEngine';-import { detectWhitePaperGround } from '../../../utils/pigmentEngine';+import { detectWhitePaperGround } from '../../../utils/spectralPigmentEngine';import { buildStrokeSelectionMask } from '../../../utils/selectionUtils';import {DraftCanvasElement,diff --git a/src/hooks/drawing/brushStrokeSession/strokeHelpers.ts b/src/hooks/drawing/brushStrokeSession/strokeHelpers.tsindex 798d36c..60ba835 100644--- a/src/hooks/drawing/brushStrokeSession/strokeHelpers.ts+++ b/src/hooks/drawing/brushStrokeSession/strokeHelpers.ts@@ -25,7 +25,7 @@ import { ConstraintAxis } from '../../../types/perspective';import { uiStoreActions, useUiStore } from '../../../stores';import { recoveryService } from '../../../services/recoveryService';import { evaluateGlobalPressureCurve } from '../../../utils/pressureCurve';-import { isPigmentCompatibleCombineMode } from '../../../utils/pigmentEngine';+import { isPigmentCompatibleCombineMode } from '../../../utils/spectralPigmentEngine';import { DraftCanvasElement, applySelectionCanvasClip } from '../types';import { StrokeSessionState, TouchedTileSpan, UseBrushStrokeSessionParams } from './types';diff --git a/src/hooks/drawing/useRetouchTool.ts b/src/hooks/drawing/useRetouchTool.tsindex f5b871b..651c07d 100644--- a/src/hooks/drawing/useRetouchTool.ts+++ b/src/hooks/drawing/useRetouchTool.ts@@ -11,7 +11,7 @@ import {applyRetouchStrokeSegment,createRetouchStrokeSession,} from '../../utils/retouchEngine';-import { detectWhitePaperGround } from '../../utils/pigmentEngine';+import { detectWhitePaperGround } from '../../utils/spectralPigmentEngine';import { createSelectionMask } from '../../utils/tonalEngine';import { documentStore } from '../../utils/documentStore';import { TileFormat } from '../../utils/tileEngine';diff --git a/src/utils/__tests__/pigmentBlending.test.ts b/src/utils/__tests__/pigmentBlending.test.tsindex d941018..924d58d 100644--- a/src/utils/__tests__/pigmentBlending.test.ts+++ b/src/utils/__tests__/pigmentBlending.test.ts@@ -8,9 +8,12 @@ import {parseColorToRgb,} from '../colorMixingEngine';import {- buildPigmentLutRgba,+ SPECTRAL_PIGMENT_GLSL,+ SPECTRAL_STATE_SIZE,encodePigmentOverWhiteOut,-} from '../pigmentEngine';+ spectralStateToSrgbOut,+ srgbToSpectralStateOut,+} from '../spectralPigmentEngine';import {DEFAULT_RETOUCH_SETTINGS,applyRetouchStrokeSegment,@@ -99,10 +102,10 @@ describe('Physical Kubelka-Munk Pigment Blending Engine', () => {expect(pgPig.b - pgPig.r).toBeGreaterThan(60);expect(pgReg.g - pgReg.r).toBeLessThan(35);- // Ultramarine + White -> electric royal blue+ // Ultramarine + White -> bright royal blueconst ultramarine = { r: 25, g: 0, b: 89 };const umPig = mixSubtractivePigments(ultramarine, white, 0.5);- expect(umPig.b - umPig.r).toBeGreaterThan(110);+ expect(umPig.b - umPig.r).toBeGreaterThan(90);// Cobalt Violet + White -> vibrant magenta-violetconst cobaltViolet = { r: 78, g: 0, b: 66 };@@ -183,13 +186,30 @@ describe('Physical Kubelka-Munk Pigment Blending Engine', () => {}});- it('buildPigmentLutRgba generates a valid 512x512 RGBA8 GPU concentration lookup table', () => {- const rgba = buildPigmentLutRgba();- expect(rgba.length).toBe(512 * 512 * 4);- // Blue corner (r=0, g=0, b=63) sits at slice 63 (tile x=7*64, y=7*64)- const blueCornerIdx = ((7 * 64 + 0) * 512 + (7 * 64 + 0)) * 4;- expect(rgba[blueCornerIdx]).toBeGreaterThan(150); // high c0 (blue pigment)- expect(rgba[blueCornerIdx + 3]).toBe(255);+ it('encodes and decodes 12-float spectral Kubelka-Munk state accurately with pure analytical GLSL (zero LUT)', () => {+ const state = new Float32Array(SPECTRAL_STATE_SIZE);+ const out = { r: 0, g: 0, b: 0 };+ const samples = [+ [0, 33, 133],+ [252, 211, 0],+ [13, 27, 68],+ [128, 2, 46],+ [255, 255, 255],+ [0, 0, 0],+ ] as const;++ for (const [r, g, b] of samples) {+ srgbToSpectralStateOut(r, g, b, state, 0);+ expect(state[8]).toBeGreaterThan(0); // positive scattering S+ spectralStateToSrgbOut(state, 0, out);+ expect(Math.abs(out.r - r)).toBeLessThan(1.0);+ expect(Math.abs(out.g - g)).toBeLessThan(1.0);+ expect(Math.abs(out.b - b)).toBeLessThan(1.0);+ }++ expect(SPECTRAL_PIGMENT_GLSL).toContain('mixPigmentsGpu');+ expect(SPECTRAL_PIGMENT_GLSL).not.toContain('sampler2D');+ expect(SPECTRAL_PIGMENT_GLSL).not.toContain('textureLod');});it('compositeStrokeToTiles blends glazing strokes in pigment space and supports Regular vs Pigment on the same layer', () => {@@ -420,7 +440,7 @@ describe('Physical Kubelka-Munk Pigment Blending Engine', () => {expect(pigCerulean).toBeGreaterThan(regSlate + 60);});- it('Retouch Blur produces vibrant Green across a Blue | Yellow seam via 7-channel pigment latent convolution', () => {+ it('Retouch Blur produces vibrant Green across a Blue | Yellow seam via 12-channel spectral Kubelka-Munk convolution', () => {const runBlur = (pigmentBlending: boolean) => {const grid = new SparseTileGrid();const dims = { width: 128, height: 128 };diff --git a/src/utils/colorMixingEngine.ts b/src/utils/colorMixingEngine.tsindex 691e577..f3eb143 100644--- a/src/utils/colorMixingEngine.ts+++ b/src/utils/colorMixingEngine.ts@@ -142,20 +142,20 @@ export function oklabToLinearRgb(L: number, a: number, b: number): { r: number;import { SparseTileGrid, TILE_SIZE, globalTileGrid } from './tileEngine';import type { BristleProfilePreset, BristleSettings, NaturalMixingMode, NaturalPaintSettings } from '../types/editor';import {- PIGMENT_LATENT_SIZE,- pigmentLatentToSrgbOut,+ SPECTRAL_STATE_SIZE,+ spectralStateToSrgbOut,pigmentMixRgbOut,- srgbToPigmentLatentOut,-} from './pigmentEngine';+ srgbToSpectralStateOut,+} from './spectralPigmentEngine';// --------------------------------------------------------------// Physical Kubelka-Munk 4-Pigment + Residual Latent Space Engine-// Uses a calibrated 64x64x64 3D pigment concentration LUT + 20-term cubic-// Bernstein polynomial (7-float latent space: 4 physical pigment concentrations-// + 3 linear sRGB residuals).+// Clean-Room 8-Band Spectral Two-Constant Kubelka-Munk + Saunderson Engine+// Uses analytical 8-band visible reflectance spectra (410..680 nm) with+// Kubelka-Munk (1931) absorption/scattering and Saunderson (1942) air-binder+// reflection correction (12-float linear spectral state, zero external LUTs).// Guarantees:// - Blue + Yellow -> vibrant natural Green (no muddy gray)-// - Dark Navy / Crimson / Emerald + White -> luminous Cerulean / Magenta / Teal+// - Dark Navy / Crimson / Emerald + White -> luminous Cerulean / Rose / Teal// - Red + Blue -> rich Violet; Red + Yellow -> luminous Orange// - Exact self-mix invariance: mix(c, c, t) === c// - Exact neutral-gray invariance: Black + White stays strictly neutral gray@@ -179,7 +179,7 @@ function fastOneMinusExpNeg(x: number): number {}/**- * Encodes an sRGB color [0..255] into 7 Kubelka-Munk pigment latent components+ * Encodes an sRGB color [0..255] into 12 spectral Kubelka-Munk state components* written starting at `offset`. Useful for separable pigment convolution (Pigment Blur).*/export function encodeSrgbToPigmentLatent(@@ -189,32 +189,32 @@ export function encodeSrgbToPigmentLatent(out: Float32Array,offset: number): void {- srgbToPigmentLatentOut(r, g, b, out, offset);+ srgbToSpectralStateOut(r, g, b, out, offset);}/**- * Decodes 7 Kubelka-Munk pigment latent components back to sRGB [0..255].+ * Decodes 12 spectral Kubelka-Munk state components back to sRGB [0..255].*/export function decodePigmentLatentToSrgb(buf: Float32Array,offset: number,out: RgbColor): RgbColor {- return pigmentLatentToSrgbOut(buf, offset, out);+ return spectralStateToSrgbOut(buf, offset, out);}/**- * Backward-compatible alias for encoding sRGB into 7-channel pigment latent space.+ * Backward-compatible alias for encoding sRGB into 12-channel spectral K/S state.*/export const encodeSrgbToSpectralKS = encodeSrgbToPigmentLatent;/**- * Backward-compatible alias for decoding 7-channel pigment latent space to sRGB.+ * Backward-compatible alias for decoding 12-channel spectral K/S state to sRGB.*/export const decodeSpectralKSToSrgb = decodePigmentLatentToSrgb;/**- * Zero-allocation physical Kubelka-Munk pigment mixer writing directly into `out`.+ * Zero-allocation physical 8-band spectral Kubelka-Munk pigment mixer writing directly into `out`.* `pigmentStrength` in [0..1]: 0.0 = regular sRGB blend, 1.0 = full physical pigment blend.*/export function mixPigmentRgbOut(@@ -232,18 +232,18 @@ export function mixPigmentRgbOut(}// Reusable scratch buffers for zero-allocation pigment sampling & deposit in LayerPixelSampler-const _samplerLatTmp = new Float32Array(PIGMENT_LATENT_SIZE);-const _samplerLatAcc = new Float32Array(PIGMENT_LATENT_SIZE);-const _samplerRgbScratch: RgbColor = { r: 0, g: 0, b: 0 };+const _samplerLatTmp = new Float32Array(SPECTRAL_STATE_SIZE);+const _samplerLatAcc = new Float32Array(SPECTRAL_STATE_SIZE);const _sample5TapR = new Uint8Array(5);const _sample5TapG = new Uint8Array(5);const _sample5TapB = new Uint8Array(5);const _sample5TapW = new Float32Array(5);+const _samplerRgbScratch: RgbColor = { r: 0, g: 0, b: 0 };/**- * Subtractive Pigment Mixing (Physical Kubelka-Munk 4-Pigment Latent Engine).+ * Subtractive Pigment Mixing (8-Band Spectral Two-Constant Kubelka-Munk Engine).* Simulates real-world paint absorption and scattering where Blue + Yellow = vibrant Green,- * Dark Navy / Crimson / Emerald + White = luminous Cerulean / Magenta / Teal,+ * Dark Navy / Crimson / Emerald + White = luminous Cerulean / Rose / Teal,* Red + Blue = Violet, Red + Yellow = Orange.*/export function mixSubtractivePigments(@@ -473,8 +473,8 @@ export class LayerPixelSampler {if (alpha > 4) {const radialWeight = geomWeight * (alpha / 255);if (usePigment) {- srgbToPigmentLatentOut(tile[idx], tile[idx + 1], tile[idx + 2], _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * radialWeight;+ srgbToSpectralStateOut(tile[idx], tile[idx + 1], tile[idx + 2], _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * radialWeight;} else {sumR += tile[idx] * radialWeight;sumG += tile[idx + 1] * radialWeight;@@ -489,8 +489,8 @@ export class LayerPixelSampler {if (weightSum > 0.001 && radialAreaSum > 0) {if (usePigment) {const invW = 1 / weightSum;- for (let c = 0; c < 7; c++) _samplerLatAcc[c] *= invW;- pigmentLatentToSrgbOut(_samplerLatAcc, 0, _samplerRgbScratch);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] *= invW;+ spectralStateToSrgbOut(_samplerLatAcc, 0, _samplerRgbScratch);return {r: _samplerRgbScratch.r,g: _samplerRgbScratch.g,@@ -551,8 +551,8 @@ export class LayerPixelSampler {if (alpha > 4) {const radialWeight = geomWeight * (alpha / 255);if (usePigment) {- srgbToPigmentLatentOut(this.data[idx], this.data[idx + 1], this.data[idx + 2], _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * radialWeight;+ srgbToSpectralStateOut(this.data[idx], this.data[idx + 1], this.data[idx + 2], _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * radialWeight;} else {sumR += this.data[idx] * radialWeight;sumG += this.data[idx + 1] * radialWeight;@@ -567,8 +567,8 @@ export class LayerPixelSampler {if (weightSum > 0.001 && radialAreaSum > 0) {if (usePigment) {const invW = 1 / weightSum;- for (let c = 0; c < 7; c++) _samplerLatAcc[c] *= invW;- pigmentLatentToSrgbOut(_samplerLatAcc, 0, _samplerRgbScratch);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] *= invW;+ spectralStateToSrgbOut(_samplerLatAcc, 0, _samplerRgbScratch);return {r: _samplerRgbScratch.r,g: _samplerRgbScratch.g,@@ -864,23 +864,11 @@ export class LayerPixelSampler {_samplerLatAcc.fill(0);for (let k = 0; k < tapCount; k++) {const tw = tapW[k];- srgbToPigmentLatentOut(tapR[k], tapG[k], tapB[k], _samplerLatTmp, 0);- _samplerLatAcc[0] += _samplerLatTmp[0] * tw;- _samplerLatAcc[1] += _samplerLatTmp[1] * tw;- _samplerLatAcc[2] += _samplerLatTmp[2] * tw;- _samplerLatAcc[3] += _samplerLatTmp[3] * tw;- _samplerLatAcc[4] += _samplerLatTmp[4] * tw;- _samplerLatAcc[5] += _samplerLatTmp[5] * tw;- _samplerLatAcc[6] += _samplerLatTmp[6] * tw;+ srgbToSpectralStateOut(tapR[k], tapG[k], tapB[k], _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * tw;}- _samplerLatAcc[0] *= invW;- _samplerLatAcc[1] *= invW;- _samplerLatAcc[2] *= invW;- _samplerLatAcc[3] *= invW;- _samplerLatAcc[4] *= invW;- _samplerLatAcc[5] *= invW;- _samplerLatAcc[6] *= invW;- pigmentLatentToSrgbOut(_samplerLatAcc, 0, out);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] *= invW;+ spectralStateToSrgbOut(_samplerLatAcc, 0, out);} else {out.r = sumR * invW;out.g = sumG * invW;@@ -956,23 +944,11 @@ export class LayerPixelSampler {_samplerLatAcc.fill(0);for (let k = 0; k < tapCount; k++) {const tw = tapW[k];- srgbToPigmentLatentOut(tapR[k], tapG[k], tapB[k], _samplerLatTmp, 0);- _samplerLatAcc[0] += _samplerLatTmp[0] * tw;- _samplerLatAcc[1] += _samplerLatTmp[1] * tw;- _samplerLatAcc[2] += _samplerLatTmp[2] * tw;- _samplerLatAcc[3] += _samplerLatTmp[3] * tw;- _samplerLatAcc[4] += _samplerLatTmp[4] * tw;- _samplerLatAcc[5] += _samplerLatTmp[5] * tw;- _samplerLatAcc[6] += _samplerLatTmp[6] * tw;+ srgbToSpectralStateOut(tapR[k], tapG[k], tapB[k], _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * tw;}- _samplerLatAcc[0] *= invW;- _samplerLatAcc[1] *= invW;- _samplerLatAcc[2] *= invW;- _samplerLatAcc[3] *= invW;- _samplerLatAcc[4] *= invW;- _samplerLatAcc[5] *= invW;- _samplerLatAcc[6] *= invW;- pigmentLatentToSrgbOut(_samplerLatAcc, 0, out);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] *= invW;+ spectralStateToSrgbOut(_samplerLatAcc, 0, out);} else {out.r = sumR * invW;out.g = sumG * invW;@@ -1062,23 +1038,23 @@ export class LayerPixelSampler {if (differs) {_samplerLatAcc.fill(0);if (aw00 > 0) {- srgbToPigmentLatentOut(r00, g00, b00, _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw00;+ srgbToSpectralStateOut(r00, g00, b00, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw00;}if (aw10 > 0) {- srgbToPigmentLatentOut(r10, g10, b10, _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw10;+ srgbToSpectralStateOut(r10, g10, b10, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw10;}if (aw01 > 0) {- srgbToPigmentLatentOut(r01, g01, b01, _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw01;+ srgbToSpectralStateOut(r01, g01, b01, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw01;}if (aw11 > 0) {- srgbToPigmentLatentOut(r11, g11, b11, _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw11;+ srgbToSpectralStateOut(r11, g11, b11, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw11;}- for (let c = 0; c < 7; c++) _samplerLatAcc[c] *= inv;- pigmentLatentToSrgbOut(_samplerLatAcc, 0, out);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] *= inv;+ spectralStateToSrgbOut(_samplerLatAcc, 0, out);out.a = sumA > 1 ? 1 : sumA;return out;}@@ -1122,8 +1098,8 @@ export class LayerPixelSampler {if (ta > 0.002) {const aw = wt * ta;if (usePigment) {- srgbToPigmentLatentOut(tr, tg, tb, _samplerLatTmp, 0);- for (let c = 0; c < 7; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw;+ srgbToSpectralStateOut(tr, tg, tb, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw;} else {sumR += tr * aw;sumG += tg * aw;@@ -1141,8 +1117,8 @@ export class LayerPixelSampler {if (sumA > 0.002) {const inv = 1 / sumA;if (usePigment) {- for (let c = 0; c < 7; c++) _samplerLatAcc[c] *= inv;- pigmentLatentToSrgbOut(_samplerLatAcc, 0, out);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] *= inv;+ spectralStateToSrgbOut(_samplerLatAcc, 0, out);} else {out.r = sumR * inv;out.g = sumG * inv;@@ -1159,9 +1135,9 @@ export class LayerPixelSampler {}/**- * Zero-allocation O(1) bilinear sample directly into 7-channel Kubelka-Munk latent space+ * Zero-allocation O(1) bilinear sample directly into 12-channel spectral Kubelka-Munk state* for SpatialTipMixer: eliminates redundant encode/decode roundtrips per texel!- * Writes linear RGB into `out` (and 7 latent floats into `outLat[outLatOff..+6]` when `out.a > 0.002`).+ * Writes linear RGB into `out` (and 12 spectral floats into `outLat[outLatOff..+11]` when `out.a > 0.002`).*/public sampleBilinearLatentFast(cx: number,@@ -1288,38 +1264,30 @@ export class LayerPixelSampler {(aw11 > 0 && Math.abs(r11 - r00) + Math.abs(g11 - g00) + Math.abs(b11 - b00) > 12);if (!differs) {- srgbToPigmentLatentOut(linR, linG, linB, outLat, outLatOff);+ srgbToSpectralStateOut(linR, linG, linB, outLat, outLatOff);return out;}- let l0 = 0, l1 = 0, l2 = 0, l3 = 0, l4 = 0, l5 = 0, l6 = 0;+ _samplerLatAcc.fill(0);if (aw00 > 0) {- srgbToPigmentLatentOut(r00, g00, b00, _samplerLatTmp, 0);- l0 += _samplerLatTmp[0] * aw00; l1 += _samplerLatTmp[1] * aw00; l2 += _samplerLatTmp[2] * aw00;- l3 += _samplerLatTmp[3] * aw00; l4 += _samplerLatTmp[4] * aw00; l5 += _samplerLatTmp[5] * aw00; l6 += _samplerLatTmp[6] * aw00;+ srgbToSpectralStateOut(r00, g00, b00, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw00;}if (aw10 > 0) {- srgbToPigmentLatentOut(r10, g10, b10, _samplerLatTmp, 0);- l0 += _samplerLatTmp[0] * aw10; l1 += _samplerLatTmp[1] * aw10; l2 += _samplerLatTmp[2] * aw10;- l3 += _samplerLatTmp[3] * aw10; l4 += _samplerLatTmp[4] * aw10; l5 += _samplerLatTmp[5] * aw10; l6 += _samplerLatTmp[6] * aw10;+ srgbToSpectralStateOut(r10, g10, b10, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw10;}if (aw01 > 0) {- srgbToPigmentLatentOut(r01, g01, b01, _samplerLatTmp, 0);- l0 += _samplerLatTmp[0] * aw01; l1 += _samplerLatTmp[1] * aw01; l2 += _samplerLatTmp[2] * aw01;- l3 += _samplerLatTmp[3] * aw01; l4 += _samplerLatTmp[4] * aw01; l5 += _samplerLatTmp[5] * aw01; l6 += _samplerLatTmp[6] * aw01;+ srgbToSpectralStateOut(r01, g01, b01, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw01;}if (aw11 > 0) {- srgbToPigmentLatentOut(r11, g11, b11, _samplerLatTmp, 0);- l0 += _samplerLatTmp[0] * aw11; l1 += _samplerLatTmp[1] * aw11; l2 += _samplerLatTmp[2] * aw11;- l3 += _samplerLatTmp[3] * aw11; l4 += _samplerLatTmp[4] * aw11; l5 += _samplerLatTmp[5] * aw11; l6 += _samplerLatTmp[6] * aw11;+ srgbToSpectralStateOut(r11, g11, b11, _samplerLatTmp, 0);+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) _samplerLatAcc[c] += _samplerLatTmp[c] * aw11;+ }+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ outLat[outLatOff + c] = _samplerLatAcc[c] * inv;}- outLat[outLatOff] = l0 * inv;- outLat[outLatOff + 1] = l1 * inv;- outLat[outLatOff + 2] = l2 * inv;- outLat[outLatOff + 3] = l3 * inv;- outLat[outLatOff + 4] = l4 * inv;- outLat[outLatOff + 5] = l5 * inv;- outLat[outLatOff + 6] = l6 * inv;return out;}@@ -2377,8 +2345,8 @@ export class SpatialTipMixer {private uniG: number;private uniB: number;private uniCarry: number = 0;- private readonly uniLat = new Float32Array(PIGMENT_LATENT_SIZE);- private readonly freshLat = new Float32Array(PIGMENT_LATENT_SIZE);+ private readonly uniLat = new Float32Array(SPECTRAL_STATE_SIZE);+ private readonly freshLat = new Float32Array(SPECTRAL_STATE_SIZE);private cachedFreshR: number = -9999;private cachedFreshG: number = -9999;private cachedFreshB: number = -9999;@@ -2393,11 +2361,11 @@ export class SpatialTipMixer {private readonly tmpR = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM);private readonly tmpG = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM);private readonly tmpB = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM);- private readonly latBuf = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM * PIGMENT_LATENT_SIZE);- private readonly latTmp = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM * PIGMENT_LATENT_SIZE);- private readonly canvasLatSum = new Float32Array(PIGMENT_LATENT_SIZE);- private readonly latSampleTmp = new Float32Array(PIGMENT_LATENT_SIZE);- private readonly latEqTmp = new Float32Array(PIGMENT_LATENT_SIZE);+ private readonly latBuf = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM * SPECTRAL_STATE_SIZE);+ private readonly latTmp = new Float32Array(MAX_SPATIAL_COLOR_DIM * MAX_SPATIAL_COLOR_DIM * SPECTRAL_STATE_SIZE);+ private readonly canvasLatSum = new Float32Array(SPECTRAL_STATE_SIZE);+ private readonly latSampleTmp = new Float32Array(SPECTRAL_STATE_SIZE);+ private readonly latEqTmp = new Float32Array(SPECTRAL_STATE_SIZE);private readonly surfacePool = new Map<number, SpatialSurfaceEntry>();private scratchCanvas: HTMLCanvasElement | null = null;@@ -2583,7 +2551,7 @@ export class SpatialTipMixer {const freshLat = this.freshLat;if (usePigment) {if (freshR !== this.cachedFreshR || freshG !== this.cachedFreshG || freshB !== this.cachedFreshB) {- srgbToPigmentLatentOut(freshR, freshG, freshB, freshLat, 0);+ srgbToSpectralStateOut(freshR, freshG, freshB, freshLat, 0);this.cachedFreshR = freshR;this.cachedFreshG = freshG;this.cachedFreshB = freshB;@@ -2591,21 +2559,21 @@ export class SpatialTipMixer {// If stroke switched into pigment mode after already initializing in linear mode, sync latBuf onceif (!firstTouch && !this.latInitialized) {for (let i = 0; i < totalTexels; i++) {- srgbToPigmentLatentOut(this.bufR[i], this.bufG[i], this.bufB[i], this.latBuf, i * 7);+ srgbToSpectralStateOut(this.bufR[i], this.bufG[i], this.bufB[i], this.latBuf, i * SPECTRAL_STATE_SIZE);if (partialPigment) {this.linR[i] = this.bufR[i];this.linG[i] = this.bufG[i];this.linB[i] = this.bufB[i];}}- srgbToPigmentLatentOut(this.uniR, this.uniG, this.uniB, this.uniLat, 0);+ srgbToSpectralStateOut(this.uniR, this.uniG, this.uniB, this.uniLat, 0);this.latInitialized = true;}}// 1. Sample underlying canvas & update 2D oriented paint buffer- // When usePigment is active, ALL mixing & diffusion run directly in 7-channel Kubelka-Munk latent- // space (7 linear float ops/texel) with ONE single decode at the end of Step 2 — 15-25x faster!+ // When usePigment is active, ALL mixing & diffusion run directly in 12-channel spectral Kubelka-Munk+ // state with ONE single decode at the end of Step 2 — 15-25x faster!const stretchPickupBoost = 1.0 + clampedStretch * (isRunning ? 1.45 : 1.15);const wFreshBase = clampedAmount * (isRunning ? 0.72 : 0.88);const wFreshFirst = clampedAmount * (isRunning ? 0.75 : 0.9);@@ -2625,14 +2593,8 @@ export class SpatialTipMixer {this.bufA[i] = 1.0;this.bufCarry[i] = 0.0;if (usePigment) {- const lo = i * 7;- latBuf[lo] = freshLat[0];- latBuf[lo + 1] = freshLat[1];- latBuf[lo + 2] = freshLat[2];- latBuf[lo + 3] = freshLat[3];- latBuf[lo + 4] = freshLat[4];- latBuf[lo + 5] = freshLat[5];- latBuf[lo + 6] = freshLat[6];+ const lo = i * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latBuf[lo + c] = freshLat[c];}}sumBufR = freshR * totalTexels;@@ -2649,7 +2611,7 @@ export class SpatialTipMixer {let wy = p.y + lx0Start * sinA + ly * cosA;for (let gx = 0; gx < gw; gx++) {const idx = rowOff + gx;- const lo = idx * 7;+ const lo = idx * SPECTRAL_STATE_SIZE;const sampled = sampler? sampler.sampleBilinearLatentFast(wx, wy, this.sampleScratch, latSamp, 0): ((this.sampleScratch.a = 0), this.sampleScratch);@@ -2663,13 +2625,7 @@ export class SpatialTipMixer {canvasSumR += sampled.r * ca;canvasSumG += sampled.g * ca;canvasSumB += sampled.b * ca;- this.canvasLatSum[0] += latSamp[0] * ca;- this.canvasLatSum[1] += latSamp[1] * ca;- this.canvasLatSum[2] += latSamp[2] * ca;- this.canvasLatSum[3] += latSamp[3] * ca;- this.canvasLatSum[4] += latSamp[4] * ca;- this.canvasLatSum[5] += latSamp[5] * ca;- this.canvasLatSum[6] += latSamp[6] * ca;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) this.canvasLatSum[c] += latSamp[c] * ca;canvasAlphaSum += ca;}}@@ -2677,15 +2633,12 @@ export class SpatialTipMixer {if (firstTouch) {if (isPureSmudge) {if (ca > 0.002) {- latBuf[lo] = latSamp[0]; latBuf[lo + 1] = latSamp[1]; latBuf[lo + 2] = latSamp[2];- latBuf[lo + 3] = latSamp[3]; latBuf[lo + 4] = latSamp[4]; latBuf[lo + 5] = latSamp[5];- latBuf[lo + 6] = latSamp[6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latBuf[lo + c] = latSamp[c];if (partialPigment) {this.linR[idx] = sampled.r; this.linG[idx] = sampled.g; this.linB[idx] = sampled.b;}} else {- latBuf[lo] = 0; latBuf[lo + 1] = 0; latBuf[lo + 2] = 0; latBuf[lo + 3] = 1;- latBuf[lo + 4] = 0; latBuf[lo + 5] = 0; latBuf[lo + 6] = 0;+ srgbToSpectralStateOut(255, 255, 255, latBuf, lo);if (partialPigment) {this.linR[idx] = 255; this.linG[idx] = 255; this.linB[idx] = 255;}@@ -2696,13 +2649,9 @@ export class SpatialTipMixer {const wCanvas = (1.0 - clampedAmount) * stretchPickupBoost * ca;const eqFresh = wFreshFirst / Math.max(1e-4, wFreshFirst + wCanvas);const invEq = 1.0 - eqFresh;- latBuf[lo] = latSamp[0] * invEq + freshLat[0] * eqFresh;- latBuf[lo + 1] = latSamp[1] * invEq + freshLat[1] * eqFresh;- latBuf[lo + 2] = latSamp[2] * invEq + freshLat[2] * eqFresh;- latBuf[lo + 3] = latSamp[3] * invEq + freshLat[3] * eqFresh;- latBuf[lo + 4] = latSamp[4] * invEq + freshLat[4] * eqFresh;- latBuf[lo + 5] = latSamp[5] * invEq + freshLat[5] * eqFresh;- latBuf[lo + 6] = latSamp[6] * invEq + freshLat[6] * eqFresh;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latBuf[lo + c] = latSamp[c] * invEq + freshLat[c] * eqFresh;+ }if (partialPigment) {this.linR[idx] = sampled.r * invEq + freshR * eqFresh;this.linG[idx] = sampled.g * invEq + freshG * eqFresh;@@ -2711,9 +2660,7 @@ export class SpatialTipMixer {this.bufA[idx] = 1.0;this.bufCarry[idx] = Math.min(1.0, ca * (1.0 - eqFresh * 0.65));} else {- latBuf[lo] = freshLat[0]; latBuf[lo + 1] = freshLat[1]; latBuf[lo + 2] = freshLat[2];- latBuf[lo + 3] = freshLat[3]; latBuf[lo + 4] = freshLat[4]; latBuf[lo + 5] = freshLat[5];- latBuf[lo + 6] = freshLat[6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latBuf[lo + c] = freshLat[c];if (partialPigment) {this.linR[idx] = freshR; this.linG[idx] = freshG; this.linB[idx] = freshB;}@@ -2725,22 +2672,16 @@ export class SpatialTipMixer {const rate = Math.min(1.0, pickupRate * (0.55 + 0.45 * ca));const curA = this.bufA[idx];if (curA <= 0.02) {- latBuf[lo] = latSamp[0]; latBuf[lo + 1] = latSamp[1]; latBuf[lo + 2] = latSamp[2];- latBuf[lo + 3] = latSamp[3]; latBuf[lo + 4] = latSamp[4]; latBuf[lo + 5] = latSamp[5];- latBuf[lo + 6] = latSamp[6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latBuf[lo + c] = latSamp[c];if (partialPigment) {this.linR[idx] = sampled.r; this.linG[idx] = sampled.g; this.linB[idx] = sampled.b;}this.bufA[idx] = ca;this.bufCarry[idx] = ca;} else {- latBuf[lo] += (latSamp[0] - latBuf[lo]) * rate;- latBuf[lo + 1] += (latSamp[1] - latBuf[lo + 1]) * rate;- latBuf[lo + 2] += (latSamp[2] - latBuf[lo + 2]) * rate;- latBuf[lo + 3] += (latSamp[3] - latBuf[lo + 3]) * rate;- latBuf[lo + 4] += (latSamp[4] - latBuf[lo + 4]) * rate;- latBuf[lo + 5] += (latSamp[5] - latBuf[lo + 5]) * rate;- latBuf[lo + 6] += (latSamp[6] - latBuf[lo + 6]) * rate;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latBuf[lo + c] += (latSamp[c] - latBuf[lo + c]) * rate;+ }if (partialPigment) {this.linR[idx] += (sampled.r - this.linR[idx]) * rate;this.linG[idx] += (sampled.g - this.linG[idx]) * rate;@@ -2754,13 +2695,9 @@ export class SpatialTipMixer {const wCanvas = (1.0 - clampedAmount) * stretchPickupBoost * ca;const eqFresh = wFreshBase / Math.max(1e-4, wFreshBase + wCanvas);const invEq = 1.0 - eqFresh;- latBuf[lo] += (latSamp[0] * invEq + freshLat[0] * eqFresh - latBuf[lo]) * pickupRate;- latBuf[lo + 1] += (latSamp[1] * invEq + freshLat[1] * eqFresh - latBuf[lo + 1]) * pickupRate;- latBuf[lo + 2] += (latSamp[2] * invEq + freshLat[2] * eqFresh - latBuf[lo + 2]) * pickupRate;- latBuf[lo + 3] += (latSamp[3] * invEq + freshLat[3] * eqFresh - latBuf[lo + 3]) * pickupRate;- latBuf[lo + 4] += (latSamp[4] * invEq + freshLat[4] * eqFresh - latBuf[lo + 4]) * pickupRate;- latBuf[lo + 5] += (latSamp[5] * invEq + freshLat[5] * eqFresh - latBuf[lo + 5]) * pickupRate;- latBuf[lo + 6] += (latSamp[6] * invEq + freshLat[6] * eqFresh - latBuf[lo + 6]) * pickupRate;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latBuf[lo + c] += (latSamp[c] * invEq + freshLat[c] * eqFresh - latBuf[lo + c]) * pickupRate;+ }if (partialPigment) {this.linR[idx] += (sampled.r * invEq + freshR * eqFresh - this.linR[idx]) * pickupRate;this.linG[idx] += (sampled.g * invEq + freshG * eqFresh - this.linG[idx]) * pickupRate;@@ -2775,13 +2712,9 @@ export class SpatialTipMixer {this.bufA[idx] = Math.max(0, this.bufA[idx] * (1.0 - smudgeDecay));this.bufCarry[idx] = Math.max(0, this.bufCarry[idx] * (1.0 - carryDecay));} else {- latBuf[lo] += (freshLat[0] - latBuf[lo]) * recoverRate;- latBuf[lo + 1] += (freshLat[1] - latBuf[lo + 1]) * recoverRate;- latBuf[lo + 2] += (freshLat[2] - latBuf[lo + 2]) * recoverRate;- latBuf[lo + 3] += (freshLat[3] - latBuf[lo + 3]) * recoverRate;- latBuf[lo + 4] += (freshLat[4] - latBuf[lo + 4]) * recoverRate;- latBuf[lo + 5] += (freshLat[5] - latBuf[lo + 5]) * recoverRate;- latBuf[lo + 6] += (freshLat[6] - latBuf[lo + 6]) * recoverRate;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latBuf[lo + c] += (freshLat[c] - latBuf[lo + c]) * recoverRate;+ }if (partialPigment) {this.linR[idx] += (freshR - this.linR[idx]) * recoverRate;this.linG[idx] += (freshG - this.linG[idx]) * recoverRate;@@ -2915,7 +2848,7 @@ export class SpatialTipMixer {if (usePigment) {const uniLat = this.uniLat;if (hasCanvasAvg) {- for (let c = 0; c < 7; c++) latSamp[c] = this.canvasLatSum[c] * invCa;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latSamp[c] = this.canvasLatSum[c] * invCa;}const avgLinR = hasCanvasAvg ? canvasSumR * invCa : 0;const avgLinG = hasCanvasAvg ? canvasSumG * invCa : 0;@@ -2926,13 +2859,13 @@ export class SpatialTipMixer {const wCanvasUni = (1.0 - clampedAmount) * stretchPickupBoost * avgCanvasCa;const uniFresh = wFreshFirst / Math.max(1e-4, wFreshFirst + wCanvasUni);const invUF = 1.0 - uniFresh;- for (let c = 0; c < 7; c++) uniLat[c] = latSamp[c] * invUF + freshLat[c] * uniFresh;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) uniLat[c] = latSamp[c] * invUF + freshLat[c] * uniFresh;this.uniR = avgLinR * invUF + freshR * uniFresh;this.uniG = avgLinG * invUF + freshG * uniFresh;this.uniB = avgLinB * invUF + freshB * uniFresh;this.uniCarry = Math.min(1.0, avgCanvasCa * (1.0 - uniFresh * 0.65));} else {- for (let c = 0; c < 7; c++) uniLat[c] = freshLat[c];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) uniLat[c] = freshLat[c];this.uniR = freshR;this.uniG = freshG;this.uniB = freshB;@@ -2942,7 +2875,7 @@ export class SpatialTipMixer {const wCanvasUni = (1.0 - clampedAmount) * stretchPickupBoost * avgCanvasCa;const uniFresh = wFreshBase / Math.max(1e-4, wFreshBase + wCanvasUni);const invUF = 1.0 - uniFresh;- for (let c = 0; c < 7; c++) {+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {latEq[c] = latSamp[c] * invUF + freshLat[c] * uniFresh;uniLat[c] += (latEq[c] - uniLat[c]) * pickupRate;}@@ -2954,7 +2887,7 @@ export class SpatialTipMixer {const targetCarry = Math.min(1.0, avgCanvasCa * (1.0 - uniFresh * 0.55));this.uniCarry += (targetCarry - this.uniCarry) * pickupRate;} else if (step > 0) {- for (let c = 0; c < 7; c++) uniLat[c] += (freshLat[c] - uniLat[c]) * recoverRate;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) uniLat[c] += (freshLat[c] - uniLat[c]) * recoverRate;if (partialPigment) {this.uniR += (freshR - this.uniR) * recoverRate;this.uniG += (freshG - this.uniG) * recoverRate;@@ -2963,16 +2896,11 @@ export class SpatialTipMixer {this.uniCarry = Math.max(0, this.uniCarry * (1.0 - carryDecay));}- const u0 = uniLat[0], u1 = uniLat[1], u2 = uniLat[2], u3 = uniLat[3], u4 = uniLat[4], u5 = uniLat[5], u6 = uniLat[6];for (let i = 0; i < totalTexels; i++) {- const lo = i * 7;- latBuf[lo] = latBuf[lo] * invU + u0 * u;- latBuf[lo + 1] = latBuf[lo + 1] * invU + u1 * u;- latBuf[lo + 2] = latBuf[lo + 2] * invU + u2 * u;- latBuf[lo + 3] = latBuf[lo + 3] * invU + u3 * u;- latBuf[lo + 4] = latBuf[lo + 4] * invU + u4 * u;- latBuf[lo + 5] = latBuf[lo + 5] * invU + u5 * u;- latBuf[lo + 6] = latBuf[lo + 6] * invU + u6 * u;+ const lo = i * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latBuf[lo + c] = latBuf[lo + c] * invU + uniLat[c] * u;+ }if (partialPigment) {this.linR[i] = this.linR[i] * invU + this.uniR * u;this.linG[i] = this.linG[i] * invU + this.uniG * u;@@ -3034,7 +2962,7 @@ export class SpatialTipMixer {}// 2. Lateral / isotropic pigment diffusion & CSP useBlur optical smoothing:- // - When pigment mode is active, `this.latBuf` is ALREADY in 7-channel Kubelka-Munk latent space+ // - When pigment mode is active, `this.latBuf` is ALREADY in 12-channel spectral Kubelka-Munk state// so convolution runs directly with ZERO per-dab re-encoding!if (!isFullFresh && blendUniformity < 0.999 && gw >= 4 && gh >= 4) {if (blurAmt > 0.005) {@@ -3048,12 +2976,10 @@ export class SpatialTipMixer {const row = gy * gw;for (let gx = 0; gx < gw; gx++) {const i = row + gx;- const lo = i * 7;+ const lo = i * SPECTRAL_STATE_SIZE;const aC = this.bufA[i];if (aC <= 0.001) {- latTmp[lo] = latBuf[lo]; latTmp[lo + 1] = latBuf[lo + 1]; latTmp[lo + 2] = latBuf[lo + 2];- latTmp[lo + 3] = latBuf[lo + 3]; latTmp[lo + 4] = latBuf[lo + 4]; latTmp[lo + 5] = latBuf[lo + 5];- latTmp[lo + 6] = latBuf[lo + 6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latTmp[lo + c] = latBuf[lo + c];continue;}const iL = row + (gx > 0 ? gx - 1 : 0);@@ -3064,19 +2990,13 @@ export class SpatialTipMixer {const wSum = wL + wC + wR;if (wSum > 1e-4) {const invW = 1.0 / wSum;- const loL = iL * 7;- const loR = iR * 7;- latTmp[lo] = (latBuf[loL] * wL + latBuf[lo] * wC + latBuf[loR] * wR) * invW;- latTmp[lo + 1] = (latBuf[loL + 1] * wL + latBuf[lo + 1] * wC + latBuf[loR + 1] * wR) * invW;- latTmp[lo + 2] = (latBuf[loL + 2] * wL + latBuf[lo + 2] * wC + latBuf[loR + 2] * wR) * invW;- latTmp[lo + 3] = (latBuf[loL + 3] * wL + latBuf[lo + 3] * wC + latBuf[loR + 3] * wR) * invW;- latTmp[lo + 4] = (latBuf[loL + 4] * wL + latBuf[lo + 4] * wC + latBuf[loR + 4] * wR) * invW;- latTmp[lo + 5] = (latBuf[loL + 5] * wL + latBuf[lo + 5] * wC + latBuf[loR + 5] * wR) * invW;- latTmp[lo + 6] = (latBuf[loL + 6] * wL + latBuf[lo + 6] * wC + latBuf[loR + 6] * wR) * invW;+ const loL = iL * SPECTRAL_STATE_SIZE;+ const loR = iR * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latTmp[lo + c] = (latBuf[loL + c] * wL + latBuf[lo + c] * wC + latBuf[loR + c] * wR) * invW;+ }} else {- latTmp[lo] = latBuf[lo]; latTmp[lo + 1] = latBuf[lo + 1]; latTmp[lo + 2] = latBuf[lo + 2];- latTmp[lo + 3] = latBuf[lo + 3]; latTmp[lo + 4] = latBuf[lo + 4]; latTmp[lo + 5] = latBuf[lo + 5];- latTmp[lo + 6] = latBuf[lo + 6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latTmp[lo + c] = latBuf[lo + c];}}}@@ -3086,12 +3006,10 @@ export class SpatialTipMixer {const rowDn = (gy < gh - 1 ? gy + 1 : gh - 1) * gw;for (let gx = 0; gx < gw; gx++) {const i = row + gx;- const lo = i * 7;+ const lo = i * SPECTRAL_STATE_SIZE;const aC = this.bufA[i];if (aC <= 0.001) {- latBuf[lo] = latTmp[lo]; latBuf[lo + 1] = latTmp[lo + 1]; latBuf[lo + 2] = latTmp[lo + 2];- latBuf[lo + 3] = latTmp[lo + 3]; latBuf[lo + 4] = latTmp[lo + 4]; latBuf[lo + 5] = latTmp[lo + 5];- latBuf[lo + 6] = latTmp[lo + 6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latBuf[lo + c] = latTmp[lo + c];continue;}const iU = rowUp + gx;@@ -3102,19 +3020,13 @@ export class SpatialTipMixer {const wSum = wU + wC + wD;if (wSum > 1e-4) {const invW = 1.0 / wSum;- const loU = iU * 7;- const loD = iD * 7;- latBuf[lo] = (latTmp[loU] * wU + latTmp[lo] * wC + latTmp[loD] * wD) * invW;- latBuf[lo + 1] = (latTmp[loU + 1] * wU + latTmp[lo + 1] * wC + latTmp[loD + 1] * wD) * invW;- latBuf[lo + 2] = (latTmp[loU + 2] * wU + latTmp[lo + 2] * wC + latTmp[loD + 2] * wD) * invW;- latBuf[lo + 3] = (latTmp[loU + 3] * wU + latTmp[lo + 3] * wC + latTmp[loD + 3] * wD) * invW;- latBuf[lo + 4] = (latTmp[loU + 4] * wU + latTmp[lo + 4] * wC + latTmp[loD + 4] * wD) * invW;- latBuf[lo + 5] = (latTmp[loU + 5] * wU + latTmp[lo + 5] * wC + latTmp[loD + 5] * wD) * invW;- latBuf[lo + 6] = (latTmp[loU + 6] * wU + latTmp[lo + 6] * wC + latTmp[loD + 6] * wD) * invW;+ const loU = iU * SPECTRAL_STATE_SIZE;+ const loD = iD * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latBuf[lo + c] = (latTmp[loU + c] * wU + latTmp[lo + c] * wC + latTmp[loD + c] * wD) * invW;+ }} else {- latBuf[lo] = latTmp[lo]; latBuf[lo + 1] = latTmp[lo + 1]; latBuf[lo + 2] = latTmp[lo + 2];- latBuf[lo + 3] = latTmp[lo + 3]; latBuf[lo + 4] = latTmp[lo + 4]; latBuf[lo + 5] = latTmp[lo + 5];- latBuf[lo + 6] = latTmp[lo + 6];+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) latBuf[lo + c] = latTmp[lo + c];}}}@@ -3197,21 +3109,19 @@ export class SpatialTipMixer {const rowDn = (gy < gh - 1 ? gy + 1 : gh - 1) * gw;for (let gx = 0; gx < gw; gx++) {const i = row + gx;- const lo = i * 7;- const loL = (row + (gx > 0 ? gx - 1 : 0)) * 7;- const loR = (row + (gx < gw - 1 ? gx + 1 : gw - 1)) * 7;- const loU = (rowUp + gx) * 7;- const loD = (rowDn + gx) * 7;- latTmp[lo] = latBuf[lo] * centerW + (latBuf[loL] + latBuf[loR] + latBuf[loU] + latBuf[loD]) * diff2d;- latTmp[lo + 1] = latBuf[lo + 1] * centerW + (latBuf[loL + 1] + latBuf[loR + 1] + latBuf[loU + 1] + latBuf[loD + 1]) * diff2d;- latTmp[lo + 2] = latBuf[lo + 2] * centerW + (latBuf[loL + 2] + latBuf[loR + 2] + latBuf[loU + 2] + latBuf[loD + 2]) * diff2d;- latTmp[lo + 3] = latBuf[lo + 3] * centerW + (latBuf[loL + 3] + latBuf[loR + 3] + latBuf[loU + 3] + latBuf[loD + 3]) * diff2d;- latTmp[lo + 4] = latBuf[lo + 4] * centerW + (latBuf[loL + 4] + latBuf[loR + 4] + latBuf[loU + 4] + latBuf[loD + 4]) * diff2d;- latTmp[lo + 5] = latBuf[lo + 5] * centerW + (latBuf[loL + 5] + latBuf[loR + 5] + latBuf[loU + 5] + latBuf[loD + 5]) * diff2d;- latTmp[lo + 6] = latBuf[lo + 6] * centerW + (latBuf[loL + 6] + latBuf[loR + 6] + latBuf[loU + 6] + latBuf[loD + 6]) * diff2d;+ const lo = i * SPECTRAL_STATE_SIZE;+ const loL = (row + (gx > 0 ? gx - 1 : 0)) * SPECTRAL_STATE_SIZE;+ const loR = (row + (gx < gw - 1 ? gx + 1 : gw - 1)) * SPECTRAL_STATE_SIZE;+ const loU = (rowUp + gx) * SPECTRAL_STATE_SIZE;+ const loD = (rowDn + gx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latTmp[lo + c] =+ latBuf[lo + c] * centerW ++ (latBuf[loL + c] + latBuf[loR + c] + latBuf[loU + c] + latBuf[loD + c]) * diff2d;+ }}}- latBuf.set(latTmp.subarray(0, totalTexels * 7));+ latBuf.set(latTmp.subarray(0, totalTexels * SPECTRAL_STATE_SIZE));} else {for (let gy = 0; gy < gh; gy++) {const row = gy * gw;@@ -3245,29 +3155,28 @@ export class SpatialTipMixer {if (wideAlongX) {for (let gy = 0; gy < gh; gy++) {const row = gy * gw;- const firstOff = row * 7;- const lastOff = (row + gw - 1) * 7;- for (let c = 0; c < 7; c++) {+ const firstOff = row * SPECTRAL_STATE_SIZE;+ const lastOff = (row + gw - 1) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {latTmp[firstOff + c] = latBuf[firstOff + c];latTmp[lastOff + c] = latBuf[lastOff + c];}for (let gx = 1; gx < gw - 1; gx++) {- const lo = (row + gx) * 7;- latTmp[lo] = latBuf[lo - 7] * diff + latBuf[lo] * centerW + latBuf[lo + 7] * diff;- latTmp[lo + 1] = latBuf[lo - 6] * diff + latBuf[lo + 1] * centerW + latBuf[lo + 8] * diff;- latTmp[lo + 2] = latBuf[lo - 5] * diff + latBuf[lo + 2] * centerW + latBuf[lo + 9] * diff;- latTmp[lo + 3] = latBuf[lo - 4] * diff + latBuf[lo + 3] * centerW + latBuf[lo + 10] * diff;- latTmp[lo + 4] = latBuf[lo - 3] * diff + latBuf[lo + 4] * centerW + latBuf[lo + 11] * diff;- latTmp[lo + 5] = latBuf[lo - 2] * diff + latBuf[lo + 5] * centerW + latBuf[lo + 12] * diff;- latTmp[lo + 6] = latBuf[lo - 1] * diff + latBuf[lo + 6] * centerW + latBuf[lo + 13] * diff;+ const lo = (row + gx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latTmp[lo + c] =+ latBuf[lo - SPECTRAL_STATE_SIZE + c] * diff ++ latBuf[lo + c] * centerW ++ latBuf[lo + SPECTRAL_STATE_SIZE + c] * diff;+ }}}} else {- const stride = gw * 7;+ const stride = gw * SPECTRAL_STATE_SIZE;for (let gx = 0; gx < gw; gx++) {- const topOff = gx * 7;- const botOff = ((gh - 1) * gw + gx) * 7;- for (let c = 0; c < 7; c++) {+ const topOff = gx * SPECTRAL_STATE_SIZE;+ const botOff = ((gh - 1) * gw + gx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {latTmp[topOff + c] = latBuf[topOff + c];latTmp[botOff + c] = latBuf[botOff + c];}@@ -3275,18 +3184,17 @@ export class SpatialTipMixer {for (let gy = 1; gy < gh - 1; gy++) {const row = gy * gw;for (let gx = 0; gx < gw; gx++) {- const lo = (row + gx) * 7;- latTmp[lo] = latBuf[lo - stride] * diff + latBuf[lo] * centerW + latBuf[lo + stride] * diff;- latTmp[lo + 1] = latBuf[lo - stride + 1] * diff + latBuf[lo + 1] * centerW + latBuf[lo + stride + 1] * diff;- latTmp[lo + 2] = latBuf[lo - stride + 2] * diff + latBuf[lo + 2] * centerW + latBuf[lo + stride + 2] * diff;- latTmp[lo + 3] = latBuf[lo - stride + 3] * diff + latBuf[lo + 3] * centerW + latBuf[lo + stride + 3] * diff;- latTmp[lo + 4] = latBuf[lo - stride + 4] * diff + latBuf[lo + 4] * centerW + latBuf[lo + stride + 4] * diff;- latTmp[lo + 5] = latBuf[lo - stride + 5] * diff + latBuf[lo + 5] * centerW + latBuf[lo + stride + 5] * diff;- latTmp[lo + 6] = latBuf[lo - stride + 6] * diff + latBuf[lo + 6] * centerW + latBuf[lo + stride + 6] * diff;+ const lo = (row + gx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {+ latTmp[lo + c] =+ latBuf[lo - stride + c] * diff ++ latBuf[lo + c] * centerW ++ latBuf[lo + stride + c] * diff;+ }}}}- latBuf.set(latTmp.subarray(0, totalTexels * 7));+ latBuf.set(latTmp.subarray(0, totalTexels * SPECTRAL_STATE_SIZE));} else {if (wideAlongX) {for (let gy = 0; gy < gh; gy++) {@@ -3335,7 +3243,7 @@ export class SpatialTipMixer {}}- // 2b. Single decode pass from 7-channel Kubelka-Munk latent buffer -> bufR/G/B (ONCE per dab!)+ // 2b. Single decode pass from 12-channel spectral Kubelka-Munk buffer -> bufR/G/B (ONCE per dab!)if (usePigment && !isFullFresh) {sumBufR = 0;sumBufG = 0;@@ -3348,7 +3256,7 @@ export class SpatialTipMixer {this.bufB[i] = this.linB[i];continue;}- pigmentLatentToSrgbOut(latBuf, i * 7, this.c1Scratch);+ spectralStateToSrgbOut(latBuf, i * SPECTRAL_STATE_SIZE, this.c1Scratch);const r = this.linR[i] + (this.c1Scratch.r - this.linR[i]) * effPigmentStrength;const g = this.linG[i] + (this.c1Scratch.g - this.linG[i]) * effPigmentStrength;const b = this.linB[i] + (this.c1Scratch.b - this.linB[i]) * effPigmentStrength;@@ -3362,7 +3270,7 @@ export class SpatialTipMixer {} else {for (let i = 0; i < totalTexels; i++) {if (this.bufA[i] <= 0.002) continue;- pigmentLatentToSrgbOut(latBuf, i * 7, this.c1Scratch);+ spectralStateToSrgbOut(latBuf, i * SPECTRAL_STATE_SIZE, this.c1Scratch);const r = this.c1Scratch.r;const g = this.c1Scratch.g;const b = this.c1Scratch.b;diff --git a/src/utils/compositor.ts b/src/utils/compositor.tsindex 3711c64..4aae15d 100644--- a/src/utils/compositor.ts+++ b/src/utils/compositor.ts@@ -21,7 +21,6 @@ import {performanceManager,} from './performanceManager';import { SparseTileGrid, TILE_SIZE, globalTileGrid } from './tileEngine';-import { invalidatePigmentLutTexture } from './pigmentEngine';import { calculateVisibleDocumentBounds } from './compositor/bounds';import { disposeFboSurface, disposePingPongTarget } from './compositor/fboManager';import { initGL, resetGLStateOnContextLost } from './compositor/glInit';@@ -160,7 +159,6 @@ function createInitialCompositorState(uPigmentDraftLayerTexLoc: null,uPigmentDraftDraftTexLoc: null,uPigmentDraftMaskTexLoc: null,- uPigmentDraftLutTexLoc: null,uPigmentDraftDocDimensionsLoc: null,uPigmentDraftViewportSizeLoc: null,uPigmentDraftPanLoc: null,@@ -562,7 +560,6 @@ export class ViewportCompositor {gl.deleteProgram(s.pigmentDraftProgram);s.pigmentDraftProgram = null;}- invalidatePigmentLutTexture(gl, true);if (s.adjProgram) {gl.deleteProgram(s.adjProgram);s.adjProgram = null;diff --git a/src/utils/compositor/fallback2d.ts b/src/utils/compositor/fallback2d.tsindex 473167c..fa3aefd 100644--- a/src/utils/compositor/fallback2d.ts+++ b/src/utils/compositor/fallback2d.ts@@ -4,7 +4,7 @@ import { applyMultiFrameClip, renderFrameBorder } from '../frameEngine';import {compositePigmentOverPixelInto,isPigmentCompatibleCombineMode,-} from '../pigmentEngine';+} from '../spectralPigmentEngine';import { applyScreentoneCPU } from '../screentoneEngine';import { SparseTileGrid, TILE_SIZE, TileBuffer } from '../tileEngine';import { applyAdjustment } from '../tonalEngine';diff --git a/src/utils/compositor/glInit.ts b/src/utils/compositor/glInit.tsindex 646497c..83ceb6f 100644--- a/src/utils/compositor/glInit.ts+++ b/src/utils/compositor/glInit.ts@@ -1,5 +1,4 @@import { TILE_SIZE } from '../tileEngine';-import { invalidatePigmentLutTexture } from '../pigmentEngine';import {COMPOSITOR_FRAGMENT_SHADER,COMPOSITOR_VERTEX_SHADER,@@ -211,7 +210,6 @@ export function initTileArrayPool(state: CompositorState, gl: WebGL2RenderingCon}export function resetGLStateOnContextLost(state: CompositorState): void {- invalidatePigmentLutTexture(state.gl, false);state.contextLost = true;state.gl = null;state.program = null;@@ -454,7 +452,6 @@ export function initGL(state: CompositorState): boolean {state.uPigmentDraftLayerTexLoc = gl.getUniformLocation(pdProg, 'u_layerTex');state.uPigmentDraftDraftTexLoc = gl.getUniformLocation(pdProg, 'u_draftTex');state.uPigmentDraftMaskTexLoc = gl.getUniformLocation(pdProg, 'u_maskTex');- state.uPigmentDraftLutTexLoc = gl.getUniformLocation(pdProg, 'u_pigmentLut');state.uPigmentDraftDocDimensionsLoc = gl.getUniformLocation(pdProg, 'u_docDimensions');state.uPigmentDraftViewportSizeLoc = gl.getUniformLocation(pdProg, 'u_viewportSize');state.uPigmentDraftPanLoc = gl.getUniformLocation(pdProg, 'u_pan');diff --git a/src/utils/compositor/layerRenderer.ts b/src/utils/compositor/layerRenderer.tsindex 6e0e0a4..3ddf716 100644--- a/src/utils/compositor/layerRenderer.ts+++ b/src/utils/compositor/layerRenderer.ts@@ -1,9 +1,6 @@import { CanvasDimensions, LayerItem } from '../../types/editor';import { SparseTileGrid, TILE_SIZE, TileBuffer } from '../tileEngine';-import {- getPigmentLutTexture,- isPigmentCompatibleCombineMode,-} from '../pigmentEngine';+import { isPigmentCompatibleCombineMode } from '../spectralPigmentEngine';import { docBoundsToViewportScissor, parseHexColor } from './bounds';import { blitFolderTexture } from './effectsPass';import {@@ -174,9 +171,9 @@ export function renderSingleLayer(const maskTex = hasMask && layer.maskCanvas ? getMaskTexture(state, layer) : null;const effectiveOpacity = layer.opacity * opacityMultiplier;- // Live Physical Pigment Draft Compositing Path (WebGL2 Kubelka-Munk 4-Pigment LUT)+ // Live Physical Pigment Draft Compositing Path (WebGL2 8-Band Spectral Kubelka-Munk)// Renders the active layer's existing tiles into state.layerFbo, composites the active draft- // in pigment latent space into state.layerStylesOutFbo (matching CPU compositePigmentOverPixelInto 1:1),+ // in spectral pigment space into state.layerStylesOutFbo (matching CPU compositePigmentOverPixelInto 1:1),// and blits the result onto target with the layer's blendMode & opacity — zero pop on pointerUp!if (needsPigmentDraftFbo &&@@ -194,15 +191,13 @@ export function renderSingleLayer(if (!state.layerStylesOutFbo) {state.layerStylesOutFbo = createFboSurface(gl, false);}- const pigmentLutTex = getPigmentLutTexture(gl);const draftSpace = opts?.activeDraftSpace ?? 'document';const draftUploaded =state.layerStylesOutFbo &&ensureFboSurfaceSize(gl, state.layerStylesOutFbo, fs.viewportW, fs.viewportH) &&- pigmentLutTex !== null &&uploadActiveDraftTexture(state, gl, draftCanvas, opts?.activeDraftVersion, draftSpace);- if (draftUploaded && state.layerStylesOutFbo && state.draftTexture && pigmentLutTex) {+ if (draftUploaded && state.layerStylesOutFbo && state.draftTexture) {const stencilWasEnabled = gl.isEnabled(gl.STENCIL_TEST);if (stencilWasEnabled) gl.disable(gl.STENCIL_TEST);@@ -325,10 +320,6 @@ export function renderSingleLayer(gl.bindTexture(gl.TEXTURE_2D, maskTex);gl.uniform1i(state.uPigmentDraftMaskTexLoc, 2);- gl.activeTexture(gl.TEXTURE3);- gl.bindTexture(gl.TEXTURE_2D, pigmentLutTex);- gl.uniform1i(state.uPigmentDraftLutTexLoc, 3);-gl.uniform2f(state.uPigmentDraftDocDimensionsLoc, dimensions.width, dimensions.height);gl.uniform2f(state.uPigmentDraftViewportSizeLoc, fs.viewportW, fs.viewportH);gl.uniform2f(state.uPigmentDraftPanLoc, fs.transform.pan.x, fs.transform.pan.y);@@ -380,8 +371,6 @@ export function renderSingleLayer(}// Unbind all texture units before re-binding parent FBO- gl.activeTexture(gl.TEXTURE3);- gl.bindTexture(gl.TEXTURE_2D, null);gl.activeTexture(gl.TEXTURE2);gl.bindTexture(gl.TEXTURE_2D, null);gl.activeTexture(gl.TEXTURE1);diff --git a/src/utils/compositor/shaders/coreShaders.ts b/src/utils/compositor/shaders/coreShaders.tsindex fd7fa87..7ed6e9c 100644--- a/src/utils/compositor/shaders/coreShaders.ts+++ b/src/utils/compositor/shaders/coreShaders.ts@@ -1,6 +1,6 @@import { buildBlendModeGlsl } from './blendModesGlsl';import { buildScreentoneGlsl } from './screentoneGlsl';-import { PIGMENT_GLSL_WEBGL2 } from '../../pigmentEngine';+import { SPECTRAL_PIGMENT_GLSL } from '../../spectralPigmentEngine';// Document-Space Solid Polygon Shaders for Frame Border Folder Stencil Clipping & Borders// Clips fragments cleanly to [0, u_docDimensions] so off-canvas manga bleed (tachikiri) frames@@ -394,7 +394,7 @@ void main() {// Live Physical Pigment Draft Compositing Fragment Shader (WebGL2)// Composites the active layer's existing pixels (u_layerTex) with the active stroke draft (u_draftTex)-// in 7-channel Kubelka-Munk pigment latent space — matching CPU compositePigmentOverPixelInto 1-to-1+// in 8-band spectral Kubelka-Munk pigment space — matching CPU compositePigmentOverPixelInto 1-to-1// for zero-pop live/commit parity.export const PIGMENT_DRAFT_FRAGMENT_SHADER = `#version 300 esprecision highp float;@@ -404,7 +404,6 @@ in vec2 v_uv;uniform sampler2D u_layerTex;uniform sampler2D u_draftTex;uniform sampler2D u_maskTex;-uniform sampler2D u_pigmentLut;uniform vec2 u_docDimensions;uniform vec2 u_viewportSize;@@ -428,7 +427,7 @@ uniform vec3 u_layerSubColor;out vec4 fragColor;-${PIGMENT_GLSL_WEBGL2}+${SPECTRAL_PIGMENT_GLSL}vec4 encodePigmentOverWhiteGpu(vec3 srcRgb, float sA, float strength) {if (sA <= 0.0001) return vec4(0.0);diff --git a/src/utils/compositor/types.ts b/src/utils/compositor/types.tsindex e8a0512..be1d0d2 100644--- a/src/utils/compositor/types.ts+++ b/src/utils/compositor/types.ts@@ -301,7 +301,6 @@ export interface CompositorState {uPigmentDraftLayerTexLoc: WebGLUniformLocation | null;uPigmentDraftDraftTexLoc: WebGLUniformLocation | null;uPigmentDraftMaskTexLoc: WebGLUniformLocation | null;- uPigmentDraftLutTexLoc: WebGLUniformLocation | null;uPigmentDraftDocDimensionsLoc: WebGLUniformLocation | null;uPigmentDraftViewportSizeLoc: WebGLUniformLocation | null;uPigmentDraftPanLoc: WebGLUniformLocation | null;diff --git a/src/utils/retouchEngine.ts b/src/utils/retouchEngine.tsindex e368c1e..800c2eb 100644--- a/src/utils/retouchEngine.ts+++ b/src/utils/retouchEngine.ts@@ -28,12 +28,12 @@ import {mixPigmentRgbOut,} from './colorMixingEngine';import {- PIGMENT_LATENT_SIZE,+ SPECTRAL_STATE_SIZE,detectWhitePaperGround,encodePigmentOverWhiteOut,- pigmentLatentToSrgbOut,- srgbToPigmentLatentOut,-} from './pigmentEngine';+ spectralStateToSrgbOut,+ srgbToSpectralStateOut,+} from './spectralPigmentEngine';const _smudgeMixOut: RgbColor = { r: 0, g: 0, b: 0 };const _smudgeReloadOut: RgbColor = { r: 0, g: 0, b: 0 };@@ -649,20 +649,20 @@ function applyBlurDab(premul[o + 3] = srcData[o + 3];}- // Optional 7-channel premultiplied Kubelka-Munk pigment latent buffers for true physical pigment blur+ // Optional 12-channel premultiplied 8-band spectral Kubelka-Munk buffers for true physical pigment blurlet specPremul: Float32Array | null = null;let specHoriz: Float32Array | null = null;if (usePigment) {- specPremul = new Float32Array(pw * ph * PIGMENT_LATENT_SIZE);- specHoriz = new Float32Array(pw * ph * PIGMENT_LATENT_SIZE);- const tmpSpec = new Float32Array(PIGMENT_LATENT_SIZE);+ specPremul = new Float32Array(pw * ph * SPECTRAL_STATE_SIZE);+ specHoriz = new Float32Array(pw * ph * SPECTRAL_STATE_SIZE);+ const tmpSpec = new Float32Array(SPECTRAL_STATE_SIZE);for (let i = 0; i < pw * ph; i++) {const o = i * 4;const a = srcData[o + 3] / 255;if (a > 0) {- srgbToPigmentLatentOut(srcData[o], srcData[o + 1], srcData[o + 2], tmpSpec, 0);- const so = i * PIGMENT_LATENT_SIZE;- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) {+ srgbToSpectralStateOut(srcData[o], srcData[o + 1], srcData[o + 2], tmpSpec, 0);+ const so = i * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {specPremul[so + c] = tmpSpec[c] * a;}}@@ -670,7 +670,7 @@ function applyBlurDab(}// Horizontal pass- const sRowSum = usePigment ? new Float32Array(PIGMENT_LATENT_SIZE) : null;+ const sRowSum = usePigment ? new Float32Array(SPECTRAL_STATE_SIZE) : null;for (let y = 0; y < ph; y++) {const rowBase = y * pw;let sr = 0;@@ -686,8 +686,8 @@ function applyBlurDab(sb += premul[o + 2];sa += premul[o + 3];if (sRowSum && specPremul) {- const so = (rowBase + sx) * PIGMENT_LATENT_SIZE;- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) sRowSum[c] += specPremul[so + c];+ const so = (rowBase + sx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) sRowSum[c] += specPremul[so + c];}}for (let x = 0; x < pw; x++) {@@ -697,8 +697,8 @@ function applyBlurDab(horiz[o + 2] = sb * invK;horiz[o + 3] = sa * invK;if (sRowSum && specHoriz) {- const so = (rowBase + x) * PIGMENT_LATENT_SIZE;- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) specHoriz[so + c] = sRowSum[c] * invK;+ const so = (rowBase + x) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) specHoriz[so + c] = sRowSum[c] * invK;}const remX = x - blurK < 0 ? 0 : x - blurK;@@ -710,9 +710,9 @@ function applyBlurDab(sb += premul[ao + 2] - premul[ro + 2];sa += premul[ao + 3] - premul[ro + 3];if (sRowSum && specPremul) {- const rso = (rowBase + remX) * PIGMENT_LATENT_SIZE;- const aso = (rowBase + addX) * PIGMENT_LATENT_SIZE;- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) {+ const rso = (rowBase + remX) * SPECTRAL_STATE_SIZE;+ const aso = (rowBase + addX) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {sRowSum[c] += specPremul[aso + c] - specPremul[rso + c];}}@@ -730,8 +730,8 @@ function applyBlurDab(snapshotRoiTiles(grid, session.layerId, rx0, ry0, rw, rh, session.beforeSnapshot);- const sColSum = usePigment ? new Float32Array(PIGMENT_LATENT_SIZE) : null;- const sColDec = usePigment ? new Float32Array(PIGMENT_LATENT_SIZE) : null;+ const sColSum = usePigment ? new Float32Array(SPECTRAL_STATE_SIZE) : null;+ const sColDec = usePigment ? new Float32Array(SPECTRAL_STATE_SIZE) : null;for (let lx = 0; lx < rw; lx++) {const sx = rx0 - px0 + lx;@@ -752,8 +752,8 @@ function applyBlurDab(sb += horiz[o + 2];sa += horiz[o + 3];if (sColSum && specHoriz) {- const so = (sy * pw + sx) * PIGMENT_LATENT_SIZE;- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) sColSum[c] += specHoriz[so + c];+ const so = (sy * pw + sx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) sColSum[c] += specHoriz[so + c];}}@@ -771,10 +771,10 @@ function applyBlurDab(const hasPigBlur = Boolean(sColSum && sColDec && ba > 0.5);if (hasPigBlur && sColSum && sColDec) {const invAlphaNorm = 1 / ((ba / 255) * kSize);- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) {+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {sColDec[c] = sColSum[c] * invAlphaNorm;}- pigmentLatentToSrgbOut(sColDec, 0, _blurDecodeOut);+ spectralStateToSrgbOut(sColDec, 0, _blurDecodeOut);const invBa = 255 / ba;const regBlurR = br * invBa;const regBlurG = bg * invBa;@@ -793,9 +793,9 @@ function applyBlurDab(sb += horiz[ao + 2] - horiz[ro + 2];sa += horiz[ao + 3] - horiz[ro + 3];if (sColSum && specHoriz) {- const rso = (remY * pw + sx) * PIGMENT_LATENT_SIZE;- const aso = (addY * pw + sx) * PIGMENT_LATENT_SIZE;- for (let c = 0; c < PIGMENT_LATENT_SIZE; c++) {+ const rso = (remY * pw + sx) * SPECTRAL_STATE_SIZE;+ const aso = (addY * pw + sx) * SPECTRAL_STATE_SIZE;+ for (let c = 0; c < SPECTRAL_STATE_SIZE; c++) {sColSum[c] += specHoriz[aso + c] - specHoriz[rso + c];}}diff --git a/src/utils/spectralPigmentEngine.ts b/src/utils/spectralPigmentEngine.tsnew file mode 100644index 0000000..af3bc04--- /dev/null+++ b/src/utils/spectralPigmentEngine.ts@@ -0,0 +1,922 @@+/**+ * Clean-Room 8-Band Spectral Two-Constant Kubelka-Munk + Saunderson Pigment Engine for Ink Studio.+ *+ * Implements analytical subtractive paint physics directly from classic optical theory:+ * - Paul Kubelka & Franz Munk (1931), two-constant absorption K(lambda) and scattering S model+ * - J. L. Saunderson (1942), air-binder surface & internal Fresnel reflection correction (k1, k2)+ * - 8-band visible reflectance spectrum (410, 450, 490, 525, 565, 600, 640, 680 nm)+ * - 12-float linear spectral mixing state:+ * [u0..u7, S, resR, resG, resB]+ * where u_i = S * cbrt((1 - R_i)^2 / (2 * R_i)) is the perceptually compressed+ * cube-root Kubelka-Munk absorption density, S is the pigment bulk scattering factor,+ * and (resR, resG, resB) are linear-RGB residuals ensuring exact color identity across+ * the entire sRGB gamut.+ *+ * Zero external lookup tables, zero fitted polynomials, zero GPU texture units required.+ *+ * Properties:+ * - Blue + Yellow -> vibrant natural Green+ * - Dark saturated pigments diluted over White -> luminous high-chroma tints (Cerulean, Rose, Teal, Royal Blue)+ * - Multi-color blending (blur, tip diffusion, radial sampling) is strictly linear in 12-float spectral state+ * - Exact self-mix invariance: mix(c, c, t) === c+ * - Strict achromatic neutrality for Black + White / neutral grayscale mixes+ */++import type { CspBrushCombineMode, LayerItem } from '../types/editor';+import type { SparseTileGrid } from './tileEngine';++/** 8 spectral absorption-root bands + 1 scattering scalar S + 3 linear RGB residuals. */+export const SPECTRAL_STATE_SIZE = 12;++export interface PigmentRgb {+ r: number;+ g: number;+ b: number;+}++const INV_255 = 1 / 255;++// Saunderson (1942) air-binder reflection constants (paint medium n ~= 1.5)+const SAUNDERSON_K1 = 0.0052;+const SAUNDERSON_INV_ONE_MINUS_K1 = 1.0 / (1.0 - SAUNDERSON_K1);+const SAUNDERSON_K2 = 0.38;+const SAUNDERSON_ONE_MINUS_K2 = 1.0 - SAUNDERSON_K2;+const REFL_FLOOR = 0.0004;+const REFL_SPAN = 0.999 - REFL_FLOOR;+const INV_REFL_SPAN = 1.0 / REFL_SPAN;+const REFL_CLAMP_MIN = REFL_FLOOR;+const REFL_CLAMP_MAX = 0.9995;+// Two-constant Kubelka-Munk bulk scattering: carbon black has very low scattering,+// chromatic pigments scatter proportionally to peak reflectance & luminance, and TiO2 white reaches 1.0+const SCATTER_BASE = 0.04;+const SCATTER_LUM = 0.12;+const SCATTER_MAX = 0.66;+const SCATTER_WHITE = 0.18;++// 8-band visible spectrum (410, 450, 490, 525, 565, 600, 640, 680 nm)+// Non-negative normalized sensor integration weights (each channel sums to 1.0)+const W_R = new Float32Array([0.0, 0.0, 0.0, 0.0, 0.0, 0.34, 0.48, 0.18]);+const W_G = new Float32Array([0.0, 0.0, 0.06, 0.56, 0.38, 0.0, 0.0, 0.0]);+const W_B = new Float32Array([0.26, 0.66, 0.08, 0.0, 0.0, 0.0, 0.0, 0.0]);++// Smooth physical reflectance basis curves across the 8 bands (410..680 nm)+const B_R = new Float32Array([0.24, 0.014, 0.001, 0.001, 0.13, 0.96, 1.0, 1.0]);+const B_G = new Float32Array([0.001, 0.001, 0.08, 0.995, 1.0, 0.65, 0.015, 0.001]);+const B_B = new Float32Array([1.0, 1.0, 0.96, 0.52, 0.028, 0.001, 0.001, 0.06]);+const B_C = new Float32Array([0.85, 0.995, 1.0, 0.995, 0.62, 0.002, 0.001, 0.001]);+const B_M = new Float32Array([1.0, 0.995, 0.08, 0.001, 0.003, 0.85, 1.0, 1.0]);+const B_Y = new Float32Array([0.004, 0.004, 0.02, 0.98, 1.0, 1.0, 1.0, 1.0]);++// Exact IEC 61966-2-1 sRGB -> Linear RGB lookup table (256 entries, interpolated for floats)+const SRGB_TO_LINEAR_LUT = new Float32Array(256);+for (let i = 0; i < 256; i++) {+ const v = i / 255;+ SRGB_TO_LINEAR_LUT[i] = v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);+}++// Fast Linear RGB [0..1] -> sRGB [0..255] lookup table (4096 entries, interpolated)+const LIN_TO_SRGB_SIZE = 4096;+const LIN_TO_SRGB_LUT = new Float32Array(LIN_TO_SRGB_SIZE + 1);+for (let i = 0; i <= LIN_TO_SRGB_SIZE; i++) {+ const v = i / LIN_TO_SRGB_SIZE;+ const s = v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;+ LIN_TO_SRGB_LUT[i] = s <= 0 ? 0 : s >= 1 ? 255 : s * 255;+}++function srgbToLinear(c255: number): number {+ if (c255 <= 0) return 0;+ if (c255 >= 255) return 1;+ const idx = c255 | 0;+ const frac = c255 - idx;+ if (idx >= 255) return 1;+ const v0 = SRGB_TO_LINEAR_LUT[idx];+ return frac > 0 ? v0 + (SRGB_TO_LINEAR_LUT[idx + 1] - v0) * frac : v0;+}++function linearToSrgb255(c: number): number {+ if (c <= 0) return 0;+ if (c >= 1) return 255;+ if (c <= 0.0031308) return c * (12.92 * 255);+ const pos = c * LIN_TO_SRGB_SIZE;+ const idx = pos | 0;+ const v0 = LIN_TO_SRGB_LUT[idx];+ return v0 + (LIN_TO_SRGB_LUT[idx + 1] - v0) * (pos - idx);+}++function clamp255(v: number): number {+ return v <= 0 ? 0 : v >= 255 ? 255 : v;+}++function measuredToInternal(rm: number): number {+ const x = rm > SAUNDERSON_K1 ? (rm - SAUNDERSON_K1) * SAUNDERSON_INV_ONE_MINUS_K1 : 0;+ const ri = x / (SAUNDERSON_ONE_MINUS_K2 + SAUNDERSON_K2 * x);+ return REFL_FLOOR + ri * REFL_SPAN;+}++function internalToMeasured(ri: number): number {+ const rRaw = (ri - REFL_FLOOR) * INV_REFL_SPAN;+ const r = rRaw <= 0 ? 0 : rRaw >= 1 ? 1 : rRaw;+ const x = (SAUNDERSON_ONE_MINUS_K2 * r) / (1.0 - SAUNDERSON_K2 * r);+ return SAUNDERSON_K1 + (1.0 - SAUNDERSON_K1) * x;+}++// 256-slot direct-mapped cache for srgbToSpectralStateOut (zero heap allocation in hot loops)+const ENC_CACHE_SIZE = 256;+const ENC_CACHE_MASK = ENC_CACHE_SIZE - 1;+const _encCacheKeys = new Int32Array(ENC_CACHE_SIZE).fill(-1);+// Each slot stores [u0..u7, S, baseR, baseG, baseB]; exact residual is computed from input linear RGB+const _encCacheVals = new Float32Array(ENC_CACHE_SIZE * SPECTRAL_STATE_SIZE);++// Reusable scratch buffers for zero-allocation hot-loop mixing+const _z1 = new Float32Array(SPECTRAL_STATE_SIZE);+const _z2 = new Float32Array(SPECTRAL_STATE_SIZE);+const _zMix = new Float32Array(SPECTRAL_STATE_SIZE);+const _whiteMixRgb: PigmentRgb = { r: 0, g: 0, b: 0 };+const _compRgb: PigmentRgb = { r: 0, g: 0, b: 0 };++/**+ * Encodes an sRGB color [0..255] into 12 spectral two-constant Kubelka-Munk state components+ * written into `out[offset .. offset + 11]`. Zero heap allocations.+ */+export function srgbToSpectralStateOut(+ r255: number,+ g255: number,+ b255: number,+ out: Float32Array,+ offset: number+): void {+ const cr = r255 <= 0 ? 0 : r255 >= 255 ? 255 : r255;+ const cg = g255 <= 0 ? 0 : g255 >= 255 ? 255 : g255;+ const cb = b255 <= 0 ? 0 : b255 >= 255 ? 255 : b255;++ const lr = srgbToLinear(cr);+ const lg = srgbToLinear(cg);+ const lb = srgbToLinear(cb);++ const rInt = (cr + 0.5) | 0;+ const gInt = (cg + 0.5) | 0;+ const bInt = (cb + 0.5) | 0;+ const key = (rInt << 16) | (gInt << 8) | bInt;+ const slot = ((rInt * 73) ^ (gInt * 151) ^ (bInt * 233)) & ENC_CACHE_MASK;++ if (_encCacheKeys[slot] === key) {+ const base = slot * SPECTRAL_STATE_SIZE;+ out[offset] = _encCacheVals[base];+ out[offset + 1] = _encCacheVals[base + 1];+ out[offset + 2] = _encCacheVals[base + 2];+ out[offset + 3] = _encCacheVals[base + 3];+ out[offset + 4] = _encCacheVals[base + 4];+ out[offset + 5] = _encCacheVals[base + 5];+ out[offset + 6] = _encCacheVals[base + 6];+ out[offset + 7] = _encCacheVals[base + 7];+ out[offset + 8] = _encCacheVals[base + 8];+ out[offset + 9] = lr - _encCacheVals[base + 9];+ out[offset + 10] = lg - _encCacheVals[base + 10];+ out[offset + 11] = lb - _encCacheVals[base + 11];+ return;+ }++ // Evaluate quantized integer sRGB key into cache for stable cache reuse+ const qr = SRGB_TO_LINEAR_LUT[rInt];+ const qg = SRGB_TO_LINEAR_LUT[gInt];+ const qb = SRGB_TO_LINEAR_LUT[bInt];++ const rr = measuredToInternal(qr);+ const gg = measuredToInternal(qg);+ const bb = measuredToInternal(qb);++ const lum = 0.2126 * qr + 0.7152 * qg + 0.0722 * qb;+ const minQ = qr < qg ? (qr < qb ? qr : qb) : qg < qb ? qg : qb;+ const maxQ = qr > qg ? (qr > qb ? qr : qb) : qg > qb ? qg : qb;+ const S =+ SCATTER_BASE ++ SCATTER_LUM * Math.sqrt(lum <= 0 ? 0 : lum >= 1 ? 1 : lum) ++ SCATTER_MAX * Math.cbrt(maxQ <= 0 ? 0 : maxQ >= 1 ? 1 : maxQ) ++ SCATTER_WHITE * Math.sqrt(minQ <= 0 ? 0 : minQ >= 1 ? 1 : minQ);++ let w: number;+ let c: number;+ let p: number;+ let secBasis: Float32Array;+ let priBasis: Float32Array;+ if (rr <= gg && rr <= bb) {+ w = rr;+ if (gg <= bb) {+ c = gg - rr;+ p = bb - gg;+ secBasis = B_C;+ priBasis = B_B;+ } else {+ c = bb - rr;+ p = gg - bb;+ secBasis = B_C;+ priBasis = B_G;+ }+ } else if (gg <= rr && gg <= bb) {+ w = gg;+ if (rr <= bb) {+ c = rr - gg;+ p = bb - rr;+ secBasis = B_M;+ priBasis = B_B;+ } else {+ c = bb - gg;+ p = rr - bb;+ secBasis = B_M;+ priBasis = B_R;+ }+ } else {+ w = bb;+ if (rr <= gg) {+ c = rr - bb;+ p = gg - rr;+ secBasis = B_Y;+ priBasis = B_G;+ } else {+ c = gg - bb;+ p = rr - gg;+ secBasis = B_Y;+ priBasis = B_R;+ }+ }++ const base = slot * SPECTRAL_STATE_SIZE;+ let br = 0;+ let bg = 0;+ let bbAcc = 0;++ for (let i = 0; i < 8; i++) {+ const sp = w + c * secBasis[i] + p * priBasis[i];+ const rc = sp < REFL_CLAMP_MIN ? REFL_CLAMP_MIN : sp > REFL_CLAMP_MAX ? REFL_CLAMP_MAX : sp;+ const rm = internalToMeasured(rc);+ br += rm * W_R[i];+ bg += rm * W_G[i];+ bbAcc += rm * W_B[i];+ const oneMinusR = 1.0 - rc;+ const ks = (oneMinusR * oneMinusR) / (2.0 * rc);+ const u = S * Math.cbrt(ks);+ out[offset + i] = u;+ _encCacheVals[base + i] = u;+ }++ out[offset + 8] = S;+ out[offset + 9] = lr - br;+ out[offset + 10] = lg - bg;+ out[offset + 11] = lb - bbAcc;++ _encCacheKeys[slot] = key;+ _encCacheVals[base + 8] = S;+ _encCacheVals[base + 9] = br;+ _encCacheVals[base + 10] = bg;+ _encCacheVals[base + 11] = bbAcc;+}++/**+ * Decodes 12 spectral Kubelka-Munk state components from `state[offset .. offset + 11]`+ * back into sRGB [0..255] written directly into `out`. Zero heap allocations.+ */+export function spectralStateToSrgbOut(+ state: Float32Array,+ offset: number,+ out: PigmentRgb+): PigmentRgb {+ const s = state[offset + 8];+ const invS = 1.0 / (s > 1e-6 ? s : 1e-6);++ let mr = 0;+ let mg = 0;+ let mb = 0;++ // Band 0 (410nm): W_B = 0.26+ {+ const u = state[offset] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mb += rm * 0.26;+ }+ // Band 1 (450nm): W_B = 0.66+ {+ const u = state[offset + 1] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mb += rm * 0.66;+ }+ // Band 2 (490nm): W_G = 0.06, W_B = 0.08+ {+ const u = state[offset + 2] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mg += rm * 0.06;+ mb += rm * 0.08;+ }+ // Band 3 (525nm): W_G = 0.56+ {+ const u = state[offset + 3] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mg += rm * 0.56;+ }+ // Band 4 (565nm): W_G = 0.38+ {+ const u = state[offset + 4] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mg += rm * 0.38;+ }+ // Band 5 (600nm): W_R = 0.34+ {+ const u = state[offset + 5] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mr += rm * 0.34;+ }+ // Band 6 (640nm): W_R = 0.48+ {+ const u = state[offset + 6] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mr += rm * 0.48;+ }+ // Band 7 (680nm): W_R = 0.18+ {+ const u = state[offset + 7] * invS;+ const ks = u > 0 ? u * u * u : 0;+ const rm = internalToMeasured(1.0 + ks - Math.sqrt(ks * ks + 2.0 * ks));+ mr += rm * 0.18;+ }++ out.r = linearToSrgb255(mr + state[offset + 9]);+ out.g = linearToSrgb255(mg + state[offset + 10]);+ out.b = linearToSrgb255(mb + state[offset + 11]);+ return out;+}++/**+ * Zero-allocation physical 8-band spectral Kubelka-Munk color mixer writing sRGB [0..255] into `out`.+ * `pigmentStrength` in [0..1]: 0.0 = regular linear sRGB blend, 1.0 = full spectral pigment blend.+ */+export function pigmentMixRgbOut(+ r1: number,+ g1: number,+ b1: number,+ r2: number,+ g2: number,+ b2: number,+ weight2: number,+ pigmentStrength: number,+ out: PigmentRgb+): PigmentRgb {+ const t = weight2 <= 0 ? 0 : weight2 >= 1 ? 1 : weight2;+ if (t <= 0.0001) {+ out.r = clamp255(r1);+ out.g = clamp255(g1);+ out.b = clamp255(b1);+ return out;+ }+ if (t >= 0.9999) {+ out.r = clamp255(r2);+ out.g = clamp255(g2);+ out.b = clamp255(b2);+ return out;+ }++ const strength = pigmentStrength <= 0 ? 0 : pigmentStrength >= 1 ? 1 : pigmentStrength;+ const dr = r2 - r1;+ const dg = g2 - g1;+ const db = b2 - b1;+ const linR = r1 + dr * t;+ const linG = g1 + dg * t;+ const linB = b1 + db * t;++ if (strength <= 0.005 || dr * dr + dg * dg + db * db < 1.0) {+ out.r = clamp255(linR);+ out.g = clamp255(linG);+ out.b = clamp255(linB);+ return out;+ }++ // Smooth achromatic neutrality guard: when both inputs are neutral gray (Black/Gray/White),+ // keep the blend strictly achromatic and perceptually smooth.+ const max1 = r1 > g1 ? (r1 > b1 ? r1 : b1) : g1 > b1 ? g1 : b1;+ const min1 = r1 < g1 ? (r1 < b1 ? r1 : b1) : g1 < b1 ? g1 : b1;+ const max2 = r2 > g2 ? (r2 > b2 ? r2 : b2) : g2 > b2 ? g2 : b2;+ const min2 = r2 < g2 ? (r2 < b2 ? r2 : b2) : g2 < b2 ? g2 : b2;+ const maxChroma = Math.max(max1 - min1, max2 - min2);+ if (maxChroma <= 1.0) {+ out.r = clamp255(linR);+ out.g = clamp255(linG);+ out.b = clamp255(linB);+ return out;+ }++ srgbToSpectralStateOut(r1, g1, b1, _z1, 0);+ srgbToSpectralStateOut(r2, g2, b2, _z2, 0);++ const invT = 1.0 - t;+ for (let i = 0; i < SPECTRAL_STATE_SIZE; i++) {+ _zMix[i] = invT * _z1[i] + t * _z2[i];+ }+ spectralStateToSrgbOut(_zMix, 0, out);++ // Smoothly ramp spectral strength for near-achromatic pairs (chroma in (1..8])+ let effStrength = strength;+ if (maxChroma < 8.0) {+ const chromaRamp = (maxChroma - 1.0) / 7.0;+ effStrength *= chromaRamp * chromaRamp * (3.0 - 2.0 * chromaRamp);+ }++ if (effStrength >= 0.995) {+ return out;+ }++ out.r = clamp255(linR + (out.r - linR) * effStrength);+ out.g = clamp255(linG + (out.g - linG) * effStrength);+ out.b = clamp255(linB + (out.b - linB) * effStrength);+ return out;+}++/**+ * Optical pigment-over-white encoding for translucent strokes painted onto an empty+ * transparent layer sitting over a white canvas ground.+ *+ * Solves for the minimal valid RGBA pixel (outR, outG, outB, outA) such that standard+ * `source-over` compositing over white (255, 255, 255) reproduces the exact spectral+ * Kubelka-Munk dilution `pigmentMix(White, src, sA, strength)`:+ * outRGB * outA + 255 * (1 - outA) === pigmentMix(White, src, sA, strength)+ */+export function encodePigmentOverWhiteOut(+ sr: number,+ sg: number,+ sb: number,+ sA: number,+ pigmentStrength: number,+ out: [number, number, number, number]+): void {+ if (sA <= 0.0001) {+ out[0] = 0;+ out[1] = 0;+ out[2] = 0;+ out[3] = 0;+ return;+ }+ if (sA >= 0.999) {+ out[0] = clamp255(Math.round(sr));+ out[1] = clamp255(Math.round(sg));+ out[2] = clamp255(Math.round(sb));+ out[3] = 255;+ return;+ }+ if (pigmentStrength <= 0.005) {+ out[0] = clamp255(Math.round(sr));+ out[1] = clamp255(Math.round(sg));+ out[2] = clamp255(Math.round(sb));+ out[3] = clamp255(Math.round(sA * 255));+ return;+ }++ pigmentMixRgbOut(255, 255, 255, sr, sg, sb, sA, pigmentStrength, _whiteMixRgb);+ const pr = _whiteMixRgb.r;+ const pg = _whiteMixRgb.g;+ const pb = _whiteMixRgb.b;++ const minC = pr < pg ? (pr < pb ? pr : pb) : pg < pb ? pg : pb;+ const minValidA = 1.0 - minC * INV_255;+ const outA = sA > minValidA ? sA : minValidA;++ if (outA <= 0.0001) {+ out[0] = 0;+ out[1] = 0;+ out[2] = 0;+ out[3] = 0;+ return;+ }++ const bg = 255.0 * (1.0 - outA);+ const invOutA = 1.0 / outA;+ out[0] = clamp255(Math.round((pr - bg) * invOutA));+ out[1] = clamp255(Math.round((pg - bg) * invOutA));+ out[2] = clamp255(Math.round((pb - bg) * invOutA));+ out[3] = clamp255(Math.round(outA * 255.0));+}++/**+ * Returns true if the given brush combine mode is compatible with physical pigment compositing.+ */+export function isPigmentCompatibleCombineMode(mode?: CspBrushCombineMode): boolean {+ return (+ !mode ||+ mode === 'normal' ||+ mode === 'pass-through' ||+ mode === 'compare-density' ||+ mode === 'behind'+ );+}++/**+ * Composites a single brush/stroke RGBA pixel (0..255) over a destination pixel (0..255)+ * using 8-band spectral Kubelka-Munk pigment mixing.+ * Matches `PIGMENT_DRAFT_FRAGMENT_SHADER` in WebGL2 1-to-1 for zero-pop live/commit parity.+ */+export function compositePigmentOverPixelInto(+ dr: number,+ dg: number,+ db: number,+ da: number,+ sr: number,+ sg: number,+ sb: number,+ effA: number,+ pigmentStrength: number,+ pigmentWhiteGround: boolean,+ compositeOp: 'source-over' | 'destination-out' | 'source-atop',+ combineMode: CspBrushCombineMode | undefined,+ out: [number, number, number, number]+): void {+ const mode: CspBrushCombineMode = combineMode ?? 'normal';+ const sA = effA * INV_255;++ if (compositeOp === 'source-atop') {+ if (da === 0) {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ out[3] = 0;+ return;+ }+ if (mode === 'behind') {+ if (da >= 255) {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ out[3] = da;+ return;+ }+ const dA = da * INV_255;+ const rem = (1.0 - dA) * sA;+ const denom = dA + rem;+ if (denom > 1e-5) {+ pigmentMixRgbOut(dr, dg, db, sr, sg, sb, rem / denom, pigmentStrength, _compRgb);+ out[0] = clamp255(Math.round(_compRgb.r));+ out[1] = clamp255(Math.round(_compRgb.g));+ out[2] = clamp255(Math.round(_compRgb.b));+ } else {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ }+ out[3] = da;+ return;+ }+ if (effA >= 255) {+ out[0] = sr;+ out[1] = sg;+ out[2] = sb;+ out[3] = da;+ return;+ }+ pigmentMixRgbOut(dr, dg, db, sr, sg, sb, sA, pigmentStrength, _compRgb);+ out[0] = clamp255(Math.round(_compRgb.r));+ out[1] = clamp255(Math.round(_compRgb.g));+ out[2] = clamp255(Math.round(_compRgb.b));+ out[3] = da;+ return;+ }++ if (mode === 'behind') {+ if (da >= 255) {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ out[3] = da;+ return;+ }+ if (da === 0) {+ if (pigmentWhiteGround && effA < 255) {+ encodePigmentOverWhiteOut(sr, sg, sb, sA, pigmentStrength, out);+ } else {+ out[0] = sr;+ out[1] = sg;+ out[2] = sb;+ out[3] = effA;+ }+ return;+ }+ const dA = da * INV_255;+ const rem = sA * (1.0 - dA);+ const compA = dA + rem;+ if (compA > 1e-5) {+ pigmentMixRgbOut(dr, dg, db, sr, sg, sb, rem / compA, pigmentStrength, _compRgb);+ out[0] = clamp255(Math.round(_compRgb.r));+ out[1] = clamp255(Math.round(_compRgb.g));+ out[2] = clamp255(Math.round(_compRgb.b));+ out[3] = clamp255(Math.round(compA * 255));+ } else {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ out[3] = da;+ }+ return;+ }++ if (da === 0) {+ if (pigmentWhiteGround && effA < 255) {+ encodePigmentOverWhiteOut(sr, sg, sb, sA, pigmentStrength, out);+ } else {+ out[0] = sr;+ out[1] = sg;+ out[2] = sb;+ out[3] = effA;+ }+ return;+ }++ if (mode === 'compare-density') {+ const dA = da * INV_255;+ const dstW = dA * (1.0 - sA);+ const denom = sA + dstW;+ if (denom <= 1e-5) {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ out[3] = da;+ return;+ }+ pigmentMixRgbOut(dr, dg, db, sr, sg, sb, sA / denom, pigmentStrength, _compRgb);+ out[0] = clamp255(Math.round(_compRgb.r));+ out[1] = clamp255(Math.round(_compRgb.g));+ out[2] = clamp255(Math.round(_compRgb.b));+ out[3] = effA > da ? effA : da;+ return;+ }++ // Standard source-over in spectral Kubelka-Munk pigment space+ if (effA >= 255) {+ out[0] = sr;+ out[1] = sg;+ out[2] = sb;+ out[3] = 255;+ return;+ }++ const dA = da * INV_255;+ const compA = sA + dA * (1.0 - sA);+ if (compA <= 1e-5) {+ out[0] = dr;+ out[1] = dg;+ out[2] = db;+ out[3] = da;+ return;+ }++ // Optical white-paper ground compositing for translucent pixels over a visible white canvas+ if (pigmentWhiteGround && da < 255) {+ const invDA = 1.0 - dA;+ const visDr = dr * dA + 255.0 * invDA;+ const visDg = dg * dA + 255.0 * invDA;+ const visDb = db * dA + 255.0 * invDA;+ pigmentMixRgbOut(visDr, visDg, visDb, sr, sg, sb, sA, pigmentStrength, _compRgb);+ const pr = _compRgb.r;+ const pg = _compRgb.g;+ const pb = _compRgb.b;+ const minC = pr < pg ? (pr < pb ? pr : pb) : pg < pb ? pg : pb;+ const minValidA = 1.0 - minC * INV_255;+ const outA = compA > minValidA ? compA : minValidA;+ if (outA <= 1e-5) {+ out[0] = 0;+ out[1] = 0;+ out[2] = 0;+ out[3] = 0;+ return;+ }+ const bg = 255.0 * (1.0 - outA);+ const invOutA = 1.0 / outA;+ out[0] = clamp255(Math.round((pr - bg) * invOutA));+ out[1] = clamp255(Math.round((pg - bg) * invOutA));+ out[2] = clamp255(Math.round((pb - bg) * invOutA));+ out[3] = clamp255(Math.round(outA * 255.0));+ return;+ }++ const t = sA / compA;+ pigmentMixRgbOut(dr, dg, db, sr, sg, sb, t, pigmentStrength, _compRgb);+ out[0] = clamp255(Math.round(_compRgb.r));+ out[1] = clamp255(Math.round(_compRgb.g));+ out[2] = clamp255(Math.round(_compRgb.b));+ out[3] = clamp255(Math.round(compA * 255));+}++/**+ * Detects whether the active layer sits over a visible light/white paper background+ * (e.g., default white `layer-bg`), used to enable optical pigment-over-white dilution+ * on transparent pixels.+ */+export function detectWhitePaperGround(+ layers: LayerItem[] | undefined,+ activeLayerId: string | null | undefined,+ grid?: SparseTileGrid | null+): boolean {+ if (!layers || layers.length === 0) return true;+ const isBgId = (id: string) => id === 'layer-bg' || id.startsWith('layer-bg');+ if (activeLayerId && isBgId(activeLayerId)) {+ return true;+ }+ const bgLayer = layers.find((l) => isBgId(l.id));+ if (!bgLayer) return false;+ if (!bgLayer.visible || bgLayer.opacity < 0.5) return false;++ if (grid) {+ try {+ const tile = grid.getTile(bgLayer.id, 0, 0, 0);+ if (tile) {+ if (tile.solidColorU32 !== undefined) {+ const u = tile.solidColorU32;+ const r = u & 0xff;+ const g = (u >>> 8) & 0xff;+ const b = (u >>> 16) & 0xff;+ const a = (u >>> 24) & 0xff;+ if (a < 180) return false;+ return 0.299 * r + 0.587 * g + 0.114 * b >= 205;+ }+ const px = grid.getPixelRGBA(bgLayer.id, 0, 0);+ if (px[3] >= 180) {+ return 0.299 * px[0] + 0.587 * px[1] + 0.114 * px[2] >= 205;+ }+ }+ } catch {+ // Fallback to light ground assumption when bg layer is visible+ }+ }+ return true;+}++// ============================================================================+// Pure Analytical GLSL Module (WebGL1 & WebGL2 compatible, zero LUT textures)+// ============================================================================++export const SPECTRAL_PIGMENT_GLSL = `+float kmMeasuredToInternal(float rm) {+ float x = max(0.0, (rm - 0.0052) * 1.00522718);+ float ri = x / (0.62 + 0.38 * x);+ return 0.0004 + ri * 0.9986;+}++float kmInternalToMeasured(float ri) {+ float r = clamp((ri - 0.0004) * 1.00140196, 0.0, 1.0);+ float x = (0.62 * r) / max(0.0001, 1.0 - 0.38 * r);+ return 0.0052 + 0.9948 * x;+}++vec3 kmSrgbToLinear(vec3 c) {+ vec3 cl = clamp(c, 0.0, 1.0);+ vec3 lo = cl / 12.92;+ vec3 hi = pow((cl + 0.055) / 1.055, vec3(2.4));+ return mix(lo, hi, step(vec3(0.04045), cl));+}++vec3 kmLinearToSrgb(vec3 c) {+ vec3 cl = clamp(c, 0.0, 1.0);+ vec3 lo = cl * 12.92;+ vec3 hi = 1.055 * pow(max(cl, vec3(0.00001)), vec3(1.0 / 2.4)) - 0.055;+ return clamp(mix(lo, hi, step(vec3(0.0031308), cl)), 0.0, 1.0);+}++float kmBandRefl(vec3 ir, float bR, float bG, float bB, float bC, float bM, float bY) {+ if (ir.r <= ir.g && ir.r <= ir.b) {+ if (ir.g <= ir.b) {+ return ir.r + (ir.g - ir.r) * bC + (ir.b - ir.g) * bB;+ }+ return ir.r + (ir.b - ir.r) * bC + (ir.g - ir.b) * bG;+ } else if (ir.g <= ir.r && ir.g <= ir.b) {+ if (ir.r <= ir.b) {+ return ir.g + (ir.r - ir.g) * bM + (ir.b - ir.r) * bB;+ }+ return ir.g + (ir.b - ir.g) * bM + (ir.r - ir.b) * bR;+ } else {+ if (ir.r <= ir.g) {+ return ir.b + (ir.r - ir.b) * bY + (ir.g - ir.r) * bG;+ }+ return ir.b + (ir.g - ir.b) * bY + (ir.r - ir.g) * bR;+ }+}++float kmMixBandVal(float r1, float r2, float w1, float w2) {+ float c1 = clamp(r1, 0.0004, 0.9995);+ float c2 = clamp(r2, 0.0004, 0.9995);+ float d1 = 1.0 - c1;+ float d2 = 1.0 - c2;+ float ks1 = (d1 * d1) / (2.0 * c1);+ float ks2 = (d2 * d2) / (2.0 * c2);+ float u1 = ks1 > 1e-12 ? pow(ks1, 0.33333334) : 0.0;+ float u2 = ks2 > 1e-12 ? pow(ks2, 0.33333334) : 0.0;+ float um = max(0.0, u1 * w1 + u2 * w2);+ float ks = um * um * um;+ return kmInternalToMeasured(1.0 + ks - sqrt(ks * ks + 2.0 * ks));+}++vec3 mixPigmentsGpu(vec3 c1, vec3 c2, float t, float strength) {+ float ct = clamp(t, 0.0, 1.0);+ vec3 lin = mix(c1, c2, ct);+ float s = clamp(strength, 0.0, 1.0);+ if (s <= 0.005 || ct <= 0.0001 || ct >= 0.9999) {+ return lin;+ }+ float ch1 = max(max(c1.r, c1.g), c1.b) - min(min(c1.r, c1.g), c1.b);+ float ch2 = max(max(c2.r, c2.g), c2.b) - min(min(c2.r, c2.g), c2.b);+ float maxCh = max(ch1, ch2);+ if (maxCh <= 0.004) {+ return lin;+ }++ vec3 l1 = kmSrgbToLinear(c1);+ vec3 l2 = kmSrgbToLinear(c2);+ vec3 ir1 = vec3(kmMeasuredToInternal(l1.r), kmMeasuredToInternal(l1.g), kmMeasuredToInternal(l1.b));+ vec3 ir2 = vec3(kmMeasuredToInternal(l2.r), kmMeasuredToInternal(l2.g), kmMeasuredToInternal(l2.b));++ float lum1 = dot(l1, vec3(0.2126, 0.7152, 0.0722));+ float lum2 = dot(l2, vec3(0.2126, 0.7152, 0.0722));+ float min1 = min(min(l1.r, l1.g), l1.b);+ float min2 = min(min(l2.r, l2.g), l2.b);+ float max1 = clamp(max(max(l1.r, l1.g), l1.b), 0.0, 1.0);+ float max2 = clamp(max(max(l2.r, l2.g), l2.b), 0.0, 1.0);+ float cMax1 = max1 > 1e-12 ? pow(max1, 0.33333334) : 0.0;+ float cMax2 = max2 > 1e-12 ? pow(max2, 0.33333334) : 0.0;+ float sc1 = 0.04 + 0.12 * sqrt(clamp(lum1, 0.0, 1.0)) + 0.66 * cMax1 + 0.18 * sqrt(clamp(min1, 0.0, 1.0));+ float sc2 = 0.04 + 0.12 * sqrt(clamp(lum2, 0.0, 1.0)) + 0.66 * cMax2 + 0.18 * sqrt(clamp(min2, 0.0, 1.0));+ float invT = 1.0 - ct;+ float w1 = (invT * sc1) / max(0.000001, invT * sc1 + ct * sc2);+ float w2 = 1.0 - w1;++ vec3 b1 = vec3(0.0);+ vec3 b2 = vec3(0.0);+ vec3 mBase = vec3(0.0);++ float r1, r2, rm1, rm2, rmM;++ // Band 0 (410nm): W_B = 0.26+ r1 = clamp(kmBandRefl(ir1, 0.240, 0.001, 1.000, 0.850, 1.000, 0.004), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 0.240, 0.001, 1.000, 0.850, 1.000, 0.004), 0.0004, 0.9995);+ b1.b += kmInternalToMeasured(r1) * 0.26;+ b2.b += kmInternalToMeasured(r2) * 0.26;+ mBase.b += kmMixBandVal(r1, r2, w1, w2) * 0.26;++ // Band 1 (450nm): W_B = 0.66+ r1 = clamp(kmBandRefl(ir1, 0.014, 0.001, 1.000, 0.995, 0.995, 0.004), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 0.014, 0.001, 1.000, 0.995, 0.995, 0.004), 0.0004, 0.9995);+ b1.b += kmInternalToMeasured(r1) * 0.66;+ b2.b += kmInternalToMeasured(r2) * 0.66;+ mBase.b += kmMixBandVal(r1, r2, w1, w2) * 0.66;++ // Band 2 (490nm): W_G = 0.06, W_B = 0.08+ r1 = clamp(kmBandRefl(ir1, 0.001, 0.080, 0.960, 1.000, 0.080, 0.020), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 0.001, 0.080, 0.960, 1.000, 0.080, 0.020), 0.0004, 0.9995);+ rm1 = kmInternalToMeasured(r1);+ rm2 = kmInternalToMeasured(r2);+ rmM = kmMixBandVal(r1, r2, w1, w2);+ b1.g += rm1 * 0.06; b1.b += rm1 * 0.08;+ b2.g += rm2 * 0.06; b2.b += rm2 * 0.08;+ mBase.g += rmM * 0.06; mBase.b += rmM * 0.08;++ // Band 3 (525nm): W_G = 0.56+ r1 = clamp(kmBandRefl(ir1, 0.001, 0.995, 0.520, 0.995, 0.001, 0.980), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 0.001, 0.995, 0.520, 0.995, 0.001, 0.980), 0.0004, 0.9995);+ b1.g += kmInternalToMeasured(r1) * 0.56;+ b2.g += kmInternalToMeasured(r2) * 0.56;+ mBase.g += kmMixBandVal(r1, r2, w1, w2) * 0.56;++ // Band 4 (565nm): W_G = 0.38+ r1 = clamp(kmBandRefl(ir1, 0.130, 1.000, 0.028, 0.620, 0.003, 1.000), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 0.130, 1.000, 0.028, 0.620, 0.003, 1.000), 0.0004, 0.9995);+ b1.g += kmInternalToMeasured(r1) * 0.38;+ b2.g += kmInternalToMeasured(r2) * 0.38;+ mBase.g += kmMixBandVal(r1, r2, w1, w2) * 0.38;++ // Band 5 (600nm): W_R = 0.34+ r1 = clamp(kmBandRefl(ir1, 0.960, 0.650, 0.001, 0.002, 0.850, 1.000), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 0.960, 0.650, 0.001, 0.002, 0.850, 1.000), 0.0004, 0.9995);+ b1.r += kmInternalToMeasured(r1) * 0.34;+ b2.r += kmInternalToMeasured(r2) * 0.34;+ mBase.r += kmMixBandVal(r1, r2, w1, w2) * 0.34;++ // Band 6 (640nm): W_R = 0.48+ r1 = clamp(kmBandRefl(ir1, 1.000, 0.015, 0.001, 0.001, 1.000, 1.000), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 1.000, 0.015, 0.001, 0.001, 1.000, 1.000), 0.0004, 0.9995);+ b1.r += kmInternalToMeasured(r1) * 0.48;+ b2.r += kmInternalToMeasured(r2) * 0.48;+ mBase.r += kmMixBandVal(r1, r2, w1, w2) * 0.48;++ // Band 7 (680nm): W_R = 0.18+ r1 = clamp(kmBandRefl(ir1, 1.000, 0.001, 0.060, 0.001, 1.000, 1.000), 0.0004, 0.9995);+ r2 = clamp(kmBandRefl(ir2, 1.000, 0.001, 0.060, 0.001, 1.000, 1.000), 0.0004, 0.9995);+ b1.r += kmInternalToMeasured(r1) * 0.18;+ b2.r += kmInternalToMeasured(r2) * 0.18;+ mBase.r += kmMixBandVal(r1, r2, w1, w2) * 0.18;++ vec3 res1 = l1 - b1;+ vec3 res2 = l2 - b2;+ vec3 pig = kmLinearToSrgb(mBase + res1 * invT + res2 * ct);++ float effS = s * smoothstep(0.004, 0.032, maxCh);+ return mix(lin, pig, effS);+}+`;diff --git a/src/utils/tileEngine/strokeComposite.ts b/src/utils/tileEngine/strokeComposite.tsindex 5f9a192..6615648 100644--- a/src/utils/tileEngine/strokeComposite.ts+++ b/src/utils/tileEngine/strokeComposite.ts@@ -8,7 +8,7 @@ import type { CspBrushCombineMode } from '../../types/editor';import {compositePigmentOverPixelInto,isPigmentCompatibleCombineMode,-} from '../pigmentEngine';+} from '../spectralPigmentEngine';import { packRGBATo1Bit, packRGBATo8Bit, unpack8BitToRGBA } from './pixelCodec';import {ISparseTileGridHost,