All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.3 KiB

  1. import {fontColorContrast} from "./lib/FontColorContrast.js"
  2. const API = {
  3. /**
  4. * Turn hex rgba into rgba object
  5. * @param {String} hex 8 long hex value in string form, eg: "#123456ff"
  6. * @returns object of {r, g, b, a}
  7. */
  8. hexToRGBA(hex) {
  9. const hexArr = hex.slice(1).match(new RegExp(".{2}", "g"));
  10. const [r, g, b, a] = hexArr.map((hexStr) => {
  11. return parseInt(hexStr.repeat(2 / hexStr.length), 16);
  12. });
  13. const realAlpha = this._isRealNumber(a) ? a : 1;
  14. const rgba = [r, g, b, Math.round((realAlpha / 256 + Number.EPSILON) * 100) / 100];
  15. return {
  16. r: rgba[0] ?? 255,
  17. g: rgba[1] ?? 255,
  18. b: rgba[2] ?? 255,
  19. a: rgba[3] ?? 255,
  20. };
  21. },
  22. /**
  23. * Makes text white or black according to background color
  24. * @href https://wunnle.com/dynamic-text-color-based-on-background
  25. * @href https://stackoverflow.com/questions/54230440/how-to-change-text-color-based-on-rgb-and-rgba-background-color
  26. * @param {String} rgbaHex 8 long hex value in string form, eg: "#123456ff"
  27. * @param {number} threshold Contrast threshold to control the resulting font color, float values from 0 to 1. Default is 0.5.
  28. * @returns {( '#ffffff'|'#000000')} hex color
  29. */
  30. getTextColor(rgbaHex, threshold = 0.5) {
  31. // return game.modules.get("colorsettings").api.getTextColor(rgbaHex);
  32. const rgba = this.hexToRGBA(rgbaHex);
  33. // OLD METHOD
  34. /*
  35. //const realAlpha = this._isRealNumber(rgba.a) ? rgba.a : 1;
  36. const brightness = Math.round((rgba.r * 299 + rgba.g * 587 + rgba.b * 114) / 1000);
  37. // const realAlpha = this._isRealNumber(rgba.a) ? rgba.a : 1;
  38. if (this._isRealNumber(rgba.a) && rgba.a > 0.5) {
  39. return brightness > 125 ? "black" : "white";
  40. } else {
  41. //return 'black';
  42. return brightness > 125 ? "black" : "white";
  43. }
  44. */
  45. const hexTextColor = fontColorContrast(rgba.r, rgba.g, rgba.b, threshold);
  46. return hexTextColor;
  47. },
  48. /**
  49. * Convert a Array of rgba[r, g, b, a] in string format to a hex string
  50. * @param {String} rgba as string e.g. rgba('xxx','xxx','xxx','xxx')
  51. * @param {boolean} forceRemoveAlpha
  52. * @returns turns the hex string
  53. */
  54. RGBAToHexFromString(rgba, forceRemoveAlpha = false) {
  55. return (
  56. "#" +
  57. rgba
  58. .replace(/^rgba?\(|\s+|\)$/g, "") // Get's rgba / rgb string values
  59. .split(",") // splits them at ","
  60. .filter((string, index) => !forceRemoveAlpha || index !== 3)
  61. .map((string) => parseFloat(string)) // Converts them to numbers
  62. .map((number, index) => (index === 3 ? Math.round(number * 255) : number)) // Converts alpha to 255 number
  63. .map((number) => number.toString(16)) // Converts numbers to hex
  64. .map((string) => (string.length === 1 ? "0" + string : string)) // Adds 0 when length of one number is 1
  65. .join("")
  66. ); // Puts the array to together to a string
  67. },
  68. /**
  69. * Convert a Array of rgba[r, g, b, a] in to a hex string
  70. * @param {*} r
  71. * @param {*} g
  72. * @param {*} b
  73. * @param {*} a
  74. * @returns the hex string
  75. */
  76. RGBAToHex(r, g, b, a) {
  77. let r2 = r.toString(16);
  78. let g2 = g.toString(16);
  79. let b2 = b.toString(16);
  80. let a2 = Math.round(a * 255).toString(16);
  81. if (r2.length == 1) {
  82. r2 = "0" + r2;
  83. }
  84. if (g2.length == 1) {
  85. g2 = "0" + g2;
  86. }
  87. if (b2.length == 1) {
  88. b2 = "0" + b2;
  89. }
  90. if (a2.length == 1) {
  91. a2 = "0" + a2;
  92. }
  93. return "#" + r2 + g2 + b2 + a2;
  94. },
  95. /**
  96. * Turn hex rgba into rgba string
  97. * @href https://stackoverflow.com/questions/19799777/how-to-add-transparency-information-to-a-hex-color-code
  98. * @href https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
  99. * @param colorHex
  100. * @param alpha
  101. * @return rgba as string e.g. rgba('xxx','xxx','xxx','xxx')
  102. */
  103. hexToRGBAString(colorHex, alpha = 1) {
  104. let rgba = Color.from(colorHex);
  105. // return "rgba(" + rgb.r + ", " + rgb.g + ", " + rgb.b + ", " + alpha + ")";
  106. if (colorHex.length > 7) {
  107. rgba = this.hexToRGBA(colorHex);
  108. } else {
  109. const colorHex2 = `${colorHex}${Math.floor(alpha * 255)
  110. .toString(16)
  111. .padStart(2, "0")}`;
  112. rgba = this.hexToRGBA(colorHex2);
  113. // const c = Color.from(colorHex);
  114. // rgba = c.toRGBA();
  115. }
  116. const realAlpha = this._isRealNumber(rgba.a) ? rgba.a : alpha;
  117. return "rgba(" + rgba.r + ", " + rgba.g + ", " + rgba.b + ", " + realAlpha + ")";
  118. },
  119. /**
  120. * Calculate brightness value by RGB or HEX color.
  121. * @param color (String) The color value in RGB or HEX (for example: #000000 || #000 || rgb(0,0,0) || rgba(0,0,0,0))
  122. * @returns (Number) The brightness value (dark) 0 ... 255 (light)
  123. * @return {number} brigthness
  124. */
  125. brightnessByColor(colorHexOrRgb) {
  126. let color = "" + colorHexOrRgb;
  127. let isHEX = color.indexOf("#") == 0;
  128. let isRGB = color.indexOf("rgb") == 0;
  129. let r = 0;
  130. let g = 0;
  131. let b = 0;
  132. if (isHEX) {
  133. const rgba = this.hexToRGBA(color);
  134. r = rgba.r;
  135. g = rgba.g;
  136. b = rgba.b;
  137. }
  138. if (isRGB) {
  139. var m = color.match(/(\d+){3}/g);
  140. if (m) {
  141. r = m[0];
  142. g = m[1];
  143. b = m[2];
  144. }
  145. }
  146. if (typeof r != "undefined") {
  147. return (r * 299 + g * 587 + b * 114) / 1000;
  148. } else {
  149. return undefined;
  150. }
  151. },
  152. _isRealNumber(inNumber) {
  153. return !isNaN(inNumber) && typeof inNumber === "number" && isFinite(inNumber);
  154. }
  155. }
  156. export default API;