All files / web/src/utils interpolate.ts

100% Statements 347/347
100% Branches 51/51
100% Functions 25/25
100% Lines 347/347

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 3481x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 80x 80x 1x 1x 1x 1x 1x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 11x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 6x 18x 18x 18x 6x 6x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 4x 4x 4x 1x 1x 4x 4x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x 1x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 6x 6x 5x 5x 11x 2x 2x 3x 3x 3x 3x  
/**
 * Interpolation utilities for smooth CSS property transitions
 *
 * Used by the celebration wind-down system to morph between
 * celebration and normal banner states over ~60 seconds.
 */
 
// =============================================================================
// Types
// =============================================================================
 
export interface RGB {
  r: number
  g: number
  b: number
}
 
export interface RGBA extends RGB {
  a: number
}
 
export interface GradientStop {
  color: RGBA
  position: number // percentage 0-100
}
 
export interface BoxShadow {
  x: number
  y: number
  blur: number
  spread: number
  color: RGBA
  inset?: boolean
}
 
// =============================================================================
// Basic Interpolation
// =============================================================================
 
/**
 * Linear interpolation between two numbers
 */
export function lerp(start: number, end: number, t: number): number {
  return start + (end - start) * t
}
 
/**
 * Clamp a value between min and max
 */
export function clamp(value: number, min: number, max: number): number {
  return Math.min(Math.max(value, min), max)
}
 
// =============================================================================
// Color Parsing & Interpolation
// =============================================================================
 
/**
 * Parse a hex color to RGB
 */
export function hexToRgb(hex: string): RGB {
  // Remove # if present
  const cleanHex = hex.replace('#', '')
 
  // Handle 3-char hex
  const fullHex =
    cleanHex.length === 3
      ? cleanHex
          .split('')
          .map((c) => c + c)
          .join('')
      : cleanHex
 
  const num = parseInt(fullHex, 16)
  return {
    r: (num >> 16) & 255,
    g: (num >> 8) & 255,
    b: num & 255,
  }
}
 
/**
 * Convert RGB to hex string
 */
export function rgbToHex(r: number, g: number, b: number): string {
  const toHex = (n: number) =>
    Math.round(clamp(n, 0, 255))
      .toString(16)
      .padStart(2, '0')
  return `#${toHex(r)}${toHex(g)}${toHex(b)}`
}
 
/**
 * Parse an rgba() or rgb() string to RGBA
 */
export function parseRgba(str: string): RGBA {
  // Handle rgba(r, g, b, a) or rgb(r, g, b)
  const match = str.match(
    /rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)/
  )
  if (match) {
    return {
      r: parseFloat(match[1]),
      g: parseFloat(match[2]),
      b: parseFloat(match[3]),
      a: match[4] !== undefined ? parseFloat(match[4]) : 1,
    }
  }
 
  // Handle hex
  if (str.startsWith('#')) {
    const rgb = hexToRgb(str)
    return { ...rgb, a: 1 }
  }
 
  // Default to black
  return { r: 0, g: 0, b: 0, a: 1 }
}
 
/**
 * Interpolate between two hex colors
 */
export function lerpColor(startHex: string, endHex: string, t: number): string {
  const start = hexToRgb(startHex)
  const end = hexToRgb(endHex)
  return rgbToHex(lerp(start.r, end.r, t), lerp(start.g, end.g, t), lerp(start.b, end.b, t))
}
 
/**
 * Interpolate between two RGBA colors
 */
export function lerpRgba(start: RGBA, end: RGBA, t: number): RGBA {
  return {
    r: lerp(start.r, end.r, t),
    g: lerp(start.g, end.g, t),
    b: lerp(start.b, end.b, t),
    a: lerp(start.a, end.a, t),
  }
}
 
/**
 * Convert RGBA to CSS string
 */
export function rgbaToString(rgba: RGBA): string {
  return `rgba(${Math.round(rgba.r)}, ${Math.round(rgba.g)}, ${Math.round(rgba.b)}, ${rgba.a.toFixed(3)})`
}
 
/**
 * Interpolate between two RGBA colors and return CSS string
 */
export function lerpRgbaString(start: RGBA, end: RGBA, t: number): string {
  return rgbaToString(lerpRgba(start, end, t))
}
 
// =============================================================================
// Gradient Interpolation
// =============================================================================
 
/**
 * Create a gradient stop
 */
export function gradientStop(
  r: number,
  g: number,
  b: number,
  a: number,
  position: number
): GradientStop {
  return { color: { r, g, b, a }, position }
}
 
/**
 * Interpolate between two gradient stop arrays
 * Both arrays must have the same number of stops
 */
export function lerpGradientStops(
  start: GradientStop[],
  end: GradientStop[],
  t: number
): GradientStop[] {
  if (start.length !== end.length) {
    throw new Error('Gradient stop arrays must have the same length')
  }
 
  return start.map((startStop, i) => {
    const endStop = end[i]
    return {
      color: lerpRgba(startStop.color, endStop.color, t),
      position: lerp(startStop.position, endStop.position, t),
    }
  })
}
 
