Files
SpotMicroESP32-Leika/app/src/lib/utilities/math-utilities.ts
T
Rune Harlyk a77eb0b1e0 🎨 Lint project
2025-10-11 10:54:07 +02:00

19 lines
688 B
TypeScript

export const toUint8 = (number: number, min: number, max: number) => {
number = Math.max(min, Math.min(max, number))
const scaled = ((number - min) / (max - min)) * 255
return Math.round(scaled) & 0xff
}
export const toInt8 = (number: number, min: number, max: number) => {
number = Math.max(min, Math.min(max, number))
const scaled = ((number - min) / (max - min)) * 255 - 128
return Math.max(-128, Math.min(127, Math.round(scaled))) | 0
}
export const fromInt8 = (int8: number, min: number, max: number) => {
int8 = Math.max(-128, Math.min(127, int8))
const scaled = (int8 + 128) / 255
const number = scaled * (max - min) + min
return number
}