utils.js 3.5 KB

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