javascript get black or white text base on color

Solutions on MaxInterview for javascript get black or white text base on color by the best coders in the world

showing results for - "javascript get black or white text base on color"
Nancy
06 Mar 2019
1function pickTextColorBasedOnBgColorAdvanced(bgColor, lightColor, darkColor) {
2  var color = (bgColor.charAt(0) === '#') ? bgColor.substring(1, 7) : bgColor;
3  var r = parseInt(color.substring(0, 2), 16); // hexToR
4  var g = parseInt(color.substring(2, 4), 16); // hexToG
5  var b = parseInt(color.substring(4, 6), 16); // hexToB
6  var uicolors = [r / 255, g / 255, b / 255];
7  var c = uicolors.map((col) => {
8    if (col <= 0.03928) {
9      return col / 12.92;
10    }
11    return Math.pow((col + 0.055) / 1.055, 2.4);
12  });
13  var L = (0.2126 * c[0]) + (0.7152 * c[1]) + (0.0722 * c[2]);
14  return (L > 0.179) ? darkColor : lightColor;
15}
16