colorGradient.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }
  54. if (/^(rgb|RGB)/.test(sColor)) {
  55. const arr = sColor.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',')
  56. return arr.map((val) => Number(val))
  57. }
  58. return sColor
  59. }
  60. // 将rgb表示方式转换为hex表示方式
  61. function rgbToHex(rgb) {
  62. const _this = rgb
  63. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  64. if (/^(rgb|RGB)/.test(_this)) {
  65. const aColor = _this.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',')
  66. let strHex = '#'
  67. for (let i = 0; i < aColor.length; i++) {
  68. let hex = Number(aColor[i]).toString(16)
  69. hex = String(hex).length == 1 ? `${0}${hex}` : hex // 保证每个rgb的值为2位
  70. if (hex === '0') {
  71. hex += hex
  72. }
  73. strHex += hex
  74. }
  75. if (strHex.length !== 7) {
  76. strHex = _this
  77. }
  78. return strHex
  79. }
  80. if (reg.test(_this)) {
  81. const aNum = _this.replace(/#/, '').split('')
  82. if (aNum.length === 6) {
  83. return _this
  84. }
  85. if (aNum.length === 3) {
  86. let numHex = '#'
  87. for (let i = 0; i < aNum.length; i += 1) {
  88. numHex += (aNum[i] + aNum[i])
  89. }
  90. return numHex
  91. }
  92. } else {
  93. return _this
  94. }
  95. }
  96. /**
  97. * JS颜色十六进制转换为rgb或rgba,返回的格式为 rgba(255,255,255,0.5)字符串
  98. * sHex为传入的十六进制的色值
  99. * alpha为rgba的透明度
  100. */
  101. function colorToRgba(color, alpha) {
  102. color = rgbToHex(color)
  103. // 十六进制颜色值的正则表达式
  104. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  105. /* 16进制颜色转为RGB格式 */
  106. let sColor = String(color).toLowerCase()
  107. if (sColor && reg.test(sColor)) {
  108. if (sColor.length === 4) {
  109. let sColorNew = '#'
  110. for (let i = 1; i < 4; i += 1) {
  111. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
  112. }
  113. sColor = sColorNew
  114. }
  115. // 处理六位的颜色值
  116. const sColorChange = []
  117. for (let i = 1; i < 7; i += 2) {
  118. sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`))
  119. }
  120. // return sColorChange.join(',')
  121. return `rgba(${sColorChange.join(',')},${alpha})`
  122. }
  123. return sColor
  124. }
  125. export default {
  126. colorGradient,
  127. hexToRgb,
  128. rgbToHex,
  129. colorToRgba
  130. }