1.0.2 • Published 5 years ago

colorize-foreground v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Colorize Foreground

Validate a hexadecimal value, Convert hexadecimal to HSL, determine if the colour needs a light or dark foreground.

Installation

npm i colorize-foreground --save
yarn add colorize-foreground

Usage

import { colorizeForeground } from "colorize-foreground";
// OR
const { colorizeForeground } = require("colorize-foreground");

// could be "fff" | "#fff" | "ffffff" | "#ffffff"
const backgroundColor = "#fff";

// value becomes: { color: "000000", type: "dark" }
const foregroundColor = colorizeForeground(backgroundColor);

Functions

functionparameter: typereturns
removeHashcolor: stringstring
expandHexadecimalcolor: stringstring
parseHexadecimalcolor: stringRegExpExecArray
isValidHexadecimalcolor: stringboolean
convertHexadecimalToHSLcolor: stringnumber[]
colorizeForegroundcolor: string, threshold: numberobject

Parameters Insight

The color parameter should be a valid hexidecimal code, either with or without the hash. All functions remove the hash immediately and any hexidecimal values returned will be without it. So be conscious of that.

The threshold parameter in the colorizeForeground function starts at 75. This is the "lightness" of the passed color (in HSL format). If the lightness of the color is greater than the threshold, a dark color suggestion will be made. If the lightness is equal to or less than the allowed threshold a light color suggestion will be returned instead.

Validations and Errors

When using any of the functions, the hexidecimal value is validated and stripped of the hash (#). The validation process accepts most accepted forms for a color hex code. This includes both shorthand and full length.

Validation is case-insensitive.

Validation Passing Formats

fff
#fff
ffffff
#ffffff

Validation of new-era hexidecimal is not yet supported or on the roadmap since it is very seldom used. If you'd like to see it though feel free to post an issue and I'll revisit this opinion there.

License

MIT