Skip to content

Open source CapCut alternative.

TypeScript
23.3 KB
Raw Blame
/**
 * One mapping per clip. A clip's color is a pure function from its source
 * code values to the project's output code values, baked into one 3D LUT
 * that the WebGL pass, the CPU pass and ffmpeg's lut3d consume unchanged:
 *
 *   1 source conversion → the output's display values (none for Rec.709 /
 *     sRGB in SDR, and none for an HLG or PQ file delivered in its own
 *     encoding; Apple Log and Apple Log 2 decode to scene-linear ACES and go
 *     through the ACES 2.0 output transform — the Rec.709 100-nit transform
 *     for an SDR project, the Rec.2100 1000-nit HLG one when the project
 *     delivers HDR; HLG and PQ are display-referred, so they reach SDR
 *     through the BT.2100 OOTF on a 100-nit display, and cross into each
 *     other through the transfer at 1000 nits, so an HDR source keeps its
 *     range)
 *   2 the clip's library LUT, mixed by its amount
 *   3 the grade (manual layer, then preset), gradeMath.ts
 *   4 output: SDR as is; an SDR source delivered as HDR takes the BT.2408
 *     mapping (Rec.709 white to 203 nits in the Rec.2020 container)
 *
 * Sharpen and clarity are spatial and run on the picture afterwards
 * (detail.ts); they never enter the LUT or its key.
 *
 * A graded log clip takes stages 1 and 3 in the other order: the grade works
 * on the scene light the camera recorded and the ACES output transform
 * renders the result, so its shoulder carries a brightened sky into white
 * the way it carries the ungraded one; the LUT then reads the rendered
 * picture.
 *
 * The source conversion is the expensive stage (the ACES transform). An
 * ungraded or display-referred clip samples it once onto its own lattice and
 * composes the cheap stages on top per edit; a graded log clip runs the
 * transform per node, on the draft cube while a slider moves.
 */

import { type ColorGrade, gradeWithoutDetail } from "./colorGrade";
import { type AcesOutputPresetId, acesOutputTransform } from "./acesOutput";
import {
  type CodeFormat,
  type Mat3,
  type OutputSpace,
  type SourceProfile,
  AP0_PRIMARIES,
  APPLE_WIDE_GAMUT_PRIMARIES,
  BRADFORD,
  REC2020_PRIMARIES,
  REC709_PRIMARIES,
  appleLogDecode,
  bt1886Decode,
  hlgOetf,
  hlgOetfInverse,
  hlgOotf,
  hlgOotfInverse,
  mat3Apply,
  mat3Inv,
  pqDecode,
  pqEncode,
  rgbToRgbMatrix,
  sdrToHlg,
  sdrToPq,
  srgbEncode,
} from "./colorSpace";
import { type GradeLut, GRADE_LUT_SIZE, stableJson } from "./gradeLut";
import { type GradeSpace, type GradeTransform, createGradeTransform, sceneGradeSpace } from "./gradeMath";
import { type ParsedLut, sampleLut } from "./lutFile";

/** The file's Y'CbCr matrix and range beside the ones the decoder drew the
 * picture with. A browser decoder reads its matrix from tags, and the tags
 * are rewritten to Rec.709 so it draws code values; the picture then holds
 * R'G'B' made through the wrong matrix, and the source stage undoes that
 * before anything else. */
export interface DrawnColor {
  matrix: CodeFormat["matrix"];
  fullRange: boolean;
  drawnMatrix: CodeFormat["matrix"];
  drawnFullRange: boolean;
}

export interface ClipColorRecipe extends Partial<DrawnColor> {
  profile: SourceProfile;
  grade?: ColorGrade | null;
  output: OutputSpace;
  /** Lattice nodes per axis. */
  size: number;
}

/** True when the picture was drawn through a matrix or range that differs
 * from the file's. Missing fields mean Rec.709 limited on both sides. */
