@ppg_pl/tinting 0.0.3 → 0.0.5

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.
@@ -2565,10 +2565,21 @@ const fetchColors = async (filters) => {
2565
2565
  return [];
2566
2566
  }
2567
2567
  };
2568
+ const getSampleSortForProduct = (sample, productId) => {
2569
+ var _a, _b;
2570
+ const link = (_a = sample === null || sample === void 0 ? void 0 : sample.Products) === null || _a === void 0 ? void 0 : _a.find((p) => p.Product_id === productId);
2571
+ // put null/undefined at the end
2572
+ return (_b = link === null || link === void 0 ? void 0 : link.sort) !== null && _b !== void 0 ? _b : Number.POSITIVE_INFINITY;
2573
+ };
2568
2574
  const fetchSamplesColors = async (filters) => {
2569
2575
  try {
2576
+ // you already include Products.* in the query
2577
+ const { id: productId } = filters;
2570
2578
  const response = await ApiService.getData(`items/Sample?${fetchColorsQuery(Object.assign({}, filters))}`);
2571
- return (response === null || response === void 0 ? void 0 : response.data) || [];
2579
+ const data = (response === null || response === void 0 ? void 0 : response.data) || [];
2580
+ // order by junction sort
2581
+ const ordered = [...data].sort((a, b) => getSampleSortForProduct(a, productId) - getSampleSortForProduct(b, productId));
2582
+ return ordered;
2572
2583
  }
2573
2584
  catch (error) {
2574
2585
  console.error('Error fetching samples colors:', error);
@@ -6601,10 +6612,20 @@ const Modal = class {
6601
6612
  let mostSimilarIndex = 0;
6602
6613
  let minDistance = Infinity;
6603
6614
  remainingColors.forEach((color, index) => {
6604
- const distance = chroma.distance(lastColor.hex, color.hex, 'lab');
6605
- if (distance < minDistance) {
6606
- minDistance = distance;
6607
- mostSimilarIndex = index;
6615
+ try {
6616
+ // Validate hex colors before calculating distance
6617
+ if (!color.hex || !lastColor.hex) {
6618
+ return;
6619
+ }
6620
+ const distance = chroma.distance(lastColor.hex, color.hex, 'lab');
6621
+ if (distance < minDistance) {
6622
+ minDistance = distance;
6623
+ mostSimilarIndex = index;
6624
+ }
6625
+ }
6626
+ catch (error) {
6627
+ console.warn(`Error calculating color distance for ${color.hex}:`, error);
6628
+ // Skip this color if it causes an error
6608
6629
  }
6609
6630
  });
6610
6631
  // Move the most similar color to sortedColors
@@ -6613,36 +6634,65 @@ const Modal = class {
6613
6634
  return sortedColors;
6614
6635
  }
6615
6636
  getColorHueGroup(hex) {
6616
- const color = chroma(hex);
6617
- const hue = color.get('hsl.h');
6618
- const saturation = color.get('hsl.s');
6619
- const lightness = color.get('hsl.l');
6620
- // Handle grayscale colors
6621
- if (saturation < 0.1) {
6622
- if (lightness < 0.2)
6623
- return 'black';
6624
- if (lightness > 0.8)
6625
- return 'white';
6626
- return 'gray';
6627
- }
6628
- // Group colors by hue ranges
6629
- if (hue >= 330 || hue < 30)
6630
- return 'red';
6631
- if (hue >= 30 && hue < 90)
6632
- return 'yellow';
6633
- if (hue >= 90 && hue < 150)
6634
- return 'green';
6635
- if (hue >= 150 && hue < 210)
6636
- return 'cyan';
6637
- if (hue >= 210 && hue < 270)
6638
- return 'blue';
6639
- if (hue >= 270 && hue < 330)
6640
- return 'magenta';
6641
- return 'other';
6637
+ try {
6638
+ // Validate hex format and handle invalid colors
6639
+ if (!hex || typeof hex !== 'string') {
6640
+ return 'other';
6641
+ }
6642
+ // Basic hex validation
6643
+ const cleanHex = hex.startsWith('#') ? hex : `#${hex}`;
6644
+ if (!/^#[0-9A-Fa-f]{6}$/.test(cleanHex)) {
6645
+ console.warn(`Invalid hex color format: ${hex}`);
6646
+ return 'other';
6647
+ }
6648
+ const color = chroma(cleanHex);
6649
+ const hue = color.get('hsl.h');
6650
+ const saturation = color.get('hsl.s');
6651
+ const lightness = color.get('hsl.l');
6652
+ // Handle grayscale colors
6653
+ if (saturation < 0.1) {
6654
+ if (lightness < 0.2)
6655
+ return 'black';
6656
+ if (lightness > 0.8)
6657
+ return 'white';
6658
+ return 'gray';
6659
+ }
6660
+ // Group colors by hue ranges
6661
+ if (hue >= 330 || hue < 30)
6662
+ return 'red';
6663
+ if (hue >= 30 && hue < 90)
6664
+ return 'yellow';
6665
+ if (hue >= 90 && hue < 150)
6666
+ return 'green';
6667
+ if (hue >= 150 && hue < 210)
6668
+ return 'cyan';
6669
+ if (hue >= 210 && hue < 270)
6670
+ return 'blue';
6671
+ if (hue >= 270 && hue < 330)
6672
+ return 'magenta';
6673
+ return 'other';
6674
+ }
6675
+ catch (error) {
6676
+ console.warn(`Error parsing color "${hex}":`, error);
6677
+ return 'other';
6678
+ }
6642
6679
  }
6643
6680
  sortColorsByHueAndSimilarity(colors) {
6644
6681
  if ((colors === null || colors === void 0 ? void 0 : colors.length) <= 1)
6645
6682
  return colors;
6683
+ // Filter out colors with invalid hex values
6684
+ const validColors = colors.filter(color => {
6685
+ if (!color.hex || typeof color.hex !== 'string') {
6686
+ console.warn(`Color missing hex value:`, color);
6687
+ return false;
6688
+ }
6689
+ const cleanHex = color.hex.startsWith('#') ? color.hex : `#${color.hex}`;
6690
+ const isValid = /^#[0-9A-Fa-f]{6}$/.test(cleanHex);
6691
+ if (!isValid) {
6692
+ console.warn(`Invalid hex color format: ${color.hex} for color:`, color);
6693
+ }
6694
+ return isValid;
6695
+ });
6646
6696
  // Group valid colors by their hue family
6647
6697
  const colorGroups = {
6648
6698
  red: [],
@@ -6656,7 +6706,7 @@ const Modal = class {
6656
6706
  white: [],
6657
6707
  other: [],
6658
6708
  };
6659
- colors.forEach(color => {
6709
+ validColors.forEach(color => {
6660
6710
  const group = this.getColorHueGroup(color.hex);
6661
6711
  colorGroups[group].push(color);
6662
6712
  });
@@ -6668,7 +6718,7 @@ const Modal = class {
6668
6718
  sortedGroups.push(this.sortColorsBySimilarity(colorGroups[groupName]));
6669
6719
  }
6670
6720
  });
6671
- // Combine all sorted groups, then append invalid colors at the end
6721
+ // Combine all sorted groups
6672
6722
  return sortedGroups.flat();
6673
6723
  }
6674
6724
  async fetchColorsData({ reset = false } = {}) {