Skip to content

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

4.2 KB
Raw Blame
/**
 * Pure deterministic math helpers for ScrubSlider and PrecisionNumberInput.
 * Zero DOM dependencies so mapping, clamping, step quantization, and formatting
 * stay fast, accurate, and directly testable.
 */

export function clampNumber(value: number, min: number, max: number): number {
  if (!Number.isFinite(value)) return min;
  if (min > max) return min;
  return value < min ? min : value > max ? max : value;
}

export function getStepDecimals(step: number): number {
  if (!Number.isFinite(step) || step <= 0) return 0;
  const s = step.toString();
  if (s.includes('e-')) {
    const [, exp] = s.split('e-');
    return Math.min(8, parseInt(exp, 10) || 0);
  }
  const dotIdx = s.indexOf('.');
  return dotIdx === -1 ? 0 : Math.min(8, s.length - dotIdx - 1);
}

export function roundToDecimals(value: number, decimals: number): number {
  if (!Number.isFinite(value)) return 0;
  if (decimals <= 0) return Math.round(value);
  const factor = 10 ** decimals;
  return Math.round(value * factor) / factor;
}

export function quantizeValue(
  raw: number,
  min: number,
  max: number,
  step: number
): number {
  const clamped = clampNumber(raw, min, max);
  if (!Number.isFinite(step) || step <= 0) return clamped;
  const decimals = getStepDecimals(step);
  const stepsFromMin = Math.round((clamped - min) / step);
  const snapped = min + stepsFromMin * step;
  return clampNumber(roundToDecimals(snapped, decimals), min, max);
}

/**
 * Converts a domain value in [min, max] to a normalized ratio in [0, 1].
 * Supports true log scaling when `logarithmic` is true and `min > 0`,
 * or smooth quadratic power mapping when `logarithmic` is true and `min <= 0`.
 */
export function valueToRatio(
  value: number,
  min: number,
  max: number,
  logarithmic = false
): number {
  if (max <= min) return 0;
  const clamped = clampNumber(value, min, max);
  if (logarithmic) {
    if (min > 0) {
      const minLog = Math.log(min);
      const maxLog = Math.log(max);
      if (maxLog === minLog) return 0;
      return clampNumber((Math.log(clamped) - minLog) / (maxLog - minLog), 0, 1);
    }
    const norm = (clamped - min) / (max - min);
    return clampNumber(Math.sqrt(Math.max(0, norm)), 0, 1);
  }
  return clampNumber((clamped - min) / (max - min), 0, 1);
}

/**
 * Converts a normalized ratio in [0, 1] back to a domain value in [min, max].
 */
export function ratioToValue(
  ratio: number,
  min: number,
  max: number,
  logarithmic = false
): number {
  if (max <= min) return min;
  const r = clampNumber(ratio, 0, 1);
  if (logarithmic) {
    if (min > 0) {
      const minLog = Math.log(min);
      const maxLog = Math.log(max);
      return clampNumber(Math.exp(minLog + r * (maxLog - minLog)), min, max);
    }
    return clampNumber(min + r * r * (max - min), min, max);
  }
  return clampNumber(min + r * (max - min), min, max);
}

/**
 * Formats a numeric value cleanly with deterministic decimal places derived from `step`,
 * avoiding jittery trailing floats during scrubbing.
 */
export function formatNumericValue(
  value: number,
  step: number,
  unit = ''
): string {
  const decimals = getStepDecimals(step);
  const safe = Number.isFinite(value) ? value : 0;
  const normalized = roundToDecimals(safe, decimals);
  // Avoid displaying "-0"
  const zeroSafe = Object.is(normalized, -0) ? 0 : normalized;
  const text = decimals > 0 ? zeroSafe.toFixed(decimals) : String(Math.round(zeroSafe));
  return unit ? `${text}${unit}` : text;
}

/**
 * Parses a user-entered numeric string (tolerating unit suffixes like "42px", "85%", "+12°").
 */
export function parseNumericInput(raw: string): number | null {
  const cleaned = raw.trim().replace(/,/g, '.');
  if (!cleaned) return null;
  const match = cleaned.match(/^[+-]?(?:\d+\.?\d*|\.\d+)/);
  if (!match) return null;
  const parsed = Number(match[0]);
  return Number.isFinite(parsed) ? parsed : null;
}

/**
 * Computes a sensible coarse PageUp/PageDown step (~10% of range, snapped to `step`).
 */
export function computePageStep(min: number, max: number, step: number): number {
  const span = Math.max(0, max - min);
  const raw = Math.max(step * 10, span * 0.1);
  if (step <= 0) return raw;
  const count = Math.max(1, Math.round(raw / step));
  return roundToDecimals(count * step, getStepDecimals(step));
}