formatHex
The formatHex() function Formats a hexadecimal color code string by adding a "#" prefix if it's missing and expanding short-form hex codes to long-form.
Parameters
| Parameter | Type | Description |
|---|---|---|
hex | String | The input hexadecimal color code to be formatted. |
alt | String | The default color code to use if hex is invalid. This parameter is optional and set to #000000 by default. |
Returns
String: The formatted color code
Throws
Error: Ifhexis not a string oraltis not a string
Example
const utils = require('utils-core.js');
const hex = "f00";
const formattedHex = utils.format.formatHex(hex);
console.log(formattedHex);
// output: "#ff0000"
const utils = require('utils-core.js');
const hex = "00ff00";
const formattedHex = utils.format.formatHex(hex, "#000000");
console.log(formattedHex);
// output: "#00ff00"