Skip to content

randomColor

The randomColor function generates a random color in the specified format: hexadecimal, RGB, or HSL, with optional alpha channel.

Syntax

typescript
randomColor(format?: "hex" | "rgb" | "hsl", alpha?: boolean): string;

Parameters

NameTypeDescription
formatstring(Optional) Output format: "hex", "rgb", or "hsl"
alphaboolean(Optional) If true, includes alpha channel

Returns

TypeDescription
stringRandom color in the specified format

Examples

typescript
randomColor();
// => "rgb(123, 45, 67)"

randomColor("hex");
// => "#a1b2c3"

randomColor("hsl", true);
// => "hsla(210, 50%, 60%, 0.7)"

Notes

  • Throws an error if the format is not valid.
  • Useful for generating dynamic colors in interfaces.

References

Released under the MIT License.