@trackunit/ui-design-tokens 1.14.10 → 1.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs.js CHANGED
@@ -740,62 +740,31 @@ const themeColors = {
740
740
  AM_UNSUPPORTED: { name: "am_unsupported", variants: trackunitPalette.AM_UNSUPPORTED, defaultVariant: 500 },
741
741
  };
742
742
 
743
- function componentToHex(c) {
744
- if (c === null || c === undefined) {
745
- return "00";
746
- }
747
- const hex = c.toString(16);
748
- return hex.length === 1 ? `0${hex}` : hex;
749
- }
750
- function rgbToHex(r, g, b) {
751
- if (isNaN(r) || isNaN(g) || isNaN(b)) {
752
- return "#000000";
753
- }
754
- return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`;
755
- }
756
- /**
757
- * Convert a string to a HEX color.
758
- *
759
- * @param colorString - The string to convert to a HEX color. defaults to "#000000" if the string is not a valid RGB color.
760
- * @returns {string} HEX color
761
- */
762
- const rgbStringToHex = (colorString) => {
763
- if (colorString.split(" ").length === 3) {
764
- const [r, g, b] = colorString.split(" ").map(Number);
765
- //check if the values are valid numbers
766
- if (typeof r !== "number" || typeof g !== "number" || typeof b !== "number") {
767
- return "#000000";
768
- }
769
- if (isNaN(r) || isNaN(g) || isNaN(b)) {
770
- return "#000000";
771
- }
772
- return rgbToHex(r, g, b);
773
- }
774
- return "#000000";
743
+ const WRAPPED_CSS_VARIABLE = /rgb\(var\((.*?)\) \/ var\(--tw-bg-opacity\)\)/;
744
+ const BLACK = "#000000";
745
+ const isRgbTriple = (values) => values.length === 3 && values.every(value => !Number.isNaN(value));
746
+ const parseRgbChannels = (value) => {
747
+ const channels = value
748
+ .trim()
749
+ .replace(/^rgb\(/, "")
750
+ .split("/")[0]
751
+ ?.replace(/\)+$/, "")
752
+ .trim()
753
+ .split(/[\s,]+/)
754
+ .map(Number);
755
+ return channels !== undefined && isRgbTriple(channels) ? channels : null;
775
756
  };
776
- /**
777
- * Returns the HEX color based on a Color from the theme.
778
- * Reads the RGB value from the underlying CSS variable and convert to HEX.
779
- *
780
- * Example: color("PRIMARY", 500, "HEX") -> #3b82f6
781
- *
782
- * @param variable A color from the `colors` object of the Theme
783
- * @returns {string} HEX color
784
- */
757
+ const channelToHex = (channel) => Math.round(Math.min(255, Math.max(0, channel)))
758
+ .toString(16)
759
+ .padStart(2, "0");
760
+ const channelsToHex = (channels) => channels === null ? BLACK : `#${channels.map(channelToHex).join("")}`;
761
+ /** Convert a palette or rgb() string to HEX. Returns `#000000` when parsing fails. */
762
+ const rgbStringToHex = (colorString) => channelsToHex(parseRgbChannels(colorString));
763
+ /** Read a theme CSS variable from the document and convert it to HEX. */
785
764
  const getHEX = (variable) => {
786
- const regex = /rgb\(var\((.*?)\) \/ var\(--tw-bg-opacity\)\)/g;
787
- const root = document.documentElement;
788
- const match = regex.exec(variable);
789
- const colorValue = getComputedStyle(root).getPropertyValue(match?.[1]?.trim() ?? variable);
790
- let onlyRgb = colorValue.replace("rgb(", "");
791
- if (onlyRgb.indexOf("/") !== -1) {
792
- onlyRgb = onlyRgb.substring(0, onlyRgb.indexOf("/")).trim();
793
- }
794
- if (onlyRgb.indexOf(")") !== -1) {
795
- onlyRgb = onlyRgb.substring(0, onlyRgb.indexOf(")")).trim();
796
- }
797
- const RGB = onlyRgb.split(" ").map(channel => Number(channel));
798
- return rgbToHex(RGB[0] ?? NaN, RGB[1] ?? NaN, RGB[2] ?? NaN);
765
+ const variableName = WRAPPED_CSS_VARIABLE.exec(variable)?.[1]?.trim() ?? variable;
766
+ const cssValue = getComputedStyle(document.documentElement).getPropertyValue(variableName).trim();
767
+ return channelsToHex(parseRgbChannels(cssValue));
799
768
  };
800
769
 
801
770
  const DEFAULT_CHART_OTHER = rgbStringToHex(tailwindPalette.gray[400]);
package/index.esm.js CHANGED
@@ -738,62 +738,31 @@ const themeColors = {
738
738
  AM_UNSUPPORTED: { name: "am_unsupported", variants: trackunitPalette.AM_UNSUPPORTED, defaultVariant: 500 },
739
739
  };
740
740
 
741
- function componentToHex(c) {
742
- if (c === null || c === undefined) {
743
- return "00";
744
- }
745
- const hex = c.toString(16);
746
- return hex.length === 1 ? `0${hex}` : hex;
747
- }
748
- function rgbToHex(r, g, b) {
749
- if (isNaN(r) || isNaN(g) || isNaN(b)) {
750
- return "#000000";
751
- }
752
- return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`;
753
- }
754
- /**
755
- * Convert a string to a HEX color.
756
- *
757
- * @param colorString - The string to convert to a HEX color. defaults to "#000000" if the string is not a valid RGB color.
758
- * @returns {string} HEX color
759
- */
760
- const rgbStringToHex = (colorString) => {
761
- if (colorString.split(" ").length === 3) {
762
- const [r, g, b] = colorString.split(" ").map(Number);
763
- //check if the values are valid numbers
764
- if (typeof r !== "number" || typeof g !== "number" || typeof b !== "number") {
765
- return "#000000";
766
- }
767
- if (isNaN(r) || isNaN(g) || isNaN(b)) {
768
- return "#000000";
769
- }
770
- return rgbToHex(r, g, b);
771
- }
772
- return "#000000";
741
+ const WRAPPED_CSS_VARIABLE = /rgb\(var\((.*?)\) \/ var\(--tw-bg-opacity\)\)/;
742
+ const BLACK = "#000000";
743
+ const isRgbTriple = (values) => values.length === 3 && values.every(value => !Number.isNaN(value));
744
+ const parseRgbChannels = (value) => {
745
+ const channels = value
746
+ .trim()
747
+ .replace(/^rgb\(/, "")
748
+ .split("/")[0]
749
+ ?.replace(/\)+$/, "")
750
+ .trim()
751
+ .split(/[\s,]+/)
752
+ .map(Number);
753
+ return channels !== undefined && isRgbTriple(channels) ? channels : null;
773
754
  };
774
- /**
775
- * Returns the HEX color based on a Color from the theme.
776
- * Reads the RGB value from the underlying CSS variable and convert to HEX.
777
- *
778
- * Example: color("PRIMARY", 500, "HEX") -> #3b82f6
779
- *
780
- * @param variable A color from the `colors` object of the Theme
781
- * @returns {string} HEX color
782
- */
755
+ const channelToHex = (channel) => Math.round(Math.min(255, Math.max(0, channel)))
756
+ .toString(16)
757
+ .padStart(2, "0");
758
+ const channelsToHex = (channels) => channels === null ? BLACK : `#${channels.map(channelToHex).join("")}`;
759
+ /** Convert a palette or rgb() string to HEX. Returns `#000000` when parsing fails. */
760
+ const rgbStringToHex = (colorString) => channelsToHex(parseRgbChannels(colorString));
761
+ /** Read a theme CSS variable from the document and convert it to HEX. */
783
762
  const getHEX = (variable) => {
784
- const regex = /rgb\(var\((.*?)\) \/ var\(--tw-bg-opacity\)\)/g;
785
- const root = document.documentElement;
786
- const match = regex.exec(variable);
787
- const colorValue = getComputedStyle(root).getPropertyValue(match?.[1]?.trim() ?? variable);
788
- let onlyRgb = colorValue.replace("rgb(", "");
789
- if (onlyRgb.indexOf("/") !== -1) {
790
- onlyRgb = onlyRgb.substring(0, onlyRgb.indexOf("/")).trim();
791
- }
792
- if (onlyRgb.indexOf(")") !== -1) {
793
- onlyRgb = onlyRgb.substring(0, onlyRgb.indexOf(")")).trim();
794
- }
795
- const RGB = onlyRgb.split(" ").map(channel => Number(channel));
796
- return rgbToHex(RGB[0] ?? NaN, RGB[1] ?? NaN, RGB[2] ?? NaN);
763
+ const variableName = WRAPPED_CSS_VARIABLE.exec(variable)?.[1]?.trim() ?? variable;
764
+ const cssValue = getComputedStyle(document.documentElement).getPropertyValue(variableName).trim();
765
+ return channelsToHex(parseRgbChannels(cssValue));
797
766
  };
798
767
 
799
768
  const DEFAULT_CHART_OTHER = rgbStringToHex(tailwindPalette.gray[400]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/ui-design-tokens",
3
- "version": "1.14.10",
3
+ "version": "1.15.2",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -1,17 +1,4 @@
1
- /**
2
- * Convert a string to a HEX color.
3
- *
4
- * @param colorString - The string to convert to a HEX color. defaults to "#000000" if the string is not a valid RGB color.
5
- * @returns {string} HEX color
6
- */
1
+ /** Convert a palette or rgb() string to HEX. Returns `#000000` when parsing fails. */
7
2
  export declare const rgbStringToHex: (colorString: string) => string;
8
- /**
9
- * Returns the HEX color based on a Color from the theme.
10
- * Reads the RGB value from the underlying CSS variable and convert to HEX.
11
- *
12
- * Example: color("PRIMARY", 500, "HEX") -> #3b82f6
13
- *
14
- * @param variable A color from the `colors` object of the Theme
15
- * @returns {string} HEX color
16
- */
3
+ /** Read a theme CSS variable from the document and convert it to HEX. */
17
4
  export declare const getHEX: (variable: string) => string;