pixel-data-js 0.8.0 → 0.9.0

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.
@@ -670,6 +670,9 @@ type IndexedImage = {
670
670
  */
671
671
  transparentPalletIndex: number;
672
672
  };
673
+ /**
674
+ * Converts standard ImageData into an IndexedImage format.
675
+ */
673
676
  /**
674
677
  * Converts standard ImageData into an IndexedImage format.
675
678
  */
@@ -1595,24 +1595,22 @@ function makeIndexedImage(imageData) {
1595
1595
  const rawData = new Uint32Array(imageData.data.buffer);
1596
1596
  const indexedData = new Int32Array(rawData.length);
1597
1597
  const colorMap = /* @__PURE__ */ new Map();
1598
- const tempPalette = [];
1599
1598
  const transparentColor = 0;
1600
1599
  const transparentPalletIndex = 0;
1601
1600
  colorMap.set(transparentColor, transparentPalletIndex);
1602
- tempPalette.push(transparentColor);
1603
1601
  for (let i = 0; i < rawData.length; i++) {
1604
1602
  const pixel = rawData[i];
1605
- const isTransparent = pixel >>> 24 === 0;
1603
+ const alpha = pixel >>> 24 & 255;
1604
+ const isTransparent = alpha === 0;
1606
1605
  const colorKey = isTransparent ? transparentColor : pixel;
1607
1606
  let id = colorMap.get(colorKey);
1608
1607
  if (id === void 0) {
1609
1608
  id = colorMap.size;
1610
- tempPalette.push(colorKey);
1611
1609
  colorMap.set(colorKey, id);
1612
1610
  }
1613
1611
  indexedData[i] = id;
1614
1612
  }
1615
- const palette = new Int32Array(tempPalette);
1613
+ const palette = new Int32Array(colorMap.keys());
1616
1614
  return {
1617
1615
  width,
1618
1616
  height,
@@ -1625,7 +1623,7 @@ function makeIndexedImage(imageData) {
1625
1623
  // src/IndexedImage/indexedImageToAverageColor.ts
1626
1624
  function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1627
1625
  const { data, palette, transparentPalletIndex } = indexedImage;
1628
- const counts = new Uint32Array(palette.length / 4);
1626
+ const counts = new Uint32Array(palette.length);
1629
1627
  for (let i = 0; i < data.length; i++) {
1630
1628
  const id = data[i];
1631
1629
  counts[id]++;
@@ -1643,11 +1641,11 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1643
1641
  if (!includeTransparent && id === transparentPalletIndex) {
1644
1642
  continue;
1645
1643
  }
1646
- const pIdx = id * 4;
1647
- const r2 = palette[pIdx];
1648
- const g2 = palette[pIdx + 1];
1649
- const b2 = palette[pIdx + 2];
1650
- const a2 = palette[pIdx + 3];
1644
+ const color = palette[id] >>> 0;
1645
+ const r2 = color & 255;
1646
+ const g2 = color >> 8 & 255;
1647
+ const b2 = color >> 16 & 255;
1648
+ const a2 = color >> 24 & 255;
1651
1649
  rSum += r2 * weight;
1652
1650
  gSum += g2 * weight;
1653
1651
  bSum += b2 * weight;