added blackOrWhite color contrast function
This commit is contained in:
parent
446b1aa9db
commit
3bda688b1e
@ -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
12
src/color.ts
Normal 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';
|
||||
}
|
@ -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';
|
||||
|
Loading…
Reference in New Issue
Block a user