RGB to HSB

JavaScript, Math · Oct 22, 2020

Converts a RGB color tuple to HSB format.

  • Use the RGB to HSB conversion formula to convert to the appropriate format.
  • The range of all input parameters is [0, 255].
  • The range of the resulting values is H: [0, 360], S: [0, 100], B: [0, 100].
const RGBToHSB = (r, g, b) => {
  r /= 255;
  g /= 255;
  b /= 255;
  const v = Math.max(r, g, b),
    n = v - Math.min(r, g, b);
  const h =
    n === 0 ? 0 : n && v === r ? (g - b) / n : v === g ? 2 + (b - r) / n : 4 + (r - g) / n;
  return [60 * (h < 0 ? h + 6 : h), v && (n / v) * 100, v * 100];
};

RGBToHSB(252, 111, 48);
// [18.529411764705856, 80.95238095238095, 98.82352941176471]

More like this

  • Colors in JavaScript

    Working with color requires understanding of color formats and conversions. Luckily, this JavaScript snippet collection's got you covered.

    Collection · 13 snippets

  • HSB to RGB

    Converts a HSB color tuple to RGB format.

    JavaScript, Math · Sep 18, 2020

  • HSL to RGB

    Converts a HSL color tuple to RGB format.

    JavaScript, Math · Oct 4, 2020

  • RGB to HSL

    Converts a RGB color tuple to HSL format.

    JavaScript, Math · Oct 4, 2020