This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Pixel Transparency

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a program that allows me to edit raw pixel data on an image, but am having issues with transparency. Currently, in order to change a pixel's color, I use this method. It works fine and sets the pixel color correctly, but does not account for alpha.

I've attempted to implement alpha/transparency, but have not been successful. My current version of the method is as follows.

I'm not exactly sure what to do with the new color, old color, and alpha value in order to get the new color. Any ideas?

[If you need any more information, feel free to ask]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )
 
Brendan Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fiorenza Oppici wrote:so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )



My question is how would I go about getting the value of the new color. I have no problem actually setting the pixel color and then drawing the image to the screen, I just don't know how to implement transparency into the pixels.
 
Fiorenza Oppici
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Jones wrote:

Fiorenza Oppici wrote:so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )



My question is how would I go about getting the value of the new color. I have no problem actually setting the pixel color and then drawing the image to the screen, I just don't know how to implement transparency into the pixels.


as far as i know the int oldColor contains the info for red component in bits 16-23, for green component in bits 8-15 and for the blue one in bits 0-7, as the Javadoc says.
so, you can get from that single int three other ints: red, green, and blue, just truncating the bits and then shift dividing each chunk for the right weight. then you can instanciate the Color e.g "newcolor" with the constructor new Color (int red, int green, int blue, int alpha).

all this work can be done simply calling the Color methods getGreen(), getRed() e getBlue(). I'm not comfortable with shifting so I'd go for this option.
but you already used shifting and bitwise operations in your code, so you can do the first way.

maybe someone can be more explicative on the exact code for converting the int into separate components not using the "get" methods; I found some examples on the web but I never suggest something it's not my own code... however, I hope i've been helpful.
 
Brendan Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fiorenza Oppici wrote:

Brendan Jones wrote:

Fiorenza Oppici wrote:so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )



My question is how would I go about getting the value of the new color. I have no problem actually setting the pixel color and then drawing the image to the screen, I just don't know how to implement transparency into the pixels.


as far as i know the int oldColor contains the info for red component in bits 16-23, for green component in bits 8-15 and for the blue ones in bits 0-7, as the Javadoc says.
so, you can get from that single int three other ints red, green, and blue truncating the bits and then shift divide each chunk with the right weight, so then you can instanciate the Color e.g "newcolor" with the constructor (int red, int g, int b, int alpha).

all this work can be done simply calling the Color methods getGreen(), getRed() e getBlue(). I'm not comfortable with shifting so I'd go for this option.
but you already used shifting and bitwise operations in your below code, so you can do the first way.

maybe someone can be more explicative on the exact code for converting the int into separate components, i found some examples on the web but I never suggest something it's not my own code... however, I hope i've been hepful.



blue = bits 0 - 7
green = bits 8-15
red = bits 16-23
alpha = bits 24-31

I know how the color data is stored, so my issue isn't getting the RGBA values of the colors (That's quite simple).

Say, for example, I had a black background and wrote 'Transparency' in white letters on it. Now, if I were to draw a transparent red rectangle over it, you would be able to see the letters through the red. While if it were fully opaque, the red would cover everything (Black & White) up and you would see nothing but a red rectangle.

What I need to know is how I'd calculate what color I should actually paint the pixel. If I were to set the color to a transparent value, it would just be that color (It won't 'blend' with the colors below it, it would just overwrite them).

-----------------------------------

Edit: Nevermind, after an hour or so of searching on Google, I managed to find a formula for calculating colors. For anyone that was wondering, it looks like this.



Note: All of the float values must be between the value of 0.0 and 1.0.

Hope this helps someone
 
Marshal
Posts: 79987
399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving thread as too difficult for "beginning".
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erm... did you try searching for alpha blending implementations? There are a lot of examples.
 
Fiorenza Oppici
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Brendan, I didn't get that overwriting colors was not your intention.
Happy that you found a possible solution, it could be useful in future.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic