randomColor
A utility function to generate a random color in different formats (hex, rgb, or rgba).> randomColor(type: "hex" | "rgb" | "rgba" = "hex"): string
- type ("hex" | "rgb" | "rgba", optional): Specifies the format of the color to generate. Defaults to `"hex"`.
- Returns: A string representing the random color in the specified format:
- `"hex"`: A hexadecimal color string (e.g., `"#ff6347"`).
- `"rgb"`: A color string in RGB format (e.g., `"rgb(255, 99, 71)"`).
- `"rgba"`: A color string in RGBA format, including an alpha transparency value (e.g., `"rgba(255, 99, 71, 0.80)"`).
Example
import { randomColor } from "@explita/daily-toolset";
// Generate a random color in hex format
const hexColor = randomColor("hex");
console.log(hexColor); // Output: e.g., "#3e2f1b"
// Generate a random color in RGB format
const rgbColor = randomColor("rgb");
console.log(rgbColor); // Output: e.g., "rgb(62, 47, 27)"
// Generate a random color in RGBA format
const rgbaColor = randomColor("rgba");
console.log(rgbaColor); // Output: e.g., "rgba(62, 47, 27, 0.56)"
// Generate a random color using the default format (hex)
const defaultColor = randomColor();
console.log(defaultColor); // Output: e.g., "#5c4a7f"
Use Cases
- Generating random colors for UI elements, such as backgrounds or borders.
- Creating dynamic and visually appealing themes or color palettes.
- Testing color compatibility or randomness in design systems.