Skip to main content
gamedev.net
🔒 Locked

[java] grayscale to rgb

Started by nataku92 6 replies 1.2k views
Original Post
nataku92
nataku92
I'm making a simple particle engine and I'm using an image for the particles (since drawing primitives is too slow). The image is grayscale and I want to be able to change it to a certain shade for each particle. Also, since each particle has a different color, I'm looking for an efficient way to do this without slowing down the game too much. Thanks
nataku92
nataku92
Thanks for the reply, but could you maybe give a small code sample. Isn't multiplicative blending when you mix 2 images together (correct me if I'm wrong)? Each particle has a random color so I want to directly shade the grayscale image.

Thanks
Darragh
Darragh
Ok, just a small example..

Say if your colors range from 0-255, and your grayscale image is 200. Grayscale images are monochromotic (RGB values are equal) so its RGB values are 200,200,200. Now, if you want a red tint to that multiply it by a red RGB value like 255,0,0.

So your output value will be:

Red 200x255 = 200
Green 200x0 = 0
Blue 200x0 = 0

So now your particle is red. Just multiply every RGB value in the particle by the color you want and it will tint it to that color.

Hope this helps,
Darragh
swordfish
swordfish
If you're using OpenGL, then you'll want to look at alpha blending and material states. Calls like glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA) allow you to define how to 'combine' an incoming pixel with an existing pixel. Then depending on if you are using lighting or not, you'll use either glColor() or glMaterial() to define the color of the textured polygons.
http://blog.protonovus.com/
CJM
CJM
Hey,

Is there anything in particular wrong with storing colour information with your vertices? I mean, if you're drawing in immediate mode, then [pseudo]code would be something like:

glBegin(GL_RECTS)for each particle i  glColor(particles.colour.r, .g, .b)  glVertex() callsnextglEnd()


If you're using vertex arrays, then you just set the colour array to the colours you're using [but you need multiple copies of the same colour (1 for each vertex)]. These are not slow operations.

But, if for some reason you're already using your glColor, perhaps you may want to look into stuff related to GL_CONSTANT_COLOR. This isn't particularly fast AFAIR though.

Or, am I completely missing what you're wanting to do?

--CJM
nataku92
nataku92
Thanks for the help. So I have to go through every pixel in the image and change the rgb values? And btw, I'm not using opengl.

Thanks
Darragh
Darragh
Quote:
Original post by nataku92
Thanks for the help. So I have to go through every pixel in the image and change the rgb values? And btw, I'm not using opengl.

Thanks


Yep, pretty much. If you were using OpenGL it would simply be a case of doing a glColor3f(..yourColor..) statement at each vertex of the rectangle, but since you're using software you will probably have to do it yourself.

Perhaps there are some functions in Java2D for doing something like this, though I doubt it. Have a look through the JDK docs anyways and see if you can find something. Sorry I can't offer any more help than this, but I'm mostly using OpenGL myself and haven't really touched upon Java2D much...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.