All files / util isLightColor.ts

90.9% Statements 10/11
100% Branches 1/1
100% Functions 1/1
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x                           8x 4x   4x 4x 4x   2x         4x     1x  
import { parseRGB } from './parseRGB';
 
/**
 * 判断颜色是否为浅色
 * @see https://zh.wikipedia.org/zh-cn/YUV
 * @method isLightColor
 * @param {String} color 16进制色值
 * @param {Number} [base=0.6] 深浅判断基准值,取值 [0, 1] 之间的小数
 * @returns {Boolean} 是否为浅色
 * @example
 * import { isLightColor } from '@spore-ui/tskit';
 * isLightColor('#ffffff'); // => true
 * isLightColor('#000000'); // => false
 */
export function isLightColor(color: string, base = 0.6): boolean {
  let colorIsLight = false;
  try {
    const arrRgb = parseRGB(color);
    const yuvY = (0.299 * arrRgb[0]) + (0.587 * arrRgb[1]) + (0.114 * arrRgb[2]);
    const darkness = (yuvY / 255);
    if (darkness > base) {
      colorIsLight = true;
    }
  } catch (err) {
    colorIsLight = false;
  }
  return colorIsLight;
}
 
export default isLightColor;