File size: 3,678 Bytes
f6f03fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Deterministic helpers — no Math.random anywhere in the render path so that
// preview, covers and MP4 export produce identical frames for the same time t.

export const clamp = (x: number, a = 0, b = 1) => (x < a ? a : x > b ? b : x)
export const lerp = (a: number, b: number, t: number) => a + (b - a) * t
export const invLerp = (a: number, b: number, x: number) => clamp((x - a) / (b - a))
export const smoothstep = (a: number, b: number, x: number) => {
  const t = invLerp(a, b, x)
  return t * t * (3 - 2 * t)
}
export const fract = (x: number) => x - Math.floor(x)
export const easeOutCubic = (t: number) => 1 - Math.pow(1 - clamp(t), 3)
export const easeInCubic = (t: number) => Math.pow(clamp(t), 3)
export const easeInOutCubic = (t: number) => {
  t = clamp(t)
  return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2
}
export const easeOutExpo = (t: number) => (t >= 1 ? 1 : 1 - Math.pow(2, -10 * clamp(t)))
export const easeInExpo = (t: number) => (t <= 0 ? 0 : Math.pow(2, 10 * clamp(t) - 10))
export const easeOutBack = (t: number) => {
  const c1 = 1.70158
  const c3 = c1 + 1
  t = clamp(t)
  return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2)
}

/** 0..1 hash of a number */
export function hash(n: number): number {
  const x = Math.sin(n * 127.1 + 311.7) * 43758.5453123
  return x - Math.floor(x)
}
export function hash2(a: number, b: number): number {
  const x = Math.sin(a * 127.1 + b * 311.7 + 74.7) * 43758.5453123
  return x - Math.floor(x)
}
export function hash3(a: number, b: number, c: number): number {
  const x = Math.sin(a * 127.1 + b * 311.7 + c * 269.5 + 17.3) * 43758.5453123
  return x - Math.floor(x)
}

/** smooth 1D value noise, -1..1 */
export function noise1(x: number, seed = 0): number {
  const i = Math.floor(x)
  const f = x - i
  const u = f * f * (3 - 2 * f)
  return lerp(hash(i + seed * 57.3), hash(i + 1 + seed * 57.3), u) * 2 - 1
}

/** fractal noise, -1..1 */
export function fbm(x: number, seed = 0): number {
  return noise1(x, seed) * 0.6 + noise1(x * 2.1, seed + 3) * 0.28 + noise1(x * 4.3, seed + 7) * 0.12
}

export function hexToRgb(hex: string): [number, number, number] {
  const h = hex.replace('#', '')
  const n = parseInt(h.length === 3 ? h.split('').map((c) => c + c).join('') : h, 16)
  return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
}

export function rgba(hex: string, a: number): string {
  const [r, g, b] = hexToRgb(hex)
  return `rgba(${r},${g},${b},${clamp(a)})`
}

export function mixHex(a: string, b: string, t: number): string {
  const A = hexToRgb(a)
  const B = hexToRgb(b)
  const r = Math.round(lerp(A[0], B[0], t))
  const g = Math.round(lerp(A[1], B[1], t))
  const bb = Math.round(lerp(A[2], B[2], t))
  return `rgb(${r},${g},${bb})`
}

/** Pulse that is 1 at the start of a period and decays. */
export const decay = (phase: number, k = 6) => Math.exp(-phase * k)

/** Flicker for neon: returns 0/1 noisy pattern while `amount` in 0..1 */
export function flicker(t: number, seed: number, rate = 24): number {
  return hash2(Math.floor(t * rate), seed) > 0.45 ? 1 : 0.15
}

export const PALETTE = {
  ink: '#05060a',
  night: '#0b1420',
  navy: '#0f1d2b',
  steel: '#1d3342',
  slate: '#2e363c',
  sodium: '#ffb35c',
  amber: '#ff8a3d',
  skin: '#cfa783',
  red: '#ff2d55',
  blood: '#9b1530',
  wine: '#3b0a10',
  cyan: '#3de0ff',
  teal: '#1fb5a8',
  violet: '#8a5cff',
  bone: '#f1e6d2',
}

export const FONT = {
  neon: '"MV Neon", "ZCOOL QingKe HuangYou", "PingFang SC", "Microsoft YaHei", sans-serif',
  black: '"MV Black", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif',
  latin: '"MV Latin", "Anton", Impact, "Arial Narrow", sans-serif',
}