Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

3.4 KB
Raw Blame
/**
 * Pure desktop environment & lightweight tile/color constants (zero HTMLCanvasElement or pixel grids).
 */

export const TILE_SIZE = 256;
export const TILE_PIXELS = TILE_SIZE * TILE_SIZE;
export const BYTES_1BIT = TILE_PIXELS / 8;
export const BYTES_8BIT = TILE_PIXELS;
export const BYTES_32BIT = TILE_PIXELS * 4;

export type TileFormat = 'monochrome-1bit' | 'grayscale-8bit' | 'color-32bit';

export function getTileByteSize(format: TileFormat): number {
  switch (format) {
    case 'monochrome-1bit':
      return BYTES_1BIT;
    case 'grayscale-8bit':
      return BYTES_8BIT;
    default:
      return BYTES_32BIT;
  }
}

export interface TileSnapshotEntry {
  tileX: number;
  tileY: number;
  format: TileFormat;
  data: Uint8Array | null;
  isSolidColor?: boolean;
  solidColorRGBA?: [number, number, number, number];
}

export function isTauriEnvironment(): boolean {
  return (
    typeof window !== 'undefined' &&
    Boolean(
      (window as unknown as { __TAURI_INTERNALS__?: unknown; __TAURI__?: unknown })
        .__TAURI_INTERNALS__ ||
        (window as unknown as { __TAURI__?: unknown }).__TAURI__
    )
  );
}

export function getTileKey(layerId: string, tileX: number, tileY: number, lod: number = 0): string {
  return `${layerId}:${lod}:${tileX},${tileY}`;
}

export function parseTileKey(key: string): {
  layerId: string;
  lod: number;
  tileX: number;
  tileY: number;
} | null {
  const lastColon = key.lastIndexOf(':');
  if (lastColon < 0) return null;
  const coords = key.slice(lastColon + 1);
  const comma = coords.indexOf(',');
  if (comma < 0) return null;
  const prefix = key.slice(0, lastColon);
  const prevColon = prefix.lastIndexOf(':');
  if (prevColon < 0) return null;
  const layerId = prefix.slice(0, prevColon);
  const lod = Number(prefix.slice(prevColon + 1));
  const tileX = Number(coords.slice(0, comma));
  const tileY = Number(coords.slice(comma + 1));
  if (!Number.isFinite(lod) || !Number.isFinite(tileX) || !Number.isFinite(tileY)) {
    return null;
  }
  return { layerId, lod, tileX, tileY };
}

export function pixelToTileCoord(px: number): number {
  return Math.floor(px / TILE_SIZE);
}

/**
 * Releases any small UI-only scratch canvas backing store cleanly.
 */
export function releaseTemporaryCanvas(canvas: HTMLCanvasElement | null | undefined): void {
  if (!canvas) return;
  canvas.width = 0;
  canvas.height = 0;
}

export function parseHexRgb(hex: string): [number, number, number] {
  if (typeof hex !== 'string') return [0, 0, 0];
  const cleaned = hex.trim().replace(/^#/, '');
  if (cleaned.length === 3) {
    const r = parseInt(cleaned[0] + cleaned[0], 16);
    const g = parseInt(cleaned[1] + cleaned[1], 16);
    const b = parseInt(cleaned[2] + cleaned[2], 16);
    if (!Number.isNaN(r) && !Number.isNaN(g) && !Number.isNaN(b)) {
      return [r, g, b];
    }
  } else if (cleaned.length >= 6) {
    const r = parseInt(cleaned.slice(0, 2), 16);
    const g = parseInt(cleaned.slice(2, 4), 16);
    const b = parseInt(cleaned.slice(4, 6), 16);
    if (!Number.isNaN(r) && !Number.isNaN(g) && !Number.isNaN(b)) {
      return [r, g, b];
    }
  }
  return [0, 0, 0];
}

export const parseHexToRgb = parseHexRgb;

export function parseColorToRgba(hex: string, opacity: number = 1): [number, number, number, number] {
  const [r, g, b] = parseHexRgb(hex);
  const a = Math.max(0, Math.min(255, Math.round(opacity * 255)));
  return [r, g, b, a];
}