PNG RGB transformation related to alpha in getImag

2019-09-02 20:43发布

问题:

I have a PNG image with some pixels having an alpha of 254. These pixels have this color : RGBA{183, 221, 129, 254} (not a javascript notation).

I can check in Gimp that the R, G and B components are 183, 221 and 129.

But when I read my pixels in Chrome using getImageData (on a canvas) the R, G and B values are modified to 182, 220 and 128 (A being correctly 254).

Is that a standardized transformation or a bug ? If that's normal, where can I find the exact formula to predict RGB values given by getImageData from the ones in the PNG ?

回答1:

It's almost certainly caused by gamma correction. In short, most web browsers ignore the colour-space included in the image and use the default one (as its impossible to get everything to use the colour space without a large performance penalty, especially plugins such as Flash).

Provided you don't care about really old browsers (Opera 6 and Safari 1 most notably), you can just save the image without any gamma information.