Opacity in Image.getPixelReader.getColor()
Jim Graham
james.graham at oracle.com
Thu Mar 20 02:01:29 UTC 2014
Kevin and Jasper hit the nail on the head (Jasper's comment that the
words are usually associated with particular ranges is mostly true
AFAIRemember, but I can't guarantee it).
If there is part of the documentation here that could be made clearer,
let us know...
...jim
On 3/17/14 1:07 PM, Tom Schindl wrote:
> Hi,
>
> Yes it looks like i mixed this up with transparency!
>
> Tom
>
> On 17.03.14 10:03, Kevin Rushforth wrote:
>> I'm sure Jim could word this better, but here goes...
>>
>> Opacity and alpha are mostly used interchangeably. Whether the range is
>> expressed as a normalized value [0.0,1.0] or an 8-bit pixel value
>> [0,255], the meaning is the same. A value of 0 means that the color is
>> completely transparent. When using the default SRC_OVER blend equation,
>> a pixel with this color will have no effect. A value of 1.0 (if using a
>> normalized value) or 255 (if using an 8-bit integer value) means that
>> the color is fully opaque. When using the default SRC_OVER blend
>> equation, a pixel with this color will overwrite whatever is underneath it.
>>
>> Perhaps Tom is thinking of "transparency" which, if we were to have such
>> an attribute, would be defined as (1.0-opacity).
>>
>> -- Kevin
>>
>>
>> Jasper Potts wrote:
>>> My understanding is alpha and opacity are same just different range.
>>> Opacity is 0.0 to 1.0
>>> Alpha is 0 to 255
>>>
>>> Jim or Kevin will be authority on this.
>>> Jasper
>>>
>>>
>>>> On Mar 16, 2014, at 5:55 AM, Tom Schindl
>>>> <tom.schindl at bestsolution.at> wrote:
>>>>
>>>> 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