utils-1e0cd9e8.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. 'use strict';
  7. function rgbToHex(color) {
  8. const { r, g, b } = color;
  9. return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b
  10. .toString(16)
  11. .padStart(2, "0")}`.toLowerCase();
  12. }
  13. const hexChar = /^[0-9A-F]$/i;
  14. const shortHandHex = /^#[0-9A-F]{3}$/i;
  15. const longhandHex = /^#[0-9A-F]{6}$/i;
  16. function isValidHex(hex) {
  17. return isShorthandHex(hex) || isLonghandHex(hex);
  18. }
  19. function isShorthandHex(hex) {
  20. return hex && hex.length === 4 && shortHandHex.test(hex);
  21. }
  22. function isLonghandHex(hex) {
  23. return hex && hex.length === 7 && longhandHex.test(hex);
  24. }
  25. function normalizeHex(hex) {
  26. hex = hex.toLowerCase();
  27. if (!hex.startsWith("#")) {
  28. hex = `#${hex}`;
  29. }
  30. if (isShorthandHex(hex)) {
  31. return rgbToHex(hexToRGB(hex));
  32. }
  33. return hex;
  34. }
  35. function hexToRGB(hex) {
  36. if (!isValidHex(hex)) {
  37. return null;
  38. }
  39. hex = hex.replace("#", "");
  40. if (hex.length === 3) {
  41. const [first, second, third] = hex.split("");
  42. const r = parseInt(`${first}${first}`, 16);
  43. const g = parseInt(`${second}${second}`, 16);
  44. const b = parseInt(`${third}${third}`, 16);
  45. return { r, g, b };
  46. }
  47. const r = parseInt(hex.slice(0, 2), 16);
  48. const g = parseInt(hex.slice(2, 4), 16);
  49. const b = parseInt(hex.slice(4, 6), 16);
  50. return { r, g, b };
  51. }
  52. // these utils allow users to pass enum values as strings without having to access the enum
  53. // based on the approach suggested by https://github.com/microsoft/TypeScript/issues/17690#issuecomment-321365759,
  54. const enumify = (x) => x;
  55. const CSSColorMode = enumify({
  56. HEX: "hex",
  57. HEXA: "hexa",
  58. RGB_CSS: "rgb-css",
  59. RGBA_CSS: "rgba-css",
  60. HSL_CSS: "hsl-css",
  61. HSLA_CSS: "hsla-css"
  62. });
  63. const ObjectColorMode = enumify({
  64. RGB: "rgb",
  65. RGBA: "rgba",
  66. HSL: "hsl",
  67. HSLA: "hsla",
  68. HSV: "hsv",
  69. HSVA: "hsva"
  70. });
  71. function parseMode(colorValue) {
  72. if (typeof colorValue === "string") {
  73. if (colorValue.startsWith("#")) {
  74. const { length } = colorValue;
  75. if (length === 4 || length === 7) {
  76. return CSSColorMode.HEX;
  77. }
  78. if (length === 5 || length === 9) {
  79. return CSSColorMode.HEXA;
  80. }
  81. }
  82. if (colorValue.startsWith("rgba(")) {
  83. return CSSColorMode.RGBA_CSS;
  84. }
  85. if (colorValue.startsWith("rgb(")) {
  86. return CSSColorMode.RGB_CSS;
  87. }
  88. if (colorValue.startsWith("hsl(")) {
  89. return CSSColorMode.HSL_CSS;
  90. }
  91. if (colorValue.startsWith("hsla(")) {
  92. return CSSColorMode.HSLA_CSS;
  93. }
  94. }
  95. if (typeof colorValue === "object") {
  96. if (hasChannels(colorValue, "r", "g", "b")) {
  97. return hasChannels(colorValue, "a") ? ObjectColorMode.RGBA : ObjectColorMode.RGB;
  98. }
  99. if (hasChannels(colorValue, "h", "s", "l")) {
  100. return hasChannels(colorValue, "a") ? ObjectColorMode.HSLA : ObjectColorMode.HSL;
  101. }
  102. if (hasChannels(colorValue, "h", "s", "v")) {
  103. return hasChannels(colorValue, "a") ? ObjectColorMode.HSVA : ObjectColorMode.HSV;
  104. }
  105. }
  106. return null;
  107. }
  108. function hasChannels(colorObject, ...channels) {
  109. return channels.every((channel) => channel && colorObject && `${channel}` in colorObject);
  110. }
  111. function colorEqual(value1, value2) {
  112. return (value1 === null || value1 === void 0 ? void 0 : value1.rgbNumber()) === (value2 === null || value2 === void 0 ? void 0 : value2.rgbNumber());
  113. }
  114. exports.CSSColorMode = CSSColorMode;
  115. exports.colorEqual = colorEqual;
  116. exports.hexChar = hexChar;
  117. exports.hexToRGB = hexToRGB;
  118. exports.isLonghandHex = isLonghandHex;
  119. exports.isValidHex = isValidHex;
  120. exports.normalizeHex = normalizeHex;
  121. exports.parseMode = parseMode;
  122. exports.rgbToHex = rgbToHex;