color.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * 判断是否 十六进制颜色值.
  3. * 输入形式可为 #fff000 #f00
  4. *
  5. * @param String color 十六进制颜色值
  6. * @return Boolean
  7. */
  8. export function 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 function 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 function hexToRGB(hex: string) {
  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 'RGB(' + sColorChange.join(',') + ')';
  46. }
  47. return sHex;
  48. }
  49. export function colorIsDark(color: string) {
  50. if (!isHexColor(color)) return;
  51. const [r, g, b] = hexToRGB(color)
  52. .replace(/(?:\(|\)|rgb|RGB)*/g, '')
  53. .split(',')
  54. .map((item) => Number(item));
  55. return r * 0.299 + g * 0.578 + b * 0.114 < 192;
  56. }
  57. /**
  58. * Darkens a HEX color given the passed percentage
  59. * @param {string} color The color to process
  60. * @param {number} amount The amount to change the color by
  61. * @returns {string} The HEX representation of the processed color
  62. */
  63. export function darken(color: string, amount: number) {
  64. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
  65. amount = Math.trunc((255 * amount) / 100);
  66. return `#${subtractLight(color.substring(0, 2), amount)}${subtractLight(color.substring(2, 4), amount)}${subtractLight(
  67. color.substring(4, 6),
  68. amount
  69. )}`;
  70. }
  71. /**
  72. * Lightens a 6 char HEX color according to the passed percentage
  73. * @param {string} color The color to change
  74. * @param {number} amount The amount to change the color by
  75. * @returns {string} The processed color represented as HEX
  76. */
  77. export function lighten(color: string, amount: number) {
  78. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
  79. amount = Math.trunc((255 * amount) / 100);
  80. return `#${addLight(color.substring(0, 2), amount)}${addLight(color.substring(2, 4), amount)}${addLight(color.substring(4, 6), amount)}`;
  81. }
  82. /* Suma el porcentaje indicado a un color (RR, GG o BB) hexadecimal para aclararlo */
  83. /**
  84. * Sums the passed percentage to the R, G or B of a HEX color
  85. * @param {string} color The color to change
  86. * @param {number} amount The amount to change the color by
  87. * @returns {string} The processed part of the color
  88. */
  89. function addLight(color: string, amount: number) {
  90. const cc = parseInt(color, 16) + amount;
  91. const c = cc > 255 ? 255 : cc;
  92. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  93. }
  94. /**
  95. * Calculates luminance of an rgb color
  96. * @param {number} r red
  97. * @param {number} g green
  98. * @param {number} b blue
  99. */
  100. function luminanace(r: number, g: number, b: number) {
  101. const a = [r, g, b].map((v) => {
  102. v /= 255;
  103. return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
  104. });
  105. return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
  106. }
  107. /**
  108. * Calculates contrast between two rgb colors
  109. * @param {string} rgb1 rgb color 1
  110. * @param {string} rgb2 rgb color 2
  111. */
  112. function contrast(rgb1: string[], rgb2: number[]) {
  113. return (luminanace(~~rgb1[0], ~~rgb1[1], ~~rgb1[2]) + 0.05) / (luminanace(rgb2[0], rgb2[1], rgb2[2]) + 0.05);
  114. }
  115. /**
  116. * Determines what the best text color is (black or white) based con the contrast with the background
  117. * @param hexColor - Last selected color by the user
  118. */
  119. export function calculateBestTextColor(hexColor: string) {
  120. const rgbColor = hexToRGB(hexColor.substring(1));
  121. const contrastWithBlack = contrast(rgbColor.split(','), [0, 0, 0]);
  122. return contrastWithBlack >= 12 ? '#000000' : '#FFFFFF';
  123. }
  124. /**
  125. * Subtracts the indicated percentage to the R, G or B of a HEX color
  126. * @param {string} color The color to change
  127. * @param {number} amount The amount to change the color by
  128. * @returns {string} The processed part of the color
  129. */
  130. function subtractLight(color: string, amount: number) {
  131. const cc = parseInt(color, 16) - amount;
  132. const c = cc < 0 ? 0 : cc;
  133. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  134. }