colorize-foreground v1.0.2
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 --saveyarn add colorize-foregroundUsage
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
| function | parameter: type | returns |
|---|---|---|
| removeHash | color: string | string |
| expandHexadecimal | color: string | string |
| parseHexadecimal | color: string | RegExpExecArray |
| isValidHexadecimal | color: string | boolean |
| convertHexadecimalToHSL | color: string | number[] |
| colorizeForeground | color: string, threshold: number | object |
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
#ffffffValidation 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