export function needsMatrixFix(drawn: Partial<DrawnColor> | undefined): drawn is DrawnColor {
  if (!drawn || drawn.drawnMatrix === undefined) return false;
  return drawn.drawnMatrix !== (drawn.matrix ?? "bt709") || (drawn.drawnFullRange ?? false) !== (drawn.fullRange ?? false);
}

/** True when the source's code values already are the output's: Rec.709 or
 * sRGB delivered as SDR, or an HLG or PQ file delivered in its own encoding,
 * drawn through the file's own matrix. */
export function recipeIsIdentity(profile: SourceProfile, output: OutputSpace, drawn?: Partial<DrawnColor>): boolean {
  const same = output === "sdr" ? profile === "rec709" || profile === "srgb" : profile === output;
  return same && !needsMatrixFix(drawn);
}

/** The space an HDR delivery composites in: every clip, graphic and effect
 * meets in HLG, and a PQ file is the final pass over the finished picture. */
export function compositeSpaceFor(output: OutputSpace): OutputSpace {
  return output === "sdr" ? "sdr" : "hlg";
}

/** The fix spelled for a cache key: "" when the picture needs none. */
export const drawnColorKey = (drawn: Partial<DrawnColor> | undefined): string =>
  needsMatrixFix(drawn)
    ? `${drawn.drawnMatrix}${drawn.drawnFullRange ? "f" : "l"}>${drawn.matrix}${drawn.fullRange ? "f" : "l"}`
    : "";

const isSdrSource = (profile: SourceProfile) => profile === "rec709" || profile === "srgb";
const isHdrSource = (profile: SourceProfile) => profile === "hlg" || profile === "pq";

/** A stable identity for the recipe's baked result. The LUT is referenced by
 * its id; the spatial controls are left out. "" when the recipe is an
 * identity mapping with no grade. */
export function recipeKey(recipe: ClipColorRecipe): string {
  const grade = gradeWithoutDetail(recipe.grade);
  if (!grade && recipeIsIdentity(recipe.profile, recipe.output, recipe)) return "";
  const drawn = drawnColorKey(recipe);
  return stableJson({
    profile: isSdrSource(recipe.profile) ? "rec709" : recipe.profile,
    output: recipe.output,
    size: recipe.size,
    ...(drawn ? { drawn } : {}),
    ...(grade ? { grade } : {}),
  });
}

/* ------------------------------------------------------------------ */
/* Stage 0: the drawn matrix                                           */
/* ------------------------------------------------------------------ */

/** Luma weights (Kr, Kb) of each matrix; Kg is what is left. */
const LUMA_WEIGHTS: Record<CodeFormat["matrix"], [number, number]> = {
  bt709: [0.2126, 0.0722],
  bt601: [0.299, 0.114],
  bt2020nc: [0.2627, 0.0593],
};

/** R'G'B' → Y'CbCr (Y' in [0,1], Cb/Cr centered on 0). */
function rgbToYccMatrix(matrix: CodeFormat["matrix"]): Mat3 {
  const [kr, kb] = LUMA_WEIGHTS[matrix];
  const kg = 1 - kr - kb;
  return [kr, kg, kb, -kr / (2 * (1 - kb)), -kg / (2 * (1 - kb)), 0.5, 0.5, -kg / (2 * (1 - kr)), -kb / (2 * (1 - kr))];
}

/** Y'CbCr code values (each in [0,1] of its 8-bit scale, chroma centered on
 * 128/255) → R'G'B' through the matrix at the range. What a decoder draws. */
export function decodeYcc(
  format: Pick<CodeFormat, "matrix" | "fullRange">,
  y: number,
  cb: number,
  cr: number
): [number, number, number] {
  const [yy, cbb, crr] = format.fullRange
    ? [y, cb - 128 / 255, cr - 128 / 255]
    : [((y * 255 - 16) / 219), ((cb * 255 - 128) / 224), ((cr * 255 - 128) / 224)];
  return mat3Apply(mat3Inv(rgbToYccMatrix(format.matrix)), yy, cbb, crr);
}

