gifencoder icon indicating copy to clipboard operation
gifencoder copied to clipboard

Transparency should not fail on neighboring colors

Open arikwex opened this issue 8 years ago • 1 comments

I was trying to make a gradient fading to black and also have black represent full transparency. It seems like the color mapping for this gradient scenario would sometimes make findClosest fail to select the correct color index to represent transparency... My proposed fix is to force the transparency color in the original image to ALWAYS map to the transparency color in the colormapped image.

Specifically,

// GIFEncoder.js @ analyzePixels
for (var pixelIndex = 0; pixelIndex < nPix; pixelIndex++) {
      if (this.image[pixelIndex * 4 + 3] == 0) {
        this.indexedPixels[pixelIndex] = this.transIndex;
      }
     if (
        this.image[pixelIndex * 4 ] == ((this.transparent & 0xff0000) >> 16) &&
        this.image[pixelIndex * 4 + 1] == ((this.transparent & 0xff00) >> 8) &&
        this.image[pixelIndex * 4 + 2] == ((this.transparent & 0xff))
      ) {
        this.indexedPixels[pixelIndex] = this.transIndex;
      }
    }

Maybe this is only useful for me... but figured I'd share my findings.

arikwex avatar Dec 21 '17 23:12 arikwex

what's this.transparent?

kurnal avatar May 07 '20 17:05 kurnal