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 React, { useMemo, useState } from 'react';
import {
mixPigmentsWithStrength,
parseColorToRgb,
rgbToHex,
} from '../utils/pigmentMath';
import { ScrubSlider } from './ui';
import { useI18n } from '../i18n';
export interface PigmentBlendControlProps {
/** Whether physical spectral pigment blending is active (false = Regular RGB). */
enabled: boolean;
onEnabledChange: (enabled: boolean) => void;
/** Pigment blending strength in [0, 1] (optional — omit to hide strength slider). */
strength?: number;
onStrengthChange?: (strength: number) => void;
/** Optional active foreground / background colors for the live FG -> BG swatch option. */
primaryColor?: string;
secondaryColor?: string;
/** Section heading label (defaults to "Color Blending"). */
label?: string;
/** Slider label when Pigment mode is active (defaults to "Pigment Strength"). */
strengthLabel?: string;
/** Layout density matching either ToolPropertyInspector ('compact') or BrushStudioModal ('studio'). */
variant?: 'compact' | 'studio';
/** Whether to show the live Regular vs Pigment gradient preview bar (defaults to true). */
showPreview?: boolean;
}
interface SwatchPair {
id: string;
label: string;
c1: string;
c2: string;
}
const DEFAULT_PAIRS: readonly SwatchPair[] = [
{ id: 'blue-yellow', label: 'Blue + Yellow', c1: '#002185', c2: '#fcd300' },
{ id: 'navy-white', label: 'Navy + White', c1: '#0d1b44', c2: '#ffffff' },
{ id: 'crimson-yellow', label: 'Crimson + Yellow', c1: '#80022e', c2: '#fcd300' },
];
const PREVIEW_STEPS = 14;
/**
* Computes a CSS linear-gradient string directly from Ink Studio's physical Kubelka-Munk
* pigment engine so the UI swatch matches canvas blending 1:1.
*/
export function buildPigmentPreviewGradient(
colorA: string,
colorB: string,
enabled: boolean,
strength: number = 1.0
): string {
const rgbA = parseColorToRgb(colorA);
const rgbB = parseColorToRgb(colorB);
const effStrength = enabled ? Math.max(0, Math.min(1, strength)) : 0;
const stops: string[] = [];
for (let i = 0; i <= PREVIEW_STEPS; i++) {
const t = i / PREVIEW_STEPS;
const mixed = mixPigmentsWithStrength(rgbA, rgbB, t, effStrength);
const hex = rgbToHex(mixed.r, mixed.g, mixed.b);
stops.push(`${hex} ${Math.round(t * 100)}%`);
}
return `linear-gradient(90deg, ${stops.join(', ')})`;
}
export const PigmentBlendControl: React.FC<PigmentBlendControlProps> = React.memo(
({
enabled,
onEnabledChange,
strength = 1.0,
onStrengthChange,
primaryColor,
secondaryColor,
label = 'Color Blending',
strengthLabel = 'Pigment Strength',
variant = 'compact',
showPreview = true,
}) => {
const { t } = useI18n();
const [pairIndex, setPairIndex] = useState<number>(0);
const pairs = useMemo<SwatchPair[]>(() => {
const list: SwatchPair[] = [...DEFAULT_PAIRS];
if (
primaryColor &&
secondaryColor &&
primaryColor.toLowerCase() !== secondaryColor.toLowerCase()
) {
list.push({
id: 'fg-bg',
label: 'FG + BG',
c1: primaryColor,
c2: secondaryColor,
});
}
return list;
}, [primaryColor, secondaryColor]);
const activePair = pairs[pairIndex % pairs.length] ?? DEFAULT_PAIRS[0];
const clampedStrength = Math.max(0, Math.min(1, strength));
const strengthPct = Math.round(clampedStrength * 100);
const previewGradient = useMemo(
() =>
showPreview
? buildPigmentPreviewGradient(activePair.c1, activePair.c2, enabled, clampedStrength)
: '',
[activePair.c1, activePair.c2, enabled, clampedStrength, showPreview]
);
const isStudio = variant === 'studio';
const translatedLabel = t(label);
const translatedStrengthLabel = t(strengthLabel);
const translatedPairLabel = t(activePair.label);
return (
<div className={isStudio ? 'space-y-2.5' : 'space-y-1.5'}>
{/* Header row */}
<div className="flex items-center justify-between gap-1.5">
<span
className={
isStudio
? 'text-[11px] font-medium text-ui-primary'
: 'text-[11px] text-ui-muted truncate'
}
>
{translatedLabel}
</span>
<span
className={`px-1.5 py-[1px] rounded-full text-[9px] font-medium tracking-wide border transition-colors ${
enabled
? 'bg-ui-success/15 text-ui-success border-ui-success/30'
: 'bg-ui-control text-ui-muted border-ui-subtle'
}`}
>
{enabled ? t('Pigment') : 'RGB'}
</span>
</div>
{/* Regular vs Pigment segmented switch */}
<div
role="radiogroup"
aria-label={translatedLabel}
className="grid grid-cols-2 gap-0.5 rounded-ui-sm p-0.5 border bg-ui-input border-ui-subtle"
>
<button
type="button"
role="radio"
aria-checked={!enabled}
onClick={() => onEnabledChange(false)}
className={`flex items-center justify-center gap-1.5 rounded-ui-xs transition-colors cursor-pointer border ${
isStudio ? 'py-1.5 text-[11px]' : 'py-1 text-[10px]'
} ${
!enabled
? 'bg-ui-control-active text-ui-primary font-medium border-ui-default'
: 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent'
}`}
>
<span
className="w-2 h-2 rounded-full shrink-0 border border-ui-default"
style={{
background: 'linear-gradient(135deg, #3b82f6 0%, #9ca3af 50%, #facc15 100%)',
opacity: !enabled ? 0.9 : 0.45,
}}
/>
<span>{t('Regular')}</span>
</button>
<button
type="button"
role="radio"
aria-checked={enabled}
onClick={() => onEnabledChange(true)}
className={`flex items-center justify-center gap-1.5 rounded-ui-xs transition-colors cursor-pointer border ${
isStudio ? 'py-1.5 text-[11px]' : 'py-1 text-[10px]'
} ${
enabled
? 'bg-ui-control-active text-ui-primary font-medium border-ui-success/40'
: 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent'
}`}
>
<span
className="w-2 h-2 rounded-full shrink-0 border border-ui-default"
style={{
background: 'linear-gradient(135deg, #0084ff 0%, #34c759 50%, #fff000 100%)',
opacity: enabled ? 1 : 0.55,
}}
/>
<span>{t('Pigment')}</span>
</button>
</div>
{/* Live engine-rendered Regular vs Pigment swatch preview */}
{showPreview && (
<button
type="button"
onClick={() => setPairIndex((i) => (i + 1) % pairs.length)}
title={
enabled
? t('{pair} (Pigment {pct}%) — Click to cycle preview colors', {
pair: translatedPairLabel,
pct: strengthPct,
})
: t('{pair} (Regular RGB) — Click to cycle preview colors', {
pair: translatedPairLabel,
})
}
className="group relative w-full rounded-ui-sm overflow-hidden border border-ui-default hover:border-ui-strong transition-colors cursor-pointer block focus:outline-none focus:ring-1 focus:ring-ui-focus"
style={{ height: isStudio ? 22 : 17 }}
>
<div
className="w-full h-full transition-[background] duration-200"
style={{ background: previewGradient }}
/>
<div className="pointer-events-none absolute inset-x-1.5 inset-y-0 flex items-center justify-between">
<span className="text-[9px] font-medium text-white drop-shadow-[0_1px_2px_rgba(0,0,0,0.85)] opacity-85 group-hover:opacity-100 transition-opacity">
{translatedPairLabel}
</span>
<span className="text-[8.5px] font-ui-mono uppercase tracking-wider text-white/95 bg-black/50 px-1 rounded-ui-xs opacity-90 group-hover:opacity-100 transition-opacity">
{enabled ? t('Pigment') : t('Regular')}
</span>
</div>
</button>
)}
{/* Pigment Strength slider (only shown when Pigment mode is active & callback provided) */}
{enabled && onStrengthChange && (
<div className="pt-0.5">
<ScrubSlider
label={translatedStrengthLabel}
value={strengthPct}
min={0}
max={100}
step={1}
defaultValue={100}
unit="%"
size={isStudio ? 'sm' : 'xs'}
onChange={(v) => onStrengthChange(v / 100)}
/>
</div>
)}
</div>
);
}
);