color.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { mapGetters } from 'vuex'
  2. const version = require('element-ui/package.json').version // element-ui version from node_modules
  3. const ORIGINAL_THEME = '#409EFF' // default color
  4. export default function() {
  5. return {
  6. data() {
  7. return {
  8. themeVal: ORIGINAL_THEME
  9. }
  10. },
  11. created() {
  12. this.themeVal = this.theme
  13. },
  14. watch: {
  15. themeVal(val, oldVal) {
  16. this.$store.commit('SET_THEME', val)
  17. this.updateTheme(val, oldVal)
  18. }
  19. },
  20. computed: {
  21. ...mapGetters(['theme'])
  22. },
  23. methods: {
  24. updateTheme(val, oldVal) {
  25. if (typeof val !== 'string') return
  26. const head = document.getElementsByTagName('head')[0]
  27. const themeCluster = this.getThemeCluster(val.replace('#', ''))
  28. const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
  29. const getHandler = (variable, id) => {
  30. return () => {
  31. const originalCluster = this.getThemeCluster(
  32. ORIGINAL_THEME.replace('#', '')
  33. )
  34. const newStyle = this.updateStyle(
  35. this[variable],
  36. originalCluster,
  37. themeCluster
  38. )
  39. let styleTag = document.getElementById(id)
  40. if (!styleTag) {
  41. styleTag = document.createElement('style')
  42. styleTag.setAttribute('id', id)
  43. head.appendChild(styleTag)
  44. }
  45. styleTag.innerText = newStyle
  46. }
  47. }
  48. const chalkHandler = getHandler('chalk', 'chalk-style')
  49. if (!this.chalk) {
  50. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  51. this.getCSSString(url, chalkHandler, 'chalk')
  52. } else {
  53. chalkHandler()
  54. }
  55. const link = [].slice.call(
  56. document.getElementsByTagName('head')[0].getElementsByTagName('link')
  57. )
  58. for (let i = 0; i < link.length; i++) {
  59. const style = link[i]
  60. if (style.href.includes('css')) {
  61. this.getCSSString(style.href, innerText => {
  62. const originalCluster = this.getThemeCluster(
  63. ORIGINAL_THEME.replace('#', '')
  64. )
  65. const newStyle = this.updateStyle(
  66. innerText,
  67. originalCluster,
  68. themeCluster
  69. )
  70. let styleTag = document.getElementById(i)
  71. if (!styleTag) {
  72. styleTag = document.createElement('style')
  73. styleTag.id = i
  74. styleTag.innerText = newStyle
  75. head.appendChild(styleTag)
  76. }
  77. })
  78. }
  79. }
  80. const styles = [].slice.call(document.querySelectorAll('style'))
  81. styles.forEach(style => {
  82. const {
  83. innerText
  84. } = style
  85. if (typeof innerText !== 'string') return
  86. style.innerText = this.updateStyle(
  87. innerText,
  88. originalCluster,
  89. themeCluster
  90. )
  91. })
  92. },
  93. updateStyle(style, oldCluster, newCluster) {
  94. let newStyle = style
  95. oldCluster.forEach((color, index) => {
  96. newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
  97. })
  98. return newStyle
  99. },
  100. getCSSString(url, callback, variable) {
  101. const xhr = new XMLHttpRequest()
  102. xhr.onreadystatechange = () => {
  103. if (xhr.readyState === 4 && xhr.status === 200) {
  104. if (variable) {
  105. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
  106. }
  107. callback(xhr.responseText)
  108. }
  109. }
  110. xhr.open('GET', url)
  111. xhr.send()
  112. },
  113. getThemeCluster(theme) {
  114. const tintColor = (color, tint) => {
  115. let red = parseInt(color.slice(0, 2), 16)
  116. let green = parseInt(color.slice(2, 4), 16)
  117. let blue = parseInt(color.slice(4, 6), 16)
  118. if (tint === 0) {
  119. // when primary color is in its rgb space
  120. return [red, green, blue].join(',')
  121. } else {
  122. red += Math.round(tint * (255 - red))
  123. green += Math.round(tint * (255 - green))
  124. blue += Math.round(tint * (255 - blue))
  125. red = red.toString(16)
  126. green = green.toString(16)
  127. blue = blue.toString(16)
  128. return `#${red}${green}${blue}`
  129. }
  130. }
  131. const shadeColor = (color, shade) => {
  132. let red = parseInt(color.slice(0, 2), 16)
  133. let green = parseInt(color.slice(2, 4), 16)
  134. let blue = parseInt(color.slice(4, 6), 16)
  135. red = Math.round((1 - shade) * red)
  136. green = Math.round((1 - shade) * green)
  137. blue = Math.round((1 - shade) * blue)
  138. red = red.toString(16)
  139. green = green.toString(16)
  140. blue = blue.toString(16)
  141. return `#${red}${green}${blue}`
  142. }
  143. const clusters = [theme]
  144. for (let i = 0; i <= 9; i++) {
  145. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
  146. }
  147. clusters.push(shadeColor(theme, 0.1))
  148. return clusters
  149. }
  150. }
  151. }
  152. }