/**
 * Convert gradient stops to CSS linear-gradient string
 */
export function gradientToCss(angle: number, stops: GradientStop[]): string {
  const stopsStr = stops.map((s) => `${rgbaToString(s.color)} ${s.position}%`).join(', ')
  return `linear-gradient(${angle}deg, ${stopsStr})`
}
 
/**
 * Interpolate between two linear gradients
 */
export function lerpGradient(
  startAngle: number,
  startStops: GradientStop[],
  endAngle: number,
  endStops: GradientStop[],
  t: number
): string {
  const angle = lerp(startAngle, endAngle, t)
  const stops = lerpGradientStops(startStops, endStops, t)
  return gradientToCss(angle, stops)
}
 
// =============================================================================
// Box Shadow Interpolation
// =============================================================================
 
/**
 * Create a box shadow
 */
export function boxShadow(
  x: number,
  y: number,
  blur: number,
  spread: number,
  r: number,
  g: number,
  b: number,
  a: number,
  inset = false
): BoxShadow {
  return { x, y, blur, spread, color: { r, g, b, a }, inset }
}
 
/**
 * Create a transparent (invisible) shadow for padding arrays
 */
export function transparentShadow(): BoxShadow {
  return boxShadow(0, 0, 0, 0, 0, 0, 0, 0)
}
 
/**
 * Pad shadow array to target length with transparent shadows
 */
function padShadows(shadows: BoxShadow[], targetLength: number): BoxShadow[] {
  const result = [...shadows]
  while (result.length < targetLength) {
    result.push(transparentShadow())
  }
  return result
}
 
/**
 * Interpolate between two box shadows
 */
export function lerpBoxShadowSingle(start: BoxShadow, end: BoxShadow, t: number): BoxShadow {
  return {
    x: lerp(start.x, end.x, t),
    y: lerp(start.y, end.y, t),
    blur: lerp(start.blur, end.blur, t),
    spread: lerp(start.spread, end.spread, t),
    color: lerpRgba(start.color, end.color, t),
    inset: t < 0.5 ? start.inset : end.inset,
  }
}
 
/**
 * Interpolate between two box shadow arrays
 */
export function lerpBoxShadows(start: BoxShadow[], end: BoxShadow[], t: number): BoxShadow[] {
  const maxLen = Math.max(start.length, end.length)
  const paddedStart = padShadows(start, maxLen)
  const paddedEnd = padShadows(end, maxLen)
 
  return paddedStart.map((s, i) => lerpBoxShadowSingle(s, paddedEnd[i], t))
}
 
/**
 * Convert box shadow to CSS string
 */
export function boxShadowToCss(shadow: BoxShadow): string {
  const { x, y, blur, spread, color, inset } = shadow
  const insetStr = inset ? 'inset ' : ''
  return `${insetStr}${x}px ${y}px ${blur}px ${spread}px ${rgbaToString(color)}`
}
 
/**
 * Convert box shadow array to CSS string
 */
export function boxShadowsToCss(shadows: BoxShadow[]): string {
  // Filter out completely transparent shadows
  const visible = shadows.filter((s) => s.color.a > 0.001 || s.blur > 0)
  if (visible.length === 0) return 'none'
  return visible.map(boxShadowToCss).join(', ')
}
 
/**
 * Interpolate between two box shadow arrays and return CSS string
 */
export function lerpBoxShadowString(start: BoxShadow[], end: BoxShadow[], t: number): string {
  return boxShadowsToCss(lerpBoxShadows(start, end, t))
}
 
// =============================================================================
// Timing Functions
// =============================================================================
 
/**
 * Quintic ease-out: starts extremely slow, accelerates toward end
 * Perfect for imperceptible transitions
 */
export function easeOutQuint(t: number): number {
  return 1 - (1 - t) ** 5
}
 
/**
 * Quartic ease-out: slightly faster than quintic
 */
export function easeOutQuart(t: number): number {
  return 1 - (1 - t) ** 4
}
 
/**
 * Calculate wind-down progress for celebration banner
 *
 * @param elapsedMs - milliseconds since celebration started
 * @returns progress from 0 (full celebration) to 1 (fully normal)
 */
export function windDownProgress(elapsedMs: number): number {
  const BURST_DURATION_MS = 5_000 // 5 seconds of full celebration
  const WIND_DOWN_DURATION_MS = 55_000 // 55 seconds to transition
 
  if (elapsedMs < BURST_DURATION_MS) {
    return 0 // Full celebration mode
  }
 
  const windDownElapsed = elapsedMs - BURST_DURATION_MS
  if (windDownElapsed >= WIND_DOWN_DURATION_MS) {
    return 1 // Fully transitioned to normal
  }
 
  const linearProgress = windDownElapsed / WIND_DOWN_DURATION_MS
  return easeOutQuint(linearProgress)
}