convert HSB to Hex function using js

export const hsbToHex = (
  hue: number,
  brightness: number,
  saturation: number,
): string => {
  if (
    hue < 0 ||
    hue > 360 ||
    saturation < 0 ||
    saturation > 1 ||
    brightness < 0 ||
    brightness > 1
  ) {
    throw new Error("HSB values must be within valid ranges.");
  }

  const chroma = brightness * saturation;
  const huePrime = hue / 60;
  const x = chroma * (1 - Math.abs((huePrime % 2) - 1));

  let r: number, g: number, b: number;
  if (huePrime >= 0 && huePrime < 1) {
    [r, g, b] = [chroma, x, 0];
  } else if (huePrime >= 1 && huePrime < 2) {
    [r, g, b] = [x, chroma, 0];
  } else if (huePrime >= 2 && huePrime < 3) {
    [r, g, b] = [0, chroma, x];
  } else if (huePrime >= 3 && huePrime < 4) {
    [r, g, b] = [0, x, chroma];
  } else if (huePrime >= 4 && huePrime < 5) {
    [r, g, b] = [x, 0, chroma];
  } else {
    [r, g, b] = [chroma, 0, x];
  }

  const m = brightness - chroma;
  const red = Math.round((r + m) * 255);
  const green = Math.round((g + m) * 255);
  const blue = Math.round((b + m) * 255);

  const hexColor = `#${red.toString(16).padStart(2, "0")}${green.toString(16).padStart(2, "0")}${blue.toString(16).padStart(2, "0")}`;
  return hexColor;
};

skype

Need Coding Help?

Connect Skype

AI

DATABASE

Jobs

Nextjs

Node

Npm

PHP

Prisma

React

Remix-run

Shopify App

Shopify theme

tailwindcss

Tech

VPS

Woocommerce

WordPress

How to Setup React and Tailwind CSS with Vite in a Project

Tailwind CSS is a popular CSS framework, and React ...

Shopify Cli

Shopify.app.toml scope or config update Shopify pa ...

essential Npm package for developers

Drag and drop image upload Use of the Npm package ...

Shopify App using Node, React

...

structure of react project

...

top