Skip to main content

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

ParameterTypeDescription
hexStringThe input hexadecimal color code to be formatted.
altStringThe 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: If hex is not a string or alt is 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"