Skip to content
Huey
GitHub

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): number

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

Parse and truncate color value strings to their valid range.

truncColorValue(value: string, channel: Channel): number

Normalize a value from one scale to another.

normalize(
value: number,
fromMin: number,
fromMax: number,
toMin: number,
toMax: number
): number

Round a value to the nearest step increment.

roundToStep(value: number, step: number): number
import { clamp, normalize, roundToStep } from 'huey/utils'
// Clamp a value
const clamped = clamp(300, 0, 255) // 255
// Normalize from one range to another
const normalized = normalize(50, 0, 100, 0, 1) // 0.5
// Round to nearest step
const rounded = roundToStep(23, 5) // 25