Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

8.2 KB
Raw Blame
import { createMemo, createUniqueId, For, Show } from "solid-js";
import type { OrganizationBrandColorSwatch } from "~/utils/organization-branding";
import { BrandColorsDropdown } from "./BrandColorsDropdown";
import { hexToRgb, RgbInput } from "./color-utils";
import { useEditorContext } from "./context";
import type { RGBColor } from "./projectConfig";
import { EditorButton, Field, Section, Slider } from "./ui";

type GradientSourceFields = {
	type: "gradient";
	from: RGBColor;
	to: RGBColor;
	angle?: number;
	noise_intensity?: number | null;
	noise_scale?: number | null;
};

export const GRADIENT_PRESETS = [
	{ from: [15, 52, 67], to: [52, 232, 158] },
	{ from: [34, 193, 195], to: [253, 187, 45] },
	{ from: [29, 253, 251], to: [195, 29, 253] },
	{ from: [69, 104, 220], to: [176, 106, 179] },
	{ from: [106, 130, 251], to: [252, 92, 125] },
	{ from: [131, 58, 180], to: [253, 29, 29] },
	{ from: [249, 212, 35], to: [255, 78, 80] },
	{ from: [255, 94, 0], to: [255, 42, 104] },
	{ from: [255, 0, 150], to: [0, 204, 255] },
	{ from: [0, 242, 96], to: [5, 117, 230] },
	{ from: [238, 205, 163], to: [239, 98, 159] },
	{ from: [44, 62, 80], to: [52, 152, 219] },
	{ from: [168, 239, 255], to: [238, 205, 163] },
	{ from: [74, 0, 224], to: [143, 0, 255] },
	{ from: [252, 74, 26], to: [247, 183, 51] },
	{ from: [0, 255, 255], to: [255, 20, 147] },
	{ from: [255, 127, 0], to: [255, 255, 0] },
	{ from: [255, 0, 255], to: [0, 255, 0] },
] satisfies Array<{ from: RGBColor; to: RGBColor }>;

const DEFAULT_NOISE_SCALE = 3;
const NOISE_INTENSITY_MULTIPLIER = 0.25;

function randomColor(): RGBColor {
	return [
		Math.floor(Math.random() * 256),
		Math.floor(Math.random() * 256),
		Math.floor(Math.random() * 256),
	];
}

