Math Utilities
Math Utilities
Section titled “Math Utilities”Helper functions for mathematical operations used in color calculations.
Clamp a value between minimum and maximum bounds.
clamp(value: number, min: number, max: number): numbergetChannelBounds
Section titled “getChannelBounds”Get the minimum and maximum values for a specific color channel.
type Channel = 'r' | 'g' | 'b' | 'h' | 's' | 'l' | 'a'getChannelBounds(channel: Channel): { min: number, max: number }Returns the valid range for each color channel:
- RGB: 0-255
- Hue: 0-360
- Saturation/Lightness: 0-100
- Alpha: 0-1
truncColorValue
Section titled “truncColorValue”Parse and truncate color value strings to their valid range.
truncColorValue(value: string, channel: Channel): numbernormalize
Section titled “normalize”Normalize a value from one scale to another.
normalize( value: number, fromMin: number, fromMax: number, toMin: number, toMax: number): numberroundToStep
Section titled “roundToStep”Round a value to the nearest step increment.
roundToStep(value: number, step: number): numberExamples
Section titled “Examples”import { clamp, normalize, roundToStep } from 'huey/utils'
// Clamp a valueconst clamped = clamp(300, 0, 255) // 255
// Normalize from one range to anotherconst normalized = normalize(50, 0, 100, 0, 1) // 0.5
// Round to nearest stepconst rounded = roundToStep(23, 5) // 25