Opacity in Image.getPixelReader.getColor()
Tom Schindl
tom.schindl at bestsolution.at
Sun Mar 16 12:55:57 UTC 2014
Hi,
Maybe I'm completely wrong but to me it looks like the opacity I get
from Image.getPixelReader.getColor() is wrong.
If not mistaken the relation between alpha and opacity is expressed with:
opacity = (255 - alpha) / 255.0
which means:
opacity 0 => alpha 255
opacity 1 => alpha 0
Running the following programm on a gif
> public class OpacityBug extends Application {
>
> @Override
> public void start(Stage primaryStage) throws Exception {
> Image image = new Image(getClass().getResourceAsStream("methpri_obj.gif"));
> int width = (int) image.getWidth();
> int height = (int) image.getHeight();
>
> PixelReader reader = image.getPixelReader();
> for (int x = width - 1; x >= 0; x--) {
> for (int y = height - 1; y >= 0; y--) {
> int argb = reader.getArgb(x, y);
> int alphaArgb = (argb >> 24) & 0xFF;
> Color color = reader.getColor(x, y);
> double opacity = color.getOpacity();
> System.err.println(x+","+y + " => alpha: " + alphaArgb + " op: " + opacity + " op-alpha: " + opacityToAlpha(opacity) + " color: " + color);
> }
> }
> }
>
>
> private static int opacityToAlpha(double opacity) {
> return (int)Math.round((((opacity * 255.0) - 255) * -1));
> }
>
> public static void main(String[] args) {
> Application.launch(args);
> }
> }
produces and output like:
15,15 => alpha: 0 op: 0.0 op-alpha: 255 color: 0x00000000
which to my understanding is wrong. The argb value is correct (if
compared to the image because the pixel at 15,15 is fully transparent)
but then the the opacity should be 1.0 but is the opposite.
Looking at the code in Image I see:
> @Override
> public Color getColor(int x, int y) {
> int argb = getArgb(x, y);
> int a = argb >>> 24;
> int r = (argb >> 16) & 0xff;
> int g = (argb >> 8) & 0xff;
> int b = (argb ) & 0xff;
> return Color.rgb(r, g, b, a / 255.0);
> }
which means that:
a) my formula from the above is wrong
b) or there's the bug because it should be
return Color.rgb(r, g, b, (255 - a) / 255.0);
May I also suggest to add get a
- Color.argb(int r,int g,int b,int alpha)
maybe it is just me but I'm so much more used to alpha than opacity that
I always have to look it up when i need it.
Tom
More information about the openjfx-dev
mailing list