Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import type {
CanvasControlLayerContract,
CanvasImageRef,
CanvasInpaintMaskLayerContract,
CanvasLayerBaseContract,
CanvasLayerContract,
CanvasLayerSourceContract,
CanvasMaskFillContract,
CanvasPixelSourceContract,
CanvasRegionalGuidanceLayerContract,
} from '@workbench/canvas-engine/contracts';
import type { Rect } from '@workbench/canvas-engine/types';
import { createDefaultControlAdapter } from '@workbench/controlAdapters';
export const DEFAULT_INPAINT_MASK_FILL = { color: '#e07575', style: 'diagonal' } as const;
export const DEFAULT_REGIONAL_GUIDANCE_FILL_COLOR = '#83d683';
const MASK_FILL_COLORS = [
'#e07575',
DEFAULT_REGIONAL_GUIDANCE_FILL_COLOR,
'#f59e42',
'#f4d35e',
'#38c9c5',
'#62b5f5',
'#637be8',
'#ad80e8',
'#ed79ba',
'#b7d65a',
] as const;
type MaskLayerKind = 'inpaint_mask' | 'regional_guidance';
// Keep top-level code side-effect free (no spreads, member reads or account registration): the eager editor bundle
// imports the Canvas API, which would otherwise keep this module. The engine registry registers the account reset.
// New canvases already contain a coral inpaint mask.
const nextColorIndex = { inpaint_mask: 1, regional_guidance: 1 };
export const nextMaskFillColor = (
kind: MaskLayerKind,
offset = 0,
layers: readonly CanvasLayerContract[] = []
): string => {
const used = new Set(layers.flatMap((layer) => (layer.type === kind ? [layer.mask.fill.color.toLowerCase()] : [])));
const start = nextColorIndex[kind];
let available = 0;
for (let step = 0; step < MASK_FILL_COLORS.length; step += 1) {
const color = MASK_FILL_COLORS[(start + step) % MASK_FILL_COLORS.length]!;
if (!used.has(color)) {
if (available === offset) {
return color;
}
available += 1;
}
}
return MASK_FILL_COLORS[(start + offset - available) % MASK_FILL_COLORS.length]!;
};
export const advanceMaskFillColors = (kind: MaskLayerKind, count = 1): void => {
nextColorIndex[kind] = (nextColorIndex[kind] + count) % MASK_FILL_COLORS.length;
};
export const resetMaskFillColors = (): void => {
nextColorIndex.inpaint_mask = 1;
nextColorIndex.regional_guidance = 1;
};
export const nextNumberedName = (prefix: string, existingNames: readonly string[]): string => {
const expression = new RegExp(`^${prefix} (\\d+)$`);
const used = new Set(
existingNames
.map((name) => Number(expression.exec(name.trim())?.[1]))
.filter((number) => Number.isInteger(number) && number > 0)
);
let number = 1;
while (used.has(number)) {
number += 1;
}
return `${prefix} ${String(number)}`;
};
export const nextInpaintMaskName = (names: readonly string[]): string => nextNumberedName('Inpaint Mask', names);
export const nextControlLayerName = (names: readonly string[]): string => nextNumberedName('Control Layer', names);
export const nextRegionalGuidanceName = (names: readonly string[]): string =>
nextNumberedName('Regional Guidance', names);
export interface CreateMaskLayerFromImageInput {
image: CanvasImageRef;
rect: Rect;
id: string;
name: string;
fill: CanvasMaskFillContract;
}
export const createInpaintMaskFromImage = (input: CreateMaskLayerFromImageInput): CanvasInpaintMaskLayerContract => ({
blendMode: 'normal',
id: input.id,
isEnabled: true,
isLocked: false,
mask: {
bitmap: { ...input.image },
fill: { ...input.fill },
offset: { x: input.rect.x, y: input.rect.y },
},
name: input.name,
opacity: 1,
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'inpaint_mask',
});
export const createRegionalGuidanceFromImage = (
input: CreateMaskLayerFromImageInput
): CanvasRegionalGuidanceLayerContract => ({
autoNegative: false,
blendMode: 'normal',
id: input.id,
isEnabled: true,
isLocked: false,
mask: {
bitmap: { ...input.image },
fill: { ...input.fill },
offset: { x: input.rect.x, y: input.rect.y },
},
name: input.name,
negativePrompt: null,
opacity: 0.5,
positivePrompt: null,
referenceImages: [],
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'regional_guidance',
});
export interface CreateControlLayerInput {
name: string;
id: string;
/** Main model base whose adapter kind and defaults the layer starts on. */
base?: string | null;
/** Defaults to an empty paint source. */
source?: CanvasPixelSourceContract;
}
/** The one control-layer factory: untyped, with no model, so generation resolves the model from the type. */
export const createControlLayer = ({
base,
id,
name,
source,
}: CreateControlLayerInput): CanvasControlLayerContract => ({
adapter: createDefaultControlAdapter(base, null),
blendMode: 'normal',
id,
isEnabled: true,
isLocked: false,
name,
opacity: 1,
source: source ?? { bitmap: null, type: 'paint' },
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'control',
withTransparencyEffect: true,
});
/**
* An untyped, model-less control layer made from another layer, keeping its appearance and flags. `content` either
* places baked pixels at `rect` under an identity transform, or reuses a source and transform as they are.
*/
export const createControlLayerFrom = (
from: CanvasLayerBaseContract,
options: {
id: string;
name: string;
base: string | null;
content: { readonly rect: Rect } | { readonly source: CanvasLayerSourceContract };
}
): CanvasControlLayerContract => ({
...createControlLayer({ base: options.base, id: options.id, name: options.name }),
blendMode: from.blendMode,
...(from.colorLabel ? { colorLabel: from.colorLabel } : {}),
isEnabled: from.isEnabled,
isLocked: from.isLocked,
opacity: from.opacity,
...('rect' in options.content
? { source: { bitmap: null, offset: { x: options.content.rect.x, y: options.content.rect.y }, type: 'paint' } }
: { source: structuredClone(options.content.source), transform: { ...from.transform } }),
});