color.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(
  67. color.substring(2, 4),
  68. amount
  69. )}${subtractLight(color.substring(4, 6), amount)}`;
  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(
  81. color.substring(2, 4),
  82. amount
  83. )}${addLight(color.substring(4, 6), amount)}`;
  84. }
  85. /* Suma el porcentaje indicado a un color (RR, GG o BB) hexadecimal para aclararlo */
  86. /**
  87. * Sums the passed percentage to the R, G or B of a HEX color
  88. * @param {string} color The color to change
  89. * @param {number} amount The amount to change the color by
  90. * @returns {string} The processed part of the color
  91. */
  92. function addLight(color: string, amount: number) {
  93. const cc = parseInt(color, 16) + amount;
  94. const c = cc > 255 ? 255 : cc;
  95. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  96. }
  97. /**
  98. * Calculates luminance of an rgb color
  99. * @param {number} r red
  100. * @param {number} g green
  101. * @param {number} b blue
  102. */
  103. function luminanace(r: number, g: number, b: number) {
  104. const a = [r, g, b].map((v) => {
  105. v /= 255;
  106. return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
  107. });
  108. return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
  109. }
  110. /**
  111. * Calculates contrast between two rgb colors
  112. * @param {string} rgb1 rgb color 1
  113. * @param {string} rgb2 rgb color 2
  114. */
  115. function contrast(rgb1: string[], rgb2: number[]) {
  116. return (
  117. (luminanace(~~rgb1[0], ~~rgb1[1], ~~rgb1[2]) + 0.05) /
  118. (luminanace(rgb2[0], rgb2[1], rgb2[2]) + 0.05)
  119. );
  120. }
  121. /**
  122. * Determines what the best text color is (black or white) based con the contrast with the background
  123. * @param hexColor - Last selected color by the user
  124. */
  125. export function calculateBestTextColor(hexColor: string) {
  126. const rgbColor = hexToRGB(hexColor.substring(1));
  127. const contrastWithBlack = contrast(rgbColor.split(','), [0, 0, 0]);
  128. return contrastWithBlack >= 12 ? '#000000' : '#FFFFFF';
  129. }
  130. /**
  131. * Subtracts the indicated percentage to the R, G or B of a HEX color
  132. * @param {string} color The color to change
  133. * @param {number} amount The amount to change the color by
  134. * @returns {string} The processed part of the color
  135. */
  136. function subtractLight(color: string, amount: number) {
  137. const cc = parseInt(color, 16) - amount;
  138. const c = cc < 0 ? 0 : cc;
  139. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  140. }