Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pixel Transparency

 
Brendan Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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]
 
Fiorenza Oppici
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving thread as too difficult for "beginning".
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic