color.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * 判断是否 十六进制颜色值.
  3. * 输入形式可为 #fff000 #f00
  4. *
  5. * @param String color 十六进制颜色值
  6. * @return Boolean
  7. */
  8. export const isHexColor = (color: string) => {
  9. const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-f]{6})$/
  10. return reg.test(color)
  11. }
  12. /**
  13. * RGB 颜色值转换为 十六进制颜色值.
  14. * r, g, 和 b 需要在 [0, 255] 范围内
  15. *
  16. * @return String 类似#ff00ff
  17. * @param r
  18. * @param g
  19. * @param b
  20. */
  21. export const rgbToHex = (r: number, g: number, b: number) => {
  22. // tslint:disable-next-line:no-bitwise
  23. const hex = ((r << 16) | (g << 8) | b).toString(16)
  24. return '#' + new Array(Math.abs(hex.length - 7)).join('0') + hex
  25. }
  26. /**
  27. * Transform a HEX color to its RGB representation
  28. * @param {string} hex The color to transform
  29. * @returns The RGB representation of the passed color
  30. */
  31. export const hexToRGB = (hex: string, opacity?: number) => {
  32. let sHex = hex.toLowerCase()
  33. if (isHexColor(hex)) {
  34. if (sHex.length === 4) {
  35. let sColorNew = '#'
  36. for (let i = 1; i < 4; i += 1) {
  37. sColorNew += sHex.slice(i, i + 1).concat(sHex.slice(i, i + 1))
  38. }
  39. sHex = sColorNew
  40. }
  41. const sColorChange: number[] = []
  42. for (let i = 1; i < 7; i += 2) {
  43. sColorChange.push(parseInt('0x' + sHex.slice(i, i + 2)))
  44. }
  45. return opacity
  46. ? 'RGBA(' + sColorChange.join(',') + ',' + opacity + ')'
  47. : 'RGB(' + sColorChange.join(',') + ')'
  48. }
  49. return sHex
  50. }
  51. export const colorIsDark = (color: string) => {
  52. if (!isHexColor(color)) return
  53. const [r, g, b] = hexToRGB(color)
  54. .replace(/(?:\(|\)|rgb|RGB)*/g, '')
  55. .split(',')
  56. .map((item) => Number(item))
  57. return r * 0.299 + g * 0.578 + b * 0.114 < 192
  58. }
  59. /**
  60. * Darkens a HEX color given the passed percentage
  61. * @param {string} color The color to process
  62. * @param {number} amount The amount to change the color by
  63. * @returns {string} The HEX representation of the processed color
  64. */
  65. export const darken = (color: string, amount: number) => {
  66. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color
  67. amount = Math.trunc((255 * amount) / 100)
  68. return `#${subtractLight(color.substring(0, 2), amount)}${subtractLight(
  69. color.substring(2, 4),
  70. amount
  71. )}${subtractLight(color.substring(4, 6), amount)}`
  72. }
  73. /**
  74. * Lightens a 6 char HEX color according to the passed percentage
  75. * @param {string} color The color to change
  76. * @param {number} amount The amount to change the color by
  77. * @returns {string} The processed color represented as HEX
  78. */
  79. export const lighten = (color: string, amount: number) => {
  80. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color
  81. amount = Math.trunc((255 * amount) / 100)
  82. return `#${addLight(color.substring(0, 2), amount)}${addLight(
  83. color.substring(2, 4),
  84. amount
  85. )}${addLight(color.substring(4, 6), amount)}`
  86. }
  87. /* Suma el porcentaje indicado a un color (RR, GG o BB) hexadecimal para aclararlo */
  88. /**
  89. * Sums the passed percentage to the R, G or B of a HEX color
  90. * @param {string} color The color to change
  91. * @param {number} amount The amount to change the color by
  92. * @returns {string} The processed part of the color
  93. */
  94. const addLight = (color: string, amount: number) => {
  95. const cc = parseInt(color, 16) + amount
  96. const c = cc > 255 ? 255 : cc
  97. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`
  98. }
  99. /**
  100. * Calculates luminance of an rgb color
  101. * @param {number} r red
  102. * @param {number} g green
  103. * @param {number} b blue
  104. */
  105. const luminanace = (r: number, g: number, b: number) => {
  106. const a = [r, g, b].map((v) => {
  107. v /= 255
  108. return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4)
  109. })
  110. return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722
  111. }
  112. /**
  113. * Calculates contrast between two rgb colors
  114. * @param {string} rgb1 rgb color 1
  115. * @param {string} rgb2 rgb color 2
  116. */
  117. const contrast = (rgb1: string[], rgb2: number[]) => {
  118. return (
  119. (luminanace(~~rgb1[0], ~~rgb1[1], ~~rgb1[2]) + 0.05) /
  120. (luminanace(rgb2[0], rgb2[1], rgb2[2]) + 0.05)
  121. )
  122. }
  123. /**
  124. * Determines what the best text color is (black or white) based con the contrast with the background
  125. * @param hexColor - Last selected color by the user
  126. */
  127. export const calculateBestTextColor = (hexColor: string) => {
  128. const rgbColor = hexToRGB(hexColor.substring(1))
  129. const contrastWithBlack = contrast(rgbColor.split(','), [0, 0, 0])
  130. return contrastWithBlack >= 12 ? '#000000' : '#FFFFFF'
  131. }
  132. /**
  133. * Subtracts the indicated percentage to the R, G or B of a HEX color
  134. * @param {string} color The color to change
  135. * @param {number} amount The amount to change the color by
  136. * @returns {string} The processed part of the color
  137. */
  138. const subtractLight = (color: string, amount: number) => {
  139. const cc = parseInt(color, 16) - amount
  140. const c = cc < 0 ? 0 : cc
  141. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`
  142. }
  143. // 预设颜色
  144. export const PREDEFINE_COLORS = [
  145. '#ff4500',
  146. '#ff8c00',
  147. '#ffd700',
  148. '#90ee90',
  149. '#00ced1',
  150. '#1e90ff',
  151. '#c71585',
  152. '#409EFF',
  153. '#909399',
  154. '#C0C4CC',
  155. '#b7390b',
  156. '#ff7800',
  157. '#fad400',
  158. '#5b8c5f',
  159. '#00babd',
  160. '#1f73c3',
  161. '#711f57'
  162. ]