-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumbers.ts
More file actions
36 lines (31 loc) · 867 Bytes
/
numbers.ts
File metadata and controls
36 lines (31 loc) · 867 Bytes
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
export function clamp(value: number, min = 0, max = 1): number {
if (value < min) return min
if (value > max) return max
return value
}
export function lerp(from: number, to: number, t: number) {
t = clamp(t, 0, 1)
return (1 - t) * from + t * to
}
export function inverseLerp(v: number, minValue: number, maxValue: number) {
return (v - minValue) / (maxValue - minValue)
}
export function remap(
v: number,
inMin: number,
inMax: number,
outMin: number,
outMax: number
) {
const t = inverseLerp(v, inMin, inMax)
return lerp(outMin, outMax, t)
}
export const toRGB = (v: number) => {
if (v < 0) return [0, 0, 0]
if (v > 1677.215) return [255, 255, 255]
const m = Math.round(v * 1000)
const r = Math.floor(m / 65536)
const g = Math.floor(m / 256) - r * 256
const b = Math.floor(m) - r * 65536 - g * 256
return [r, g, b]
}