/** R'G'B' → Y'CbCr code values at the range: the inverse of `decodeYcc`. */
function encodeYcc(format: Pick<CodeFormat, "matrix" | "fullRange">, r: number, g: number, b: number): [number, number, number] {
  const [y, cb, cr] = mat3Apply(rgbToYccMatrix(format.matrix), r, g, b);
  return format.fullRange
    ? [y, cb + 128 / 255, cr + 128 / 255]
    : [(16 + 219 * y) / 255, (128 + 224 * cb) / 255, (128 + 224 * cr) / 255];
}

/** The affine map from the drawn R'G'B' back to the code values and out
 * through the file's own matrix and range. */
function matrixFixTransform(drawn: DrawnColor): GradeTransform {
  const fix = (r: number, g: number, b: number): [number, number, number] => {
    const [y, cb, cr] = encodeYcc({ matrix: drawn.drawnMatrix, fullRange: drawn.drawnFullRange }, r, g, b);
    return decodeYcc({ matrix: drawn.matrix, fullRange: drawn.fullRange }, y, cb, cr);
  };
  // The map is affine, so three basis probes and the offset pin it down.
  const t = fix(0, 0, 0);
  const cr = fix(1, 0, 0);
  const cg = fix(0, 1, 0);
  const cb = fix(0, 0, 1);
  const m: Mat3 = [
    cr[0] - t[0], cg[0] - t[0], cb[0] - t[0],
    cr[1] - t[1], cg[1] - t[1], cb[1] - t[1],
    cr[2] - t[2], cg[2] - t[2], cb[2] - t[2],
  ];
  return (r, g, b) => {
    const v = mat3Apply(m, r, g, b);
    return [v[0] + t[0], v[1] + t[1], v[2] + t[2]];
  };
}

/* ------------------------------------------------------------------ */
/* Stage 1: source conversion                                          */
/* ------------------------------------------------------------------ */

const clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);

const REC2020_TO_AP0 = rgbToRgbMatrix(REC2020_PRIMARIES, AP0_PRIMARIES, BRADFORD);
const AP0_TO_REC2020 = mat3Inv(REC2020_TO_AP0);
const AWG_TO_AP0 = rgbToRgbMatrix(APPLE_WIDE_GAMUT_PRIMARIES, AP0_PRIMARIES, BRADFORD);
const REC2020_TO_REC709 = rgbToRgbMatrix(REC2020_PRIMARIES, REC709_PRIMARIES);
const SDR_PEAK_NITS = 100;

function acesPresetFor(output: OutputSpace): AcesOutputPresetId {
  return output === "hlg" ? "rec2100-hlg-1000" : output === "pq" ? "rec2100-pq-1000" : "rec709-100";
}

/** Code values → scene-linear AP0 for the log profiles. */
function sceneDecoder(profile: SourceProfile): ((r: number, g: number, b: number) => [number, number, number]) | null {
  const toAp0 = (m: Mat3, r: number, g: number, b: number): [number, number, number] => [
    m[0] * r + m[1] * g + m[2] * b,
    m[3] * r + m[4] * g + m[5] * b,
    m[6] * r + m[7] * g + m[8] * b,
  ];
  switch (profile) {
    case "apple-log":
      return (r, g, b) => toAp0(REC2020_TO_AP0, appleLogDecode(r), appleLogDecode(g), appleLogDecode(b));
    case "apple-log-2":
      return (r, g, b) => toAp0(AWG_TO_AP0, appleLogDecode(r), appleLogDecode(g), appleLogDecode(b));
    default:
      return null;
  }
}

/** The grade space of a log clip: scene light in Rec.2020 primaries, encoded
 * by the output transform's response to grey. One per output transform. */
const sceneSpaces = new Map<AcesOutputPresetId, GradeSpace>();

function sceneSpaceFor(output: OutputSpace): GradeSpace {
  const id = acesPresetFor(output);
  let space = sceneSpaces.get(id);
  if (space) return space;
  const ot = acesOutputTransform(id);
  space = sceneGradeSpace(output, REC2020_PRIMARIES, (y) => {
    const [r, g, b] = mat3Apply(REC2020_TO_AP0, y, y, y);
    return ot.forward(r, g, b)[1];
  });
  sceneSpaces.set(id, space);
  return space;
}

/** Code values → the graded scene → the output transform, for a log clip
 * with a grade: the grade runs on scene light and the transform renders it.
 * Null when the profile is not log or the grade is neutral. */
function sceneGradedTransform(recipe: ClipColorRecipe): GradeTransform | null {
  const scene = sceneDecoder(recipe.profile);
  if (!scene) return null;
  const space = sceneSpaceFor(recipe.output);
  const gradeFn = createGradeTransform(gradeWithoutDetail(recipe.grade), space);
  if (!gradeFn) return null;
  const fix = needsMatrixFix(recipe) ? matrixFixTransform(recipe) : null;
  const ot = acesOutputTransform(acesPresetFor(recipe.output));
  return (r, g, b) => {
    const [fr, fg, fb] = fix ? fix(r, g, b) : [r, g, b];
    const [ar, ag, ab] = scene(clamp01(fr), clamp01(fg), clamp01(fb));
    const [sr, sg, sb] = mat3Apply(AP0_TO_REC2020, ar, ag, ab);
    const [gr, gg, gb] = gradeFn(space.encode(sr), space.encode(sg), space.encode(sb));
    const [or, og, ob] = mat3Apply(REC2020_TO_AP0, space.decode(gr), space.decode(gg), space.decode(gb));
    return ot.forward(or, og, ob);
  };
}

/** HLG signal → PQ signal on a 1000-nit display: the BT.2100 OOTF gives the
 * display light, and ST 2084 encodes it. What the final pass of a PQ
 * delivery runs over the HLG composite. */
export function hlgToPq(r: number, g: number, b: number): [number, number, number] {
  const [rd, gd, bd] = hlgOotf(hlgOetfInverse(r), hlgOetfInverse(g), hlgOetfInverse(b), 1000);
  return [pqEncode(rd), pqEncode(gd), pqEncode(bd)];
}

/** PQ signal → HLG signal, the inverse: display light back through the
 * BT.2390 inverse OOTF at 1000 nits and the HLG OETF. */
export function pqToHlg(r: number, g: number, b: number): [number, number, number] {
  const [rs, gs, bs] = hlgOotfInverse(pqDecode(r), pqDecode(g), pqDecode(b), 1000);
  return [hlgOetf(rs), hlgOetf(gs), hlgOetf(bs)];
}

/** HLG signal → SDR signal: the HLG picture on a 100-nit display, the
 * BT.2100 OOTF at that peak's system gamma with HLG's peak at SDR white,
 * moved into Rec.709 primaries, clipped and sRGB-encoded. Display-referred,
 * so hue and contrast hold; the rendition a phone's own player shows on an
 * SDR screen, within a level or two. */
export function hlgToSdr(r: number, g: number, b: number): [number, number, number] {
  const k = 1 / SDR_PEAK_NITS;
  const [rd, gd, bd] = hlgOotf(hlgOetfInverse(r), hlgOetfInverse(g), hlgOetfInverse(b), SDR_PEAK_NITS);
  const [x, y, z] = mat3Apply(REC2020_TO_REC709, rd * k, gd * k, bd * k);
  return [srgbEncode(clamp01(x)), srgbEncode(clamp01(y)), srgbEncode(clamp01(z))];
}

