color.ts 655 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * 改变颜色亮度
  3. * @param color 颜色
  4. * @param amount 改变值
  5. * @returns
  6. */
  7. export function lightenColor(color: string, amount: number): string {
  8. let usePound = false;
  9. if (color[0] === '#') {
  10. color = color.slice(1);
  11. usePound = true;
  12. }
  13. const num = parseInt(color, 16);
  14. let r = (num >> 16) + amount;
  15. if (r > 255) r = 255;
  16. else if (r < 0) r = 0;
  17. let b = ((num >> 8) & 0x00ff) + amount;
  18. if (b > 255) b = 255;
  19. else if (b < 0) b = 0;
  20. let g = (num & 0x0000ff) + amount;
  21. if (g > 255) g = 255;
  22. else if (g < 0) g = 0;
  23. return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16).padStart(6, '0');
  24. }