Skip to content

rgbToHsb

Converts an RGB(A) color into HSB(A), the model design tools (Figma, Sketch, XD) show in their color pickers. Unlike HSL lightness, brightness 100 is the pure color, not white.

h/s/b are rounded to 1 decimal place to avoid floating-point noise.

Available since v3.1.5

import { rgbToHsb } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { rgbToHsb } from 'helpers4/color';
rgbToHsb(color: RgbColor): HsbColor
ParameterTypeDescription
colorRgbColorThe color to convert. `r`/`g`/`b` are expected in 0-255, `a` defaults to 1 (opaque) when omitted.

HsbColor — The equivalent HSB(A) color: h in 0-360, s/b in 0-100

Pure colors have a brightness of 100.

rgbToHsb({ r: 255, g: 0, b: 0 }) // => { h: 0, s: 100, b: 100, a: 1 }

White is saturation 0 and brightness 100 (HSL would say lightness 100).

rgbToHsb({ r: 255, g: 255, b: 255 }) // => { h: 0, s: 0, b: 100, a: 1 }

An HSB(A) color (also called HSV), the model of design tool color pickers.

interface HsbColor {
  a?: number;
  b: number;
  h: number;
  s: number;
}

An RGB(A) color with channels expressed as plain numbers, convenient for further conversion or manipulation.

interface RgbColor {
  a?: number;
  b: number;
  g: number;
  r: number;
}

View source on GitHub