/** The source conversion for a profile into the output's display values, or
 * null when the source already is display values in the grade's container.
 * A drawn matrix that differs from the file's is undone first. An HDR file
 * delivered as the other HDR encoding crosses through the transfer alone —
 * same range, same display, so nothing is re-rendered. */
export function sourceTransform(
  profile: SourceProfile,
  output: OutputSpace,
  drawn?: Partial<DrawnColor>
): GradeTransform | null {
  const fix = needsMatrixFix(drawn) ? matrixFixTransform(drawn) : null;
  if (recipeIsIdentity(profile, output)) return fix;
  if (isHdrSource(profile)) {
    const cross =
      output === "sdr"
        ? profile === "pq"
          ? (r: number, g: number, b: number) => hlgToSdr(...pqToHlg(r, g, b))
          : hlgToSdr
        : output === "pq"
          ? hlgToPq
          : pqToHlg;
    return (r, g, b) => {
      const [fr, fg, fb] = fix ? fix(r, g, b) : [r, g, b];
      return cross(clamp01(fr), clamp01(fg), clamp01(fb));
    };
  }
  const scene = sceneDecoder(profile);
  if (!scene) return fix;
  const ot = acesOutputTransform(acesPresetFor(output));
  return (r, g, b) => {
    const [fr, fg, fb] = fix ? fix(r, g, b) : [r, g, b];
    const [ar, ag, ab] = scene(clamp01(fr), clamp01(fg), clamp01(fb));
    return ot.forward(ar, ag, ab);
  };
}

/* ------------------------------------------------------------------ */
/* Stage 4: output                                                     */
/* ------------------------------------------------------------------ */

/** SDR display values into the HDR container (BT.2408), or null when the
 * grade already worked in the output container. */
function outputTransform(profile: SourceProfile, output: OutputSpace): GradeTransform | null {
  if (output === "sdr" || !isSdrSource(profile)) return null;
  const map = output === "hlg" ? sdrToHlg : sdrToPq;
  return (r, g, b) => map(bt1886Decode(r), bt1886Decode(g), bt1886Decode(b));
}

/* ------------------------------------------------------------------ */
/* The composed mapping                                                */
/* ------------------------------------------------------------------ */

/** The library LUT mixed by its amount, on display values; null without one. */
function lutStage(recipe: ClipColorRecipe, userLut: ParsedLut | undefined): GradeTransform | null {
  const lutRef = recipe.grade?.lut;
  const amount = lutRef && userLut ? (lutRef.amount ?? 1) : 0;
  if (!amount || !userLut) return null;
  return (r, g, b) => {
    const l = sampleLut(userLut, clamp01(r), clamp01(g), clamp01(b));
    return [r + (l[0] - r) * amount, g + (l[1] - g) * amount, b + (l[2] - b) * amount];
  };
}

/** Stages 2–4: the LUT, the grade and the output mapping on display values. */
function postSourceTransform(recipe: ClipColorRecipe, userLut: ParsedLut | undefined): GradeTransform | null {
  const grade = gradeWithoutDetail(recipe.grade);
  const gradeSpace: OutputSpace = isSdrSource(recipe.profile) ? "sdr" : recipe.output;
  const lutFn = lutStage(recipe, userLut);
  const gradeFn = createGradeTransform(grade, gradeSpace);
  const outFn = outputTransform(recipe.profile, recipe.output);
  if (!lutFn && !gradeFn && !outFn) return null;
  return (r, g, b) => {
    let v: [number, number, number] = [r, g, b];
    if (lutFn) v = lutFn(v[0], v[1], v[2]);
    if (gradeFn) v = gradeFn(v[0], v[1], v[2]);
    if (outFn) v = outFn(clamp01(v[0]), clamp01(v[1]), clamp01(v[2]));
    return v;
  };
}

/** The whole mapping of a graded log clip: the scene grade and its rendering,
 * then the LUT on the rendered picture. Null for every other recipe. */
