colorUtils.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. let color = [
  2. 'red',
  3. 'purplered',
  4. 'purple',
  5. 'bluepurple',
  6. 'aquablue',
  7. 'blue',
  8. 'indigo',
  9. 'cyan',
  10. 'teal',
  11. 'green',
  12. 'yellowgreen',
  13. 'lime',
  14. 'yellow',
  15. 'orangeyellow',
  16. 'orange',
  17. 'orangered',
  18. 'brown',
  19. 'grey',
  20. 'gray'
  21. ]
  22. // 酷炫颜色的数量
  23. const COOL_BG_COLOR_COUNT = 16
  24. /**
  25. * 获取速立保配色颜色列表
  26. */
  27. function getTuniaoColorList() {
  28. return color
  29. }
  30. /**
  31. * 获取指定类型的随机颜色对应的类
  32. * @param {String} type 颜色类型
  33. */
  34. function getRandomColorClass(type = 'bg') {
  35. const index = Math.floor(Math.random() * color.length)
  36. const colorValue = color[index]
  37. return 'tn-' + type + '-' + colorValue
  38. }
  39. /**
  40. * 随机获取酷炫背景对应的类
  41. */
  42. function getRandomCoolBgClass() {
  43. const index = (Math.random() * COOL_BG_COLOR_COUNT) + 1
  44. return 'tn-cool-bg-color-' + Math.floor(index)
  45. }
  46. /**
  47. * 根据传入的值获取内部背景颜色类
  48. *
  49. * @param {String} backgroundColor 背景颜色信息
  50. */
  51. function getBackgroundColorInternalClass(backgroundColor = '') {
  52. if (!backgroundColor) return ''
  53. if (['tn-bg', 'tn-dynamic-bg', 'tn-main-gradient', 'tn-cool-bg'].some(item => {
  54. return backgroundColor.includes(item)
  55. })) {
  56. return backgroundColor
  57. }
  58. return ''
  59. }
  60. /**
  61. * 根据传入的值获取背景颜色样式
  62. *
  63. * @param {String} backgroundColor 背景颜色信息
  64. */
  65. function getBackgroundColorStyle(backgroundColor = '') {
  66. if (!backgroundColor) return ''
  67. if (!backgroundColor.startsWith('tn-') || ['#', 'rgb', 'rgba'].some(item => {
  68. return backgroundColor.includes(item)
  69. })) {
  70. return backgroundColor
  71. }
  72. return ''
  73. }
  74. /**
  75. * 根据传入的值获取内部字体颜色类
  76. *
  77. * @param {String} fontColor 背景颜色信息
  78. */
  79. function getFontColorInternalClass(fontColor = '') {
  80. if (!fontColor) return ''
  81. if (['tn-color'].some(item => {
  82. return fontColor.includes(item)
  83. })) {
  84. return fontColor
  85. }
  86. return ''
  87. }
  88. /**
  89. * 根据传入的值获取字体颜色样式
  90. *
  91. * @param {String} fontColor 背景颜色信息
  92. */
  93. function getFontColorStyle(fontColor = '') {
  94. if (!fontColor) return ''
  95. if (!fontColor.startsWith('tn-') || ['#', 'rgb', 'rgba'].some(item => {
  96. return fontColor.includes(item)
  97. })) {
  98. return fontColor
  99. }
  100. return ''
  101. }
  102. /**
  103. * 求两个颜色之间的渐变值
  104. *
  105. * @param {String} startColor 开始颜色
  106. * @param {String} endColor 结束颜色
  107. * @param {Number} step 颜色等分的份额
  108. */
  109. function colorGradient(startColor = 'rgb(0, 0, 0)', endColor='rgb(255, 255, 255)', step = 10) {
  110. let startRGB = hexToRGB(startColor, false)
  111. let startR = startRGB[0]
  112. let startG = startRGB[1]
  113. let startB = startRGB[2]
  114. let endRGB = hexToRGB(endColor, false)
  115. let endR = endRGB[0]
  116. let endG = endRGB[1]
  117. let endB = endRGB[2]
  118. // 求差值
  119. let R = (endR - startR) / step
  120. let G = (endG - startG) / step
  121. let B = (endB - startB) / step
  122. let colorArr = []
  123. for (let i = 0; i < step; i++) {
  124. // 计算每一步的hex值
  125. let hex = rgbToHex(`rgb(${Math.round(R * i + startR)}, ${Math.round(G * i + startG)}, ${Math.round(B * i + startB)})`)
  126. colorArr.push(hex)
  127. }
  128. return colorArr
  129. }
  130. /**
  131. * 将hex的颜色表示方式转换为rgb表示方式
  132. *
  133. * @param {String} color 颜色
  134. * @param {Boolean} str 是否返回字符串
  135. * @return {Array|String} rgb的值
  136. */
  137. function hexToRGB(color, str = true) {
  138. let reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
  139. color = color.toLowerCase()
  140. if (color && reg.test(color)) {
  141. // #000 => #000000
  142. if (color.length === 4) {
  143. let colorNew = '#'
  144. for (let i = 1; i < 4; i++) {
  145. colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1))
  146. }
  147. color = colorNew
  148. }
  149. // 处理六位的颜色值
  150. let colorChange = []
  151. for (let i = 1; i < 7; i += 2) {
  152. colorChange.push(parseInt("0x" + color.slice(i, i + 2)))
  153. }
  154. if (!str) {
  155. return colorChange
  156. } else {
  157. return `rgb(${colorChange[0]}, ${colorChange[1]}, ${colorChange[2]})`
  158. }
  159. } else if (/^(rgb|RGB)/.test(color)) {
  160. let arr = color.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(',')
  161. return arr.map(item => Number(item))
  162. } else {
  163. return color
  164. }
  165. }
  166. /**
  167. * 将rgb的颜色表示方式转换成hex表示方式
  168. *
  169. * @param {Object} rgb rgb颜色值
  170. */
  171. function rgbToHex(rgb) {
  172. let reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
  173. if (/^(rgb|RGB)/.test(rgb)) {
  174. let color = rgb.replace(/(?:\(|\)|rgb|GRB)*/g, "").split(',')
  175. let strHex = '#'
  176. for (let i = 0; i < color.length; i++) {
  177. let hex = Number(color[i]).toString(16)
  178. // 保证每个值否是两位数
  179. hex = String(hex).length === 1 ? 0 + '' + hex: hex
  180. if (hex === '0') {
  181. hex += hex
  182. }
  183. strHex += hex
  184. }
  185. if (strHex.length !== 7) {
  186. strHex = rgb
  187. }
  188. return strHex
  189. } else if (reg.test(rgb)) {
  190. let num = rgb.replace(/#/, '').split('')
  191. if (num.length === 6) {
  192. return rgb
  193. } else if (num.length === 3) {
  194. let numHex = '#'
  195. for (let i = 0; i < num.length; i++) {
  196. numHex += (num[i] + num[i])
  197. }
  198. return numHex
  199. }
  200. } else {
  201. return rgb
  202. }
  203. }
  204. /**
  205. * 将传入的颜色值转换为rgba字符串
  206. *
  207. * @param {String} color 颜色
  208. * @param {Number} alpha 透明度
  209. */
  210. function colorToRGBA(color, alpha = 0.3) {
  211. color = rgbToHex(color)
  212. // 十六进制颜色值的正则表达式
  213. let reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
  214. color = color.toLowerCase()
  215. if (color && reg.test(color)) {
  216. // #000 => #000000
  217. if (color.length === 4) {
  218. let colorNew = '#'
  219. for (let i = 1; i < 4; i++) {
  220. colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1))
  221. }
  222. color = colorNew
  223. }
  224. // 处理六位的颜色值
  225. let colorChange = []
  226. for (let i = 1; i < 7; i += 2) {
  227. colorChange.push(parseInt("0x" + color.slice(i, i + 2)))
  228. }
  229. return `rgba(${colorChange[0]}, ${colorChange[1]}, ${colorChange[2]}, ${alpha})`
  230. } else {
  231. return color
  232. }
  233. }
  234. export default {
  235. COOL_BG_COLOR_COUNT: COOL_BG_COLOR_COUNT,
  236. getTuniaoColorList,
  237. getRandomColorClass,
  238. getRandomCoolBgClass,
  239. getBackgroundColorInternalClass,
  240. getBackgroundColorStyle,
  241. getFontColorInternalClass,
  242. getFontColorStyle,
  243. colorGradient,
  244. hexToRGB,
  245. rgbToHex,
  246. colorToRGBA
  247. }