Skip to content

Instantly share code, notes, and snippets.

@boilemmashem
Last active December 22, 2023 11:11
Show Gist options
  • Save boilemmashem/77f50d4e66124393a882f74294ffc778 to your computer and use it in GitHub Desktop.
Save boilemmashem/77f50d4e66124393a882f74294ffc778 to your computer and use it in GitHub Desktop.
Black/White maximum contrast detection
/**
* Return black or white, whichever has higher contrast given the input color.
*
* @param {string} n - Input hexcolor without #
* @return {string} – white or black hex value with #
*
* @example
*
* getMaxContrast('FFFFFF') // #000000
*/
function getMaxContrast(hexcolor){
return (parseInt(hexcolor, 16) > 0xffffff/2) ? '#000000':'#FFFFFF';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment