云链智安app
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. export function hex2rgb(hex : string) : number[] {
  2. hex = hex.replace('#', '')
  3. if (hex.length == 3) {
  4. hex = hex.charAt(0).repeat(2) + hex.charAt(1).repeat(2) + hex.charAt(2).repeat(2)
  5. }
  6. let r = parseInt(hex.substring(0, 2), 16)
  7. let g = parseInt(hex.substring(2, 4), 16)
  8. let b = parseInt(hex.substring(4, 6), 16)
  9. if (isNaN(r) || isNaN(g) || isNaN(b)) {
  10. throw new Error('The param "hex" should be a hexadecimal digit.')
  11. }
  12. let a = 1;
  13. if (hex.length == 8) {
  14. a = parseInt(hex.substring(6, 8), 16) / 255
  15. }
  16. return [r, g, b, a]
  17. }
  18. const colorsMap = new Map<string, string>()
  19. export function toRgba(color : string, alpha : number) : string {
  20. const key = `${color}-${alpha}`
  21. if(colorsMap.has(key)) {
  22. return colorsMap.get(key)!
  23. }
  24. if (color.startsWith('#')) {
  25. const rgb = hex2rgb(color)
  26. const __color = `rgba(${rgb[0]},${rgb[1]}, ${rgb[2]}, ${alpha})`
  27. colorsMap.set(key, __color)
  28. return __color
  29. } else if (color.startsWith('rgb')) {
  30. const rgb = color.match(/\d+/g)?.map((item) : number => parseInt(item ?? '0'))
  31. if (Array.isArray(rgb) && rgb!.length > 2) {
  32. const __color = `rgba(${rgb[0]},${rgb[1]}, ${rgb[2]}, ${alpha})`
  33. colorsMap.set(key, __color)
  34. return __color
  35. } else {
  36. throw new Error('不支持的颜色:' + color)
  37. }
  38. } else {
  39. throw new Error('不支持的颜色:' + color)
  40. }
  41. }