export function GradientEditor(props: {
	brandColorSwatches: OrganizationBrandColorSwatch[];
}) {
	const { project, setProject } = useEditorContext();
	const filterId = createUniqueId();

	const source = createMemo(() => {
		if (project.background.source.type !== "gradient") return null;
		return project.background.source as unknown as GradientSourceFields;
	});

	const angle = () => source()?.angle ?? 90;
	const noiseIntensity = () => source()?.noise_intensity ?? 0;
	const noiseScale = () => source()?.noise_scale ?? DEFAULT_NOISE_SCALE;

	const updateGradient = (updates: Partial<GradientSourceFields>) => {
		setProject("background", "source", updates as Record<string, unknown>);
	};

	const updateGradientColor = (key: "from" | "to", color: string) => {
		const rgb = hexToRgb(color);
		if (!rgb) return;
		const [r, g, b] = rgb;
		updateGradient({ [key]: [r, g, b] as RGBColor });
	};

	const gradientCSS = createMemo(() => {
		const s = source();
		if (!s) return "";
		const from = `rgb(${s.from.join(",")})`;
		const to = `rgb(${s.to.join(",")})`;
		return `linear-gradient(${angle()}deg, ${from}, ${to})`;
	});

	const noiseBaseFrequency = createMemo(() => {
		const scale = noiseScale();
		return (0.3 + ((100 - scale) / 100) * 1.2).toFixed(3);
	});

	const noiseOpacity = createMemo(
		() => (noiseIntensity() / 100) * NOISE_INTENSITY_MULTIPLIER,
	);

	return (
		<Show when={source()}>
			{(src) => (
				<div class="flex flex-col gap-3.5">
					<div class="relative overflow-hidden rounded-xl h-28 border border-ed-line">
						<div
							class="absolute inset-0"
							style={{
								background: gradientCSS(),
								transition: "background 200ms ease",
							}}
						/>
						<Show when={noiseIntensity() > 0}>
							<svg
								class="absolute inset-0 w-full h-full pointer-events-none"
								style={{
									opacity: noiseOpacity(),
									"mix-blend-mode": "overlay",
								}}
							>
								<filter id={`noise-${filterId}`}>
									<feTurbulence
										type="fractalNoise"
										baseFrequency={noiseBaseFrequency()}
										numOctaves="4"
										stitchTiles="stitch"
									/>
									<feColorMatrix type="saturate" values="0" />
								</filter>
								<rect
									width="100%"
									height="100%"
									filter={`url(#noise-${filterId})`}
								/>
							</svg>
						</Show>
					</div>

					<div class="flex gap-3 items-end">
						<div class="flex-1 min-w-0">
							<span class="text-[11px] text-ed-text-3 mb-1 block">From</span>
							<div class="flex flex-col gap-2">
								<RgbInput
									value={src().from}
									onChange={(from) => {
										updateGradient({ from });
									}}
								/>
								<BrandColorsDropdown
									swatches={props.brandColorSwatches}
									onSelect={(color) => updateGradientColor("from", color)}
								/>
							</div>
						</div>
						<div class="flex-1 min-w-0">
							<span class="text-[11px] text-ed-text-3 mb-1 block">To</span>
							<div class="flex flex-col gap-2">
								<RgbInput
									value={src().to}
									onChange={(to) => {
										updateGradient({ to });
									}}
								/>
								<BrandColorsDropdown
									swatches={props.brandColorSwatches}
									onSelect={(color) => updateGradientColor("to", color)}
								/>
							</div>
						</div>
					</div>

					<div class="w-full border-t border-ed-line" />

					<div class="flex flex-col">
						<Field inline name="Angle" value={`${Math.round(angle())}°`}>
							<Slider
								value={[angle()]}
								onChange={(v) => {
									updateGradient({ angle: v[0] });
								}}
								minValue={0}
								maxValue={360}
								step={1}
								formatTooltip={(value) => `${Math.round(value)}°`}
							/>
						</Field>

						<Field
							inline
							name="Noise"
							value={`${noiseIntensity().toFixed(1)}%`}
						>
							<Slider
								value={[noiseIntensity()]}
								onChange={(v) => {
									updateGradient({
										noise_intensity: v[0],
									});
								}}
								minValue={0}
								maxValue={100}
								step={1}
								formatTooltip="%"
							/>
						</Field>

						<Show when={noiseIntensity() > 0}>
							<Field
								inline
								name="Grain Scale"
								value={`${noiseScale().toFixed(1)}%`}
							>
								<Slider
									value={[noiseScale()]}
									onChange={(v) => {
										updateGradient({
											noise_scale: v[0],
										});
									}}
									minValue={1}
									maxValue={100}
									step={1}
									formatTooltip="%"
								/>
							</Field>
						</Show>
					</div>

					<div class="w-full border-t border-ed-line" />

					<Section
						name="Presets"
						action={
							<EditorButton
								size="sm"
								title="Randomize"
								leftIcon={
									<svg
										xmlns="http://www.w3.org/2000/svg"
										width="14"
										height="14"
										viewBox="0 0 24 24"
										fill="none"
										stroke="currentColor"
										stroke-width="2"
										stroke-linecap="round"
										stroke-linejoin="round"
									>
										<path d="M2 18h1.4c1.3 0 2.5-.6 3.3-1.7l6.1-8.6c.7-1.1 2-1.7 3.3-1.7H22" />
										<path d="m18 2 4 4-4 4" />
										<path d="M2 6h1.9c1.5 0 2.9.9 3.6 2.2" />
										<path d="M22 18h-5.9c-1.3 0-2.6-.7-3.3-1.8l-.5-.8" />
										<path d="m18 14 4 4-4 4" />
									</svg>
								}
								onClick={() => {
									updateGradient({
										from: randomColor(),
										to: randomColor(),
									});
								}}
							>
								Randomize
							</EditorButton>
						}
					>
						<div class="flex flex-wrap gap-2">
							<For each={GRADIENT_PRESETS}>
								{(gradient) => (
									<button
										type="button"
										class="rounded-lg transition-all duration-200 size-8 hover:opacity-80 hover:scale-105 ring-offset-2 ring-offset-ed-card"
										classList={{
											"ring-2 ring-ed-accent":
												src().from[0] === gradient.from[0] &&
												src().from[1] === gradient.from[1] &&
												src().from[2] === gradient.from[2] &&
												src().to[0] === gradient.to[0] &&
												src().to[1] === gradient.to[1] &&
												src().to[2] === gradient.to[2],
										}}
										style={{
											background: `linear-gradient(${angle()}deg, rgb(${gradient.from.join(
												",",
											)}), rgb(${gradient.to.join(",")}))`,
										}}
										onClick={() => {
											updateGradient({
												from: gradient.from as RGBColor,
												to: gradient.to as RGBColor,
											});
										}}
									/>
								)}
							</For>
						</div>
					</Section>
				</div>
			)}
		</Show>
	);
}