1import Color from 'color'; 2 3export function grayRate(color: string | Color) { 4 let _color = typeof color === 'string' ? Color(color) : color; 5 6 return ( 7 ((0.299 * _color.red() + 8 0.587 * _color.green() + 9 0.114 * _color.blue()) * 10 2 - 11 255) / 12 255 13 ); 14} 15 16export function grayLevelCode(color: string | Color) { 17 const gray = grayRate(color); 18 console.log(gray); 19 if (gray < 96) { 20 return 'dark'; 21 } else if (gray > 160) { 22 return 'light'; 23 } else { 24 return 'mid'; 25 } 26} 27