util.uts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * hex颜色转rgba
  3. */
  4. export const hexToRgba = (hex : string, alpha : number) : string => {
  5. // 去除 # 符号(如果有的话)
  6. hex = hex.replace('#', '');
  7. let hexArray = hex.split('');
  8. // 检查颜色值长度,如果不符合预期则返回默认值或者抛出错误
  9. if (hexArray.length != 3 && hexArray.length != 6) {
  10. // 返回默认值或者抛出错误,这里使用默认值为黑色
  11. return 'rgba(0,0,0,1)';
  12. // 或者抛出错误
  13. // throw new Error('Invalid hex color value');
  14. }
  15. let extendedHex : string[] = [];
  16. if (hex.length == 3) {
  17. for (let i = 0; i < hexArray.length; i++) {
  18. extendedHex.push(hexArray[i]);
  19. extendedHex.push(hexArray[i]);
  20. }
  21. hexArray = extendedHex;
  22. }
  23. hex = ''
  24. for (let h = 0; h < hexArray.length; h++) {
  25. hex += hexArray[h]
  26. }
  27. // // 拆分颜色值为 R、G、B
  28. const r = parseInt(hex.substring(0, 2), 16);
  29. const g = parseInt(hex.substring(2, 4), 16);
  30. const b = parseInt(hex.substring(4, 6), 16);
  31. // // 返回 rgba 值
  32. return `rgba(${r},${g},${b},${alpha})`;
  33. }
  34. export const easeInOutCubic = (t : number) : number => {
  35. return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
  36. }