Files
luminary/src/lib/colorSpace.ts
T
bhursey 0c113e7a91 Land camera-native raw profiles, real ICC soft proofing and 16-bit proxies.
Camera profiles, proofing tables and the half-float sidecar all have to agree
with the pixels the editor actually grades, so they land together with the
preset filter menu that was unusable while thumbnails streamed in.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-28 03:59:55 +00:00

104 lines
3.4 KiB
TypeScript

import type { OutputColorSpace } from './icc';
/**
* Output colour conversion.
*
* The renderer grades in sRGB. For a wider delivery space the encoded pixels
* are decoded to linear sRGB, converted with a 3x3 matrix, then encoded with
* the target transfer curve. The browser's canvas still holds 8 bits per
* channel, so this changes the gamut (and the ICC tag), not the bit depth.
*/
type Matrix = readonly [readonly [number, number, number], readonly [number, number, number], readonly [number, number, number]];
const SRGB_TO_P3: Matrix = [
[0.822592, 0.177508, 0.0],
[0.0331993, 0.966801, 0.0],
[0.0170826, 0.0723974, 0.91052],
];
const SRGB_TO_ADOBE: Matrix = [
[0.7151, 0.2849, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.04117, 0.95883],
];
export const COLOR_SPACE_OPTIONS: { value: OutputColorSpace; label: string }[] = [
{ value: 'srgb', label: 'sRGB' },
{ value: 'p3', label: 'Display P3' },
{ value: 'adobe', label: 'Adobe RGB (1998)' },
];
export function colorSpaceLabel(space: OutputColorSpace) {
return COLOR_SPACE_OPTIONS.find((option) => option.value === space)?.label ?? 'sRGB';
}
export function srgbToLinear(value: number): number {
const v = Math.min(1, Math.max(0, value));
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
}
export function linearToSrgb(value: number): number {
const v = Math.max(0, value);
return v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
}
function encode(value: number, space: OutputColorSpace): number {
if (space === 'adobe') return Math.max(0, Math.min(1, value ** (1 / 2.2)));
return Math.max(0, Math.min(1, linearToSrgb(value)));
}
function matrixFor(space: OutputColorSpace): Matrix | null {
if (space === 'p3') return SRGB_TO_P3;
if (space === 'adobe') return SRGB_TO_ADOBE;
return null;
}
/** Convert one sRGB-encoded triple (0-1) to the output space. */
export function convertSrgbEncoded(
r: number,
g: number,
b: number,
space: OutputColorSpace,
): [number, number, number] {
if (space === 'srgb') return [r, g, b];
const matrix = matrixFor(space);
if (!matrix) return [r, g, b];
const lr = srgbToLinear(r);
const lg = srgbToLinear(g);
const lb = srgbToLinear(b);
return [
encode(matrix[0][0] * lr + matrix[0][1] * lg + matrix[0][2] * lb, space),
encode(matrix[1][0] * lr + matrix[1][1] * lg + matrix[1][2] * lb, space),
encode(matrix[2][0] * lr + matrix[2][1] * lg + matrix[2][2] * lb, space),
];
}
/** Convert an RGBA byte buffer in place, preserving alpha. */
export function convertImageData(data: Uint8ClampedArray, space: OutputColorSpace) {
if (space === 'srgb') return data;
for (let i = 0; i < data.length; i += 4) {
const [r, g, b] = convertSrgbEncoded(data[i] / 255, data[i + 1] / 255, data[i + 2] / 255, space);
data[i] = Math.round(r * 255);
data[i + 1] = Math.round(g * 255);
data[i + 2] = Math.round(b * 255);
}
return data;
}
/**
* Convert the renderer's sRGB-encoded float pixels for a 16-bit TIFF. The
* renderer returns gamma-encoded values, so they go through the same decode /
* matrix / encode path as the 8-bit formats.
*/
export function convertFloatPixels(pixels: Float32Array, space: OutputColorSpace) {
if (space === 'srgb') return pixels;
for (let i = 0; i < pixels.length; i += 4) {
const [r, g, b] = convertSrgbEncoded(pixels[i], pixels[i + 1], pixels[i + 2], space);
pixels[i] = r;
pixels[i + 1] = g;
pixels[i + 2] = b;
}
return pixels;
}