added blackOrWhite color contrast function
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	"name": "@ztimson/utils",
 | 
						"name": "@ztimson/utils",
 | 
				
			||||||
	"version": "0.23.5",
 | 
						"version": "0.23.6",
 | 
				
			||||||
	"description": "Utility library",
 | 
						"description": "Utility library",
 | 
				
			||||||
	"author": "Zak Timson",
 | 
						"author": "Zak Timson",
 | 
				
			||||||
	"license": "MIT",
 | 
						"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 './array';
 | 
				
			||||||
export * from './aset';
 | 
					export * from './aset';
 | 
				
			||||||
export * from './cache';
 | 
					export * from './cache';
 | 
				
			||||||
 | 
					export * from './color';
 | 
				
			||||||
export * from './csv';
 | 
					export * from './csv';
 | 
				
			||||||
export * from './files';
 | 
					export * from './files';
 | 
				
			||||||
export * from './emitter';
 | 
					export * from './emitter';
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user