colorGradient.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * 求两个颜色之间的渐变值
  3. * @param {string} startColor 开始的颜色
  4. * @param {string} endColor 结束的颜色
  5. * @param {number} step 颜色等分的份额
  6. * */
  7. function colorGradient(startColor = 'rgb(0, 0, 0)', endColor = 'rgb(255, 255, 255)', step = 10) {
  8. const startRGB = hexToRgb(startColor, false) // 转换为rgb数组模式
  9. const startR = startRGB[0]
  10. const startG = startRGB[1]
  11. const startB = startRGB[2]
  12. const endRGB = hexToRgb(endColor, false)
  13. const endR = endRGB[0]
  14. const endG = endRGB[1]
  15. const endB = endRGB[2]
  16. const sR = (endR - startR) / step // 总差值
  17. const sG = (endG - startG) / step
  18. const sB = (endB - startB) / step
  19. const colorArr = []
  20. for (let i = 0; i < step; i++) {
  21. // 计算每一步的hex值
  22. let hex = rgbToHex(`rgb(${Math.round((sR * i + startR))},${Math.round((sG * i + startG))},${Math.round((sB
  23. * i + startB))})`)
  24. // 确保第一个颜色值为startColor的值
  25. if (i === 0) hex = rgbToHex(startColor)
  26. // 确保最后一个颜色值为endColor的值
  27. if (i === step - 1) hex = rgbToHex(endColor)
  28. colorArr.push(hex)
  29. }
  30. return colorArr
  31. }
  32. // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
  33. function hexToRgb(sColor, str = true) {
  34. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  35. sColor = String(sColor).toLowerCase()
  36. if (sColor && reg.test(sColor)) {
  37. if (sColor.length === 4) {
  38. let sColorNew = '#'
  39. for (let i = 1; i < 4; i += 1) {
  40. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
  41. }
  42. sColor = sColorNew
  43. }
  44. // 处理六位的颜色值
  45. const sColorChange = []
  46. for (let i = 1; i < 7; i += 2) {
  47. sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`))
  48. }
  49. if (!str) {
  50. return sColorChange
  51. }
  52. return `rgb(${sColorChange[0]},${sColorChange[1]},${sColorChange[2]})`
  53. } if (/^(rgb|RGB)/.test(sColor)) {
  54. const arr = sColor.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',')
  55. return arr.map((val) => Number(val))
  56. }
  57. return sColor
  58. }
  59. // 将rgb表示方式转换为hex表示方式
  60. function rgbToHex(rgb) {
  61. const _this = rgb
  62. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  63. if (/^(rgb|RGB)/.test(_this)) {
  64. const aColor = _this.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',')
  65. let strHex = '#'
  66. for (let i = 0; i < aColor.length; i++) {
  67. let hex = Number(aColor[i]).toString(16)
  68. hex = String(hex).length == 1 ? `${0}${hex}` : hex // 保证每个rgb的值为2位
  69. if (hex === '0') {
  70. hex += hex
  71. }
  72. strHex += hex
  73. }
  74. if (strHex.length !== 7) {
  75. strHex = _this
  76. }
  77. return strHex
  78. } if (reg.test(_this)) {
  79. const aNum = _this.replace(/#/, '').split('')
  80. if (aNum.length === 6) {
  81. return _this
  82. } if (aNum.length === 3) {
  83. let numHex = '#'
  84. for (let i = 0; i < aNum.length; i += 1) {
  85. numHex += (aNum[i] + aNum[i])
  86. }
  87. return numHex
  88. }
  89. } else {
  90. return _this
  91. }
  92. }
  93. /**
  94. * JS颜色十六进制转换为rgb或rgba,返回的格式为 rgba(255,255,255,0.5)字符串
  95. * sHex为传入的十六进制的色值
  96. * alpha为rgba的透明度
  97. */
  98. function colorToRgba(color, alpha) {
  99. color = rgbToHex(color)
  100. // 十六进制颜色值的正则表达式
  101. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  102. /* 16进制颜色转为RGB格式 */
  103. let sColor = String(color).toLowerCase()
  104. if (sColor && reg.test(sColor)) {
  105. if (sColor.length === 4) {
  106. let sColorNew = '#'
  107. for (let i = 1; i < 4; i += 1) {
  108. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
  109. }
  110. sColor = sColorNew
  111. }
  112. // 处理六位的颜色值
  113. const sColorChange = []
  114. for (let i = 1; i < 7; i += 2) {
  115. sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`))
  116. }
  117. // return sColorChange.join(',')
  118. return `rgba(${sColorChange.join(',')},${alpha})`
  119. }
  120. return sColor
  121. }
  122. export default {
  123. colorGradient,
  124. hexToRgb,
  125. rgbToHex,
  126. colorToRgba
  127. }