Skip to content

Instantly share code, notes, and snippets.

View jagomf's full-sized avatar

Jago jagomf

View GitHub Profile
@jagomf
jagomf / foreground-color-selector.ts
Last active June 25, 2024 07:46
Select foreground light/dark color based on background color
/**
* Selects light or dark color for foreground (mainly text) based on background color
* @param bgColor Background color to evaluate
* @param lightColor Light foreground color (defaults to white)
* @param darkColor Dark foreground color (defaults to black)
* @returns Light or dark color based on background color
* @see https://stackoverflow.com/a/41491220
*/
function pickTextColorBasedOnBgColorAdvanced(bgColor: string, lightColor = '#FFFFFF', darkColor = '#000000'): string {
const color = (bgColor.startsWith('#')) ? bgColor.substring(1, 7) : bgColor;