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
import { NavigatorPreviewMode } from '../types/workspace';
export const MIN_NAVIGATOR_SPLIT_RATIO = 0.05;
export const MAX_NAVIGATOR_SPLIT_RATIO = 0.95;
export const DEFAULT_NAVIGATOR_SPLIT_RATIO = 0.5;
const LINEAR_LUT_STEPS = 4096;
/**
* Precomputed sRGB byte [0..255] -> linear light [0..1] lookup table.
* Matches `srgbToLinear()` in `softProofShaders.ts` for exact GPU <-> CPU parity.
*/
const SRGB_TO_LINEAR_LUT = new Float32Array(256);
for (let i = 0; i < 256; i++) {
const c = i / 255;
SRGB_TO_LINEAR_LUT[i] = c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
}
/**
* Precomputed linear light [0..1] -> sRGB byte [0..255] lookup table (12-bit precision).
* Matches `linearToSrgb()` in `softProofShaders.ts`.
*/
const LINEAR_TO_SRGB_LUT = new Uint8Array(LINEAR_LUT_STEPS + 1);
for (let i = 0; i <= LINEAR_LUT_STEPS; i++) {
const lin = i / LINEAR_LUT_STEPS;
const srgb = lin <= 0.0031308 ? lin * 12.92 : 1.055 * Math.pow(lin, 1 / 2.4) - 0.055;
LINEAR_TO_SRGB_LUT[i] = Math.max(0, Math.min(255, Math.round(srgb * 255)));
}
/**
* Computes perceptual sRGB value (0..255) using true linear-light Rec.709 / CIE Y luminance:
* Y_lin = 0.2126729 * R_lin + 0.7151522 * G_lin + 0.0721750 * B_lin
* V_srgb = linearToSrgb(Y_lin)
*
* Unlike naive HSL desaturation (which collapses pure blue, red, and yellow to identical 50% gray),
* this accurately reflects human perceived value contrast while preserving neutral grays identically.
*/
export function computePerceptualLuminanceByte(r: number, g: number, b: number): number {
const ri = r <= 0 ? 0 : r >= 255 ? 255 : r | 0;
const gi = g <= 0 ? 0 : g >= 255 ? 255 : g | 0;
const bi = b <= 0 ? 0 : b >= 255 ? 255 : b | 0;
const linY =
0.2126729 * SRGB_TO_LINEAR_LUT[ri] +
0.7151522 * SRGB_TO_LINEAR_LUT[gi] +
0.072175 * SRGB_TO_LINEAR_LUT[bi];
const lutIdx =
linY <= 0 ? 0 : linY >= 1 ? LINEAR_LUT_STEPS : (linY * LINEAR_LUT_STEPS + 0.5) | 0;
return LINEAR_TO_SRGB_LUT[lutIdx];
}
/**
* Converts an ImageData buffer (or horizontal column slice `[startX, endX)`) in-place
* to perceptual B/W tones without altering alpha values.
*/
export function applyPerceptualGrayscaleToImageData(
imageData: Pick<ImageData, 'data' | 'width' | 'height'>,
startX = 0,
endX = imageData.width
): void {
const { data, width, height } = imageData;
const x0 = Math.max(0, Math.min(width, Math.floor(startX)));
const x1 = Math.max(x0, Math.min(width, Math.ceil(endX)));
if (x0 >= x1 || height <= 0) return;
if (x0 === 0 && x1 === width) {
const len = width * height * 4;
for (let i = 0; i < len; i += 4) {
if (data[i + 3] === 0) continue;
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
if (r === g && g === b) continue;
const linY =
0.2126729 * SRGB_TO_LINEAR_LUT[r] +
0.7151522 * SRGB_TO_LINEAR_LUT[g] +
0.072175 * SRGB_TO_LINEAR_LUT[b];
const idx =
linY <= 0 ? 0 : linY >= 1 ? LINEAR_LUT_STEPS : (linY * LINEAR_LUT_STEPS + 0.5) | 0;
const gray = LINEAR_TO_SRGB_LUT[idx];
data[i] = gray;
data[i + 1] = gray;
data[i + 2] = gray;
}
return;
}
for (let y = 0; y < height; y++) {
const rowBase = y * width * 4;
for (let x = x0; x < x1; x++) {
const i = rowBase + x * 4;
if (data[i + 3] === 0) continue;
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
if (r === g && g === b) continue;
const linY =
0.2126729 * SRGB_TO_LINEAR_LUT[r] +
0.7151522 * SRGB_TO_LINEAR_LUT[g] +
0.072175 * SRGB_TO_LINEAR_LUT[b];
const idx =
linY <= 0 ? 0 : linY >= 1 ? LINEAR_LUT_STEPS : (linY * LINEAR_LUT_STEPS + 0.5) | 0;
const gray = LINEAR_TO_SRGB_LUT[idx];
data[i] = gray;
data[i + 1] = gray;
data[i + 2] = gray;
}
}
}
export function clampNavigatorSplitRatio(ratio: number): number {
if (!Number.isFinite(ratio)) return DEFAULT_NAVIGATOR_SPLIT_RATIO;
return Math.max(
MIN_NAVIGATOR_SPLIT_RATIO,
Math.min(MAX_NAVIGATOR_SPLIT_RATIO, Math.round(ratio * 1000) / 1000)
);
}
export function nextNavigatorPreviewMode(mode: NavigatorPreviewMode): NavigatorPreviewMode {
if (mode === 'color') return 'bw';
if (mode === 'bw') return 'split';
return 'color';
}
export function getNavigatorPreviewModeLabel(mode: NavigatorPreviewMode): string {
if (mode === 'bw') return 'Live B/W';
if (mode === 'split') return 'Split View (Color | B/W)';
return 'Color';
}