function sceneClipTransform(recipe: ClipColorRecipe, userLut: ParsedLut | undefined): GradeTransform | null {
  const graded = sceneGradedTransform(recipe);
  if (!graded) return null;
  const lutFn = lutStage(recipe, userLut);
  if (!lutFn) return graded;
  return (r, g, b) => {
    const v = graded(r, g, b);
    return lutFn(v[0], v[1], v[2]);
  };
}

/**
 * Compile the recipe into the pure mapping from source code values to output
 * code values, both in [0,1]. Returns null when the mapping is identity: an
 * SDR-in-SDR clip with no grade and no LUT.
 */
export function createClipTransform(recipe: ClipColorRecipe, userLut?: ParsedLut): GradeTransform | null {
  const scene = sceneClipTransform(recipe, userLut);
  if (scene) {
    return (r, g, b) => {
      const v = scene(r, g, b);
      return [clamp01(v[0]), clamp01(v[1]), clamp01(v[2])];
    };
  }
  const source = sourceTransform(recipe.profile, recipe.output, recipe);
  const post = postSourceTransform(recipe, userLut);
  if (!source && !post) return null;
  return (r, g, b) => {
    let v: [number, number, number] = source ? source(r, g, b) : [r, g, b];
    if (post) v = post(v[0], v[1], v[2]);
    return [clamp01(v[0]), clamp01(v[1]), clamp01(v[2])];
  };
}

/* ------------------------------------------------------------------ */
/* Lattices                                                            */
/* ------------------------------------------------------------------ */

/** The source conversion sampled onto a lattice: display values of the
 * output container at every node of the source cube. */
export interface SourceLattice {
  profile: SourceProfile;
  output: OutputSpace;
  size: number;
  /** The drawn-matrix fix baked in, as `drawnKey` spells it; "" for none. */
  drawn: string;
  data: Float32Array;
}

function fillLattice(size: number, fn: GradeTransform): Float32Array {
  const data = new Float32Array(size * size * size * 3);
  const step = 1 / (size - 1);
  let i = 0;
  for (let b = 0; b < size; b++) {
    for (let g = 0; g < size; g++) {
      for (let r = 0; r < size; r++) {
        const out = fn(r * step, g * step, b * step);
        data[i++] = out[0];
        data[i++] = out[1];
        data[i++] = out[2];
      }
    }
  }
  return data;
}

/** A fixed mapping sampled onto a lattice: the HLG → PQ pass a PQ delivery
 * ends with, and the sRGB → HLG mapping every graphic takes into an HDR
 * composite. */
export function buildTransferLut(size: number, fn: GradeTransform): GradeLut {
  return {
    size,
    data: fillLattice(size, (r, g, b) => {
      const v = fn(r, g, b);
      return [clamp01(v[0]), clamp01(v[1]), clamp01(v[2])];
    }),
  };
}

/** The mapping a graphic drawn in sRGB — a title, a caption, a sticker, the
 * frame color — takes into the HLG composite: BT.2408, white at 75%. */
export function graphicsToHlg(r: number, g: number, b: number): [number, number, number] {
  return sdrToHlg(bt1886Decode(clamp01(r)), bt1886Decode(clamp01(g)), bt1886Decode(clamp01(b)));
}

/** A CSS hex color mapped into the HLG composite, as hex again. */
export function hexToHlgHex(hex: string): string {
  const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
  if (!m) return hex;
  const n = parseInt(m[1], 16);
  const [r, g, b] = graphicsToHlg(((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255);
  const q = (v: number) => Math.round(clamp01(v) * 255).toString(16).padStart(2, "0");
  return `#${q(r)}${q(g)}${q(b)}`;
}

/** Sample the source conversion alone. Null when the profile needs none. */
export function buildSourceLattice(
  profile: SourceProfile,
  output: OutputSpace,
  size: number,
  drawn?: Partial<DrawnColor>
): SourceLattice | null {
  const source = sourceTransform(profile, output, drawn);
  if (!source) return null;
  return { profile, output, size, drawn: drawnColorKey(drawn), data: fillLattice(size, source) };
}

/** A few source lattices kept for the grade-only rebuilds while sliders
 * move; the cap is bytes, so four 65³ lattices at most. */
const SOURCE_LATTICE_CACHE_BYTES = 16 * 1024 * 1024;
const sourceLatticeCache = new Map<string, SourceLattice>();
let sourceLatticeBytes = 0;

function cachedSourceLattice(
  profile: SourceProfile,
  output: OutputSpace,
  size: number,
  drawn: Partial<DrawnColor> | undefined
): SourceLattice | null {
  const key = `${profile}|${output}|${size}|${drawnColorKey(drawn)}`;
  const hit = sourceLatticeCache.get(key);
  if (hit) {
    sourceLatticeCache.delete(key);
    sourceLatticeCache.set(key, hit);
    return hit;
  }
  const built = buildSourceLattice(profile, output, size, drawn);
  if (!built) return null;
  const bytes = built.data.byteLength;
  while (sourceLatticeBytes + bytes > SOURCE_LATTICE_CACHE_BYTES && sourceLatticeCache.size) {
    const oldest = sourceLatticeCache.keys().next().value as string;
    sourceLatticeBytes -= sourceLatticeCache.get(oldest)!.data.byteLength;
    sourceLatticeCache.delete(oldest);
  }
  sourceLatticeCache.set(key, built);
  sourceLatticeBytes += bytes;
  return built;
}

export function clearSourceLatticeCache(): void {
  sourceLatticeCache.clear();
  sourceLatticeBytes = 0;
}

/**
 * Sample the whole recipe onto the lattice. Returns null for an identity
 * mapping. A source lattice of the same profile, output and size composes the
 * cheap stages over it; without one the cached lattice is used, built on
 * first sight.
 */
export function buildClipLut(recipe: ClipColorRecipe, userLut?: ParsedLut, source?: SourceLattice): GradeLut | null {
  const size = recipe.size;
  // A graded log clip runs the output transform after its grade, so no
  // rendered lattice can stand in for it: every node takes the whole path.
  const scene = sceneClipTransform(recipe, userLut);
  if (scene) {
    return {
      size,
      data: fillLattice(size, (r, g, b) => {
        const v = scene(r, g, b);
        return [clamp01(v[0]), clamp01(v[1]), clamp01(v[2])];
      }),
    };
  }
  const post = postSourceTransform(recipe, userLut);
  const lattice =
    source &&
    source.profile === recipe.profile &&
    source.output === recipe.output &&
    source.size === size &&
    source.drawn === drawnColorKey(recipe)
      ? source
      : cachedSourceLattice(recipe.profile, recipe.output, size, recipe);
  if (!lattice) {
    if (!post) return null;
    return {
      size,
      data: fillLattice(size, (r, g, b) => {
        const v = post(r, g, b);
        return [clamp01(v[0]), clamp01(v[1]), clamp01(v[2])];
      }),
    };
  }
  const src = lattice.data;
  const data = new Float32Array(src.length);
  if (!post) {
    for (let i = 0; i < src.length; i++) data[i] = clamp01(src[i]);
    return { size, data };
  }
  for (let i = 0; i < src.length; i += 3) {
    const v = post(src[i], src[i + 1], src[i + 2]);
    data[i] = clamp01(v[0]);
    data[i + 1] = clamp01(v[1]);
    data[i + 2] = clamp01(v[2]);
  }
  return { size, data };
}

/** The LUT of a grade alone on a Rec.709 source delivered as SDR: preset
 * tiles, thumbnails and the grade tests. */
export function buildGradeLut(g: ColorGrade | undefined | null, size = GRADE_LUT_SIZE): GradeLut | null {
  return buildClipLut({ profile: "rec709", grade: g, output: "sdr", size });
}