formatColor
Converts a color between hex, RGB and HSL. Takes a hex string (with or without #, 3 or 6 digits), a comma-separated RGB string, or an RGB object.
formatColor(input: string | RGB, to: "hex" | "rgb" | "hsl"): string | RGB | HSL | nullParameters
- inputstring | RGB
- "#FF8800", "f80", "255,136,0" or { r: 255, g: 136, b: 0 }.
- to"hex" | "rgb" | "hsl"
- The format to convert to.
Returns
Hex as an uppercase string with #, RGB as { r, g, b }, HSL as { h, s, l } with h from 0 to 360 and s and l from 0 to 100 (not rounded). null when the input or the format is invalid.
Example
import { formatColor } from "@dimitristerzz/dtutils";
formatColor("#f80", "rgb"); // { r: 255, g: 136, b: 0 }
formatColor("255,0,0", "hex"); // "#FF0000"
formatColor("#FFFFFF", "hsl"); // { h: 0, s: 0, l: 100 }
formatColor("300,0,0", "hex"); // null, 300 is out of range