Java - Trouble understanding opacity value - parsing 64 bit long to float and adding opacityIs Java “pass-by-reference” or “pass-by-value”?How do I parse a string to a float or int?How to get an enum value from a string value in Java?Does Java support default parameter values?How do I determine whether an array contains a particular value in Java?How to convert float to int with JavaHow to parse JSON in JavaHow to parse float with two decimal places in javascript?Parse long double from stringCan IEEE 754 floating-point numbers represent the exact same value with multiple bit arrangements?
Was lunar module "pilot" Harrison Schmitt legally a "pilot" at the time?
In Parshas Chukas, why is first mention of Parah Adumah "פָרָה" instead of "פָּרָה"?
Is anyone advocating the promotion of homosexuality in UK schools?
Who has taken "my" Managed package namespace? Can we find out?
Why isn't pressure filtration popular compared to vacuum filtration?
As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?
Why are they 'nude photos'?
Why does my String turn into Integers instead of letters after I add characters with +?
How to know whether a Tamron lens is compatible with Canon EOS 60D?
Shortest distance around a pyramid?
Is there any word for "disobedience to God"?
Novel where a group of scientists in a spaceship encounter various aliens
Why didn't Thanos kill all the Dwarves on Nidavellir?
Did Lincoln tell Stowe "So you're the little woman that started this great war!"?
What is this welding tool I found in my attic?
Why does my script create an extra character?
Does throwing a penny at a train stop the train?
If the railway suggests a 5-min connection window for changing trains in the Netherlands, does that mean it's definitely doable?
The monorail explodes before I can get on it
Is there a word for a message that is intended to be intercepted by an adversary?
Why would guns not work in the dungeon?
Flatten array with OPENJSON: OPENJSON on a value that may not be an array? [ [1] ], vs [1]
Why does the U.S. tolerate foreign influence from Saudi Arabia and Israel on its domestic policies while not tolerating that from China or Russia?
Can I call 112 to check a police officer's identity in the Czech Republic?
Java - Trouble understanding opacity value - parsing 64 bit long to float and adding opacity
Is Java “pass-by-reference” or “pass-by-value”?How do I parse a string to a float or int?How to get an enum value from a string value in Java?Does Java support default parameter values?How do I determine whether an array contains a particular value in Java?How to convert float to int with JavaHow to parse JSON in JavaHow to parse float with two decimal places in javascript?Parse long double from stringCan IEEE 754 floating-point numbers represent the exact same value with multiple bit arrangements?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to understand some code that is creating an opacity value from the original color value.
long fillColorValue = fillColor != null ? Long.parseLong(fillColor) : 0;
//Set opacity based on alpha
long alpha = fillColorValue >> 24;
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
shape.setFillOpacity(opacity);
- First the String color value is parsed to long.
- Then the bits of that value are being moved 24 bits with a rightshift - why?
long alpha = fillColorValue >> 24;
- then the long is used to create a float seemingly representing an opacity value.
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
It'd be great if I could have some clarity on this method of defining opacity - what's actually happening here?
The shape fill color is originally represented by a String value -1751607581.
java floating-point opacity long-integer
add a comment |
I'm trying to understand some code that is creating an opacity value from the original color value.
long fillColorValue = fillColor != null ? Long.parseLong(fillColor) : 0;
//Set opacity based on alpha
long alpha = fillColorValue >> 24;
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
shape.setFillOpacity(opacity);
- First the String color value is parsed to long.
- Then the bits of that value are being moved 24 bits with a rightshift - why?
long alpha = fillColorValue >> 24;
- then the long is used to create a float seemingly representing an opacity value.
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
It'd be great if I could have some clarity on this method of defining opacity - what's actually happening here?
The shape fill color is originally represented by a String value -1751607581.
java floating-point opacity long-integer
1
You seem to have a good handle on the bit-shifting, which seems like the more difficult concept. Are you unfamiliar with the ternary operator (` x ? p : q `), or are you asking why zero is being treated specially)
– erickson
Mar 26 at 3:45
1
Or maybe you are asking why a 64-bitlong
is used, shifting off 24 bits and apparently working with the remaining 40?
– erickson
Mar 26 at 4:01
Do you know what color format is used? It can be assumed, that first 24 bit is RGB (8 bit per channel) and remaining 40 bit is alpha channel, but in such case the subsequent operations seems strange.
– Mikhail Ilinykh
Mar 26 at 4:19
1
In order to retrieve the integer Alpha channel value from the fillColorValue you need to right shift bits by 24. Not really sure why fillColorValue and alpha variables are declared a long when int would have sufficed. In the code you supplied, a Ternary is used to handle the chance that the Alpha value might be 0. Odd though since an Alpha value of 0 would normally mean full transparency but in this case the Ternary is enforcing the fact that any value less than 1 would place opacity to completely opaque (1.0f). There would of course be good reason for this in many different situations.
– DevilsHnd
Mar 26 at 8:27
Not being very familiar with alpha and int/long to float conversions the method didn't make much sense to me, and the intent behind the code wan't clear. as @DevilsHnd pointed out the ternary is weird, and in fact this was the issue... transparency was showing as completely white.
– Continuity8
Mar 27 at 4:10
add a comment |
I'm trying to understand some code that is creating an opacity value from the original color value.
long fillColorValue = fillColor != null ? Long.parseLong(fillColor) : 0;
//Set opacity based on alpha
long alpha = fillColorValue >> 24;
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
shape.setFillOpacity(opacity);
- First the String color value is parsed to long.
- Then the bits of that value are being moved 24 bits with a rightshift - why?
long alpha = fillColorValue >> 24;
- then the long is used to create a float seemingly representing an opacity value.
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
It'd be great if I could have some clarity on this method of defining opacity - what's actually happening here?
The shape fill color is originally represented by a String value -1751607581.
java floating-point opacity long-integer
I'm trying to understand some code that is creating an opacity value from the original color value.
long fillColorValue = fillColor != null ? Long.parseLong(fillColor) : 0;
//Set opacity based on alpha
long alpha = fillColorValue >> 24;
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
shape.setFillOpacity(opacity);
- First the String color value is parsed to long.
- Then the bits of that value are being moved 24 bits with a rightshift - why?
long alpha = fillColorValue >> 24;
- then the long is used to create a float seemingly representing an opacity value.
float opacity = (alpha > 0 ? alpha / 255.0f : 1.0f);
It'd be great if I could have some clarity on this method of defining opacity - what's actually happening here?
The shape fill color is originally represented by a String value -1751607581.
java floating-point opacity long-integer
java floating-point opacity long-integer
edited Mar 26 at 3:37
Continuity8
asked Mar 26 at 3:25
Continuity8Continuity8
6182 gold badges9 silver badges27 bronze badges
6182 gold badges9 silver badges27 bronze badges
1
You seem to have a good handle on the bit-shifting, which seems like the more difficult concept. Are you unfamiliar with the ternary operator (` x ? p : q `), or are you asking why zero is being treated specially)
– erickson
Mar 26 at 3:45
1
Or maybe you are asking why a 64-bitlong
is used, shifting off 24 bits and apparently working with the remaining 40?
– erickson
Mar 26 at 4:01
Do you know what color format is used? It can be assumed, that first 24 bit is RGB (8 bit per channel) and remaining 40 bit is alpha channel, but in such case the subsequent operations seems strange.
– Mikhail Ilinykh
Mar 26 at 4:19
1
In order to retrieve the integer Alpha channel value from the fillColorValue you need to right shift bits by 24. Not really sure why fillColorValue and alpha variables are declared a long when int would have sufficed. In the code you supplied, a Ternary is used to handle the chance that the Alpha value might be 0. Odd though since an Alpha value of 0 would normally mean full transparency but in this case the Ternary is enforcing the fact that any value less than 1 would place opacity to completely opaque (1.0f). There would of course be good reason for this in many different situations.
– DevilsHnd
Mar 26 at 8:27
Not being very familiar with alpha and int/long to float conversions the method didn't make much sense to me, and the intent behind the code wan't clear. as @DevilsHnd pointed out the ternary is weird, and in fact this was the issue... transparency was showing as completely white.
– Continuity8
Mar 27 at 4:10
add a comment |
1
You seem to have a good handle on the bit-shifting, which seems like the more difficult concept. Are you unfamiliar with the ternary operator (` x ? p : q `), or are you asking why zero is being treated specially)
– erickson
Mar 26 at 3:45
1
Or maybe you are asking why a 64-bitlong
is used, shifting off 24 bits and apparently working with the remaining 40?
– erickson
Mar 26 at 4:01
Do you know what color format is used? It can be assumed, that first 24 bit is RGB (8 bit per channel) and remaining 40 bit is alpha channel, but in such case the subsequent operations seems strange.
– Mikhail Ilinykh
Mar 26 at 4:19
1
In order to retrieve the integer Alpha channel value from the fillColorValue you need to right shift bits by 24. Not really sure why fillColorValue and alpha variables are declared a long when int would have sufficed. In the code you supplied, a Ternary is used to handle the chance that the Alpha value might be 0. Odd though since an Alpha value of 0 would normally mean full transparency but in this case the Ternary is enforcing the fact that any value less than 1 would place opacity to completely opaque (1.0f). There would of course be good reason for this in many different situations.
– DevilsHnd
Mar 26 at 8:27
Not being very familiar with alpha and int/long to float conversions the method didn't make much sense to me, and the intent behind the code wan't clear. as @DevilsHnd pointed out the ternary is weird, and in fact this was the issue... transparency was showing as completely white.
– Continuity8
Mar 27 at 4:10
1
1
You seem to have a good handle on the bit-shifting, which seems like the more difficult concept. Are you unfamiliar with the ternary operator (` x ? p : q `), or are you asking why zero is being treated specially)
– erickson
Mar 26 at 3:45
You seem to have a good handle on the bit-shifting, which seems like the more difficult concept. Are you unfamiliar with the ternary operator (` x ? p : q `), or are you asking why zero is being treated specially)
– erickson
Mar 26 at 3:45
1
1
Or maybe you are asking why a 64-bit
long
is used, shifting off 24 bits and apparently working with the remaining 40?– erickson
Mar 26 at 4:01
Or maybe you are asking why a 64-bit
long
is used, shifting off 24 bits and apparently working with the remaining 40?– erickson
Mar 26 at 4:01
Do you know what color format is used? It can be assumed, that first 24 bit is RGB (8 bit per channel) and remaining 40 bit is alpha channel, but in such case the subsequent operations seems strange.
– Mikhail Ilinykh
Mar 26 at 4:19
Do you know what color format is used? It can be assumed, that first 24 bit is RGB (8 bit per channel) and remaining 40 bit is alpha channel, but in such case the subsequent operations seems strange.
– Mikhail Ilinykh
Mar 26 at 4:19
1
1
In order to retrieve the integer Alpha channel value from the fillColorValue you need to right shift bits by 24. Not really sure why fillColorValue and alpha variables are declared a long when int would have sufficed. In the code you supplied, a Ternary is used to handle the chance that the Alpha value might be 0. Odd though since an Alpha value of 0 would normally mean full transparency but in this case the Ternary is enforcing the fact that any value less than 1 would place opacity to completely opaque (1.0f). There would of course be good reason for this in many different situations.
– DevilsHnd
Mar 26 at 8:27
In order to retrieve the integer Alpha channel value from the fillColorValue you need to right shift bits by 24. Not really sure why fillColorValue and alpha variables are declared a long when int would have sufficed. In the code you supplied, a Ternary is used to handle the chance that the Alpha value might be 0. Odd though since an Alpha value of 0 would normally mean full transparency but in this case the Ternary is enforcing the fact that any value less than 1 would place opacity to completely opaque (1.0f). There would of course be good reason for this in many different situations.
– DevilsHnd
Mar 26 at 8:27
Not being very familiar with alpha and int/long to float conversions the method didn't make much sense to me, and the intent behind the code wan't clear. as @DevilsHnd pointed out the ternary is weird, and in fact this was the issue... transparency was showing as completely white.
– Continuity8
Mar 27 at 4:10
Not being very familiar with alpha and int/long to float conversions the method didn't make much sense to me, and the intent behind the code wan't clear. as @DevilsHnd pointed out the ternary is weird, and in fact this was the issue... transparency was showing as completely white.
– Continuity8
Mar 27 at 4:10
add a comment |
1 Answer
1
active
oldest
votes
If the color are represented on 32 bits, like your example seems to, it looks like it is encoded with an alpha channel and three color channels, each on eight bits (most probably ARGB).
Shifting >> 24
thus eliminates the RGB components, and keep alpha channels in low bits.
But beware of signed arithmetic! if original string is negative number, then fillColorValue
will be negative, and remain negative after arithmetic >>
shift...
With the guard on alpha > 0
the consequence is that all negative or null alpha values will be considered as opaque.
That means that there will be no alpha
value between 128 and 255, thus no opacity
value in the open interval ]0.5,1.0[.
That sounds weird. Maybe that's the true intention... Or maybe the color should have been interpreted unsigned when being printed to a string?
Or maybe there are 9 bits in alpha channel, but then it would be a very unusual format, and I don't see the point to waste half possible values for a encoding a single opacity value... It does not make good sense.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349418%2fjava-trouble-understanding-opacity-value-parsing-64-bit-long-to-float-and-ad%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If the color are represented on 32 bits, like your example seems to, it looks like it is encoded with an alpha channel and three color channels, each on eight bits (most probably ARGB).
Shifting >> 24
thus eliminates the RGB components, and keep alpha channels in low bits.
But beware of signed arithmetic! if original string is negative number, then fillColorValue
will be negative, and remain negative after arithmetic >>
shift...
With the guard on alpha > 0
the consequence is that all negative or null alpha values will be considered as opaque.
That means that there will be no alpha
value between 128 and 255, thus no opacity
value in the open interval ]0.5,1.0[.
That sounds weird. Maybe that's the true intention... Or maybe the color should have been interpreted unsigned when being printed to a string?
Or maybe there are 9 bits in alpha channel, but then it would be a very unusual format, and I don't see the point to waste half possible values for a encoding a single opacity value... It does not make good sense.
add a comment |
If the color are represented on 32 bits, like your example seems to, it looks like it is encoded with an alpha channel and three color channels, each on eight bits (most probably ARGB).
Shifting >> 24
thus eliminates the RGB components, and keep alpha channels in low bits.
But beware of signed arithmetic! if original string is negative number, then fillColorValue
will be negative, and remain negative after arithmetic >>
shift...
With the guard on alpha > 0
the consequence is that all negative or null alpha values will be considered as opaque.
That means that there will be no alpha
value between 128 and 255, thus no opacity
value in the open interval ]0.5,1.0[.
That sounds weird. Maybe that's the true intention... Or maybe the color should have been interpreted unsigned when being printed to a string?
Or maybe there are 9 bits in alpha channel, but then it would be a very unusual format, and I don't see the point to waste half possible values for a encoding a single opacity value... It does not make good sense.
add a comment |
If the color are represented on 32 bits, like your example seems to, it looks like it is encoded with an alpha channel and three color channels, each on eight bits (most probably ARGB).
Shifting >> 24
thus eliminates the RGB components, and keep alpha channels in low bits.
But beware of signed arithmetic! if original string is negative number, then fillColorValue
will be negative, and remain negative after arithmetic >>
shift...
With the guard on alpha > 0
the consequence is that all negative or null alpha values will be considered as opaque.
That means that there will be no alpha
value between 128 and 255, thus no opacity
value in the open interval ]0.5,1.0[.
That sounds weird. Maybe that's the true intention... Or maybe the color should have been interpreted unsigned when being printed to a string?
Or maybe there are 9 bits in alpha channel, but then it would be a very unusual format, and I don't see the point to waste half possible values for a encoding a single opacity value... It does not make good sense.
If the color are represented on 32 bits, like your example seems to, it looks like it is encoded with an alpha channel and three color channels, each on eight bits (most probably ARGB).
Shifting >> 24
thus eliminates the RGB components, and keep alpha channels in low bits.
But beware of signed arithmetic! if original string is negative number, then fillColorValue
will be negative, and remain negative after arithmetic >>
shift...
With the guard on alpha > 0
the consequence is that all negative or null alpha values will be considered as opaque.
That means that there will be no alpha
value between 128 and 255, thus no opacity
value in the open interval ]0.5,1.0[.
That sounds weird. Maybe that's the true intention... Or maybe the color should have been interpreted unsigned when being printed to a string?
Or maybe there are 9 bits in alpha channel, but then it would be a very unusual format, and I don't see the point to waste half possible values for a encoding a single opacity value... It does not make good sense.
edited Mar 26 at 21:34
answered Mar 26 at 21:24
aka.niceaka.nice
6,9671 gold badge22 silver badges36 bronze badges
6,9671 gold badge22 silver badges36 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349418%2fjava-trouble-understanding-opacity-value-parsing-64-bit-long-to-float-and-ad%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You seem to have a good handle on the bit-shifting, which seems like the more difficult concept. Are you unfamiliar with the ternary operator (` x ? p : q `), or are you asking why zero is being treated specially)
– erickson
Mar 26 at 3:45
1
Or maybe you are asking why a 64-bit
long
is used, shifting off 24 bits and apparently working with the remaining 40?– erickson
Mar 26 at 4:01
Do you know what color format is used? It can be assumed, that first 24 bit is RGB (8 bit per channel) and remaining 40 bit is alpha channel, but in such case the subsequent operations seems strange.
– Mikhail Ilinykh
Mar 26 at 4:19
1
In order to retrieve the integer Alpha channel value from the fillColorValue you need to right shift bits by 24. Not really sure why fillColorValue and alpha variables are declared a long when int would have sufficed. In the code you supplied, a Ternary is used to handle the chance that the Alpha value might be 0. Odd though since an Alpha value of 0 would normally mean full transparency but in this case the Ternary is enforcing the fact that any value less than 1 would place opacity to completely opaque (1.0f). There would of course be good reason for this in many different situations.
– DevilsHnd
Mar 26 at 8:27
Not being very familiar with alpha and int/long to float conversions the method didn't make much sense to me, and the intent behind the code wan't clear. as @DevilsHnd pointed out the ternary is weird, and in fact this was the issue... transparency was showing as completely white.
– Continuity8
Mar 27 at 4:10