added blackOrWhite color contrast function
All checks were successful
Build / Build NPM Project (push) Successful in 41s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 36s

This commit is contained in:
Zakary Timson 2024-12-26 10:40:39 -05:00
parent 446b1aa9db
commit 3bda688b1e
3 changed files with 14 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.23.5",
"version": "0.23.6",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

12
src/color.ts Normal file
View File

@ -0,0 +1,12 @@
/**
* Determine if either black or white provides more contrast to the provided color
* @param {string} background Color to compare against
* @return {"white" | "black"} Color with the most contrast
*/
export function blackOrWhite(background: string): 'white' | 'black' {
const exploded = background.match(background.length >= 6 ? /\w\w/g : /\w/g);
if(!exploded) return 'black';
const [r, g, b] = exploded.map(hex => parseInt(hex, 16));
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
return luminance > 0.5 ? 'black' : 'white';
}

View File

@ -2,6 +2,7 @@ export * from './arg-parser';
export * from './array';
export * from './aset';
export * from './cache';
export * from './color';
export * from './csv';
export * from './files';
export * from './emitter';