We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › How to obtain the RGB values
Page Index Toggle Pages: 1
How to obtain the RGB values (Read 660 times)
How to obtain the RGB values
May 23rd, 2008, 10:55am
 
Hello, I'm a student in a hight school of art, and I have started few month ago to learn to work with Processing.

So, in  the framework of my studies and to lead my own project I have realized a small program which read the value of a pixel according to the mouse X and Y position. The problem is that the values I get back are negatives numbers like
"-14212709" for example. I know what this value can represent and why it's negative BUT, I would like to get back the Red, the Green and the Blue values instead of these negatives numbers.

I don't know if it's possible but I hope so.
If someone had a part of the answer it would be a great help for me.

thanks for your attention.
Re: How to obtain the RGB values
Reply #1 - May 23rd, 2008, 11:49am
 
Hi, what you want to do is quite easy. Lets assume that the variable myColor contains the number.

For the individual components you can do:
int r = red(myColor);
int g = green(myColor);
int b = blue(myColor);

Or a more optimized way is:
int r = (myColor >> 16) & 255;
int g = (myColor >> 8) & 255;
int b = (myColor) & 255;

Re: How to obtain the RGB values
Reply #2 - May 23rd, 2008, 3:16pm
 
As memo said, although I am not sure that the second solution is more optimized. I prefer the clearer one, for sure...

The issue and solution are explained in the color page, the reason being that by default, colors are opaque, ie. have a 0xFF alpha value which is in the high bits of the word.
Re: How to obtain the RGB values
Reply #3 - May 23rd, 2008, 3:29pm
 
PhiLho  wrote on May 23rd, 2008, 3:16pm:
...I am not sure that the second solution is more optimized.


It is substantially quicker, since red() etc take into account the current colorMode() parameters and convert to the range supplied to that before returning the value, whereas accessing the pixels[] is direct and quicker.
Re: How to obtain the RGB values
Reply #4 - May 23rd, 2008, 4:39pm
 
If you will only need to do this once or twice per update frame (e.g. getting the color components of the one pixel under the mouse cursor every frame), then the performance loss from using the red(), green(), blue() functions will be negligable. However if you want to loop through every pixel in a 1024x768 image - i.e. 2.35 MILLION times a frame, then I advise you use the second method! (In that case i also advise that the int r, int g, int b variable declations be done OUTSIDE the for loop as well!)

Any time someone says a method is ever-so-slightly faster than another method, generally you don't need to use the ever-so-slightly faster method if you will only be doing it a few times every frame. Its when you have it in a massive for-loop or something that you need to start saving nano-seconds! (e.g. iterating pixels in an image)
Re: How to obtain the RGB values
Reply #5 - May 23rd, 2008, 4:48pm
 
Thanks to both for the additional information! Cheesy Good to know.

memo, did you really measured a difference of performance by putting the int declarations outside of the loop? I recently read several articles stating it should be invariant. Although it might depend on the JVM (Sun vs. others, 1.4 vs. 1.6, etc.).
I will try and experiment myself.

[UPDATE] Ah, it is documented in the red() page, for example... Wink Now to see what is colorMode...
Re: How to obtain the RGB values
Reply #6 - May 23rd, 2008, 5:08pm
 
To be honest Philho, I haven't measured the difference myself!
Its just a habit I've had for many many years, and an optimization tip which all programmers will recommend whether its for C, C++, C#, Java, ActionScript etc. It is true that some compiler/interpreter options can make it irrelevant by creating the variable only once outside the for-loop as if thats what you coded, but I think it still is good practice  to bear in mind when performance really is an issue...

Re: How to obtain the RGB values
Reply #7 - May 23rd, 2008, 5:29pm
 
Ah, OK. I will just say that optimizations for C, for example, can be very different from those for Java: one produces native code with an optimized compiler while the other produces bytecode run by a virtual machine. And memory management is very different.

Likewise, in C it is overkill to use for (i = 0; i < strlen(someString); i++) while in Java for (i = 0; i < someString.length(); i++) is OK. Etc.

I saw arguments pro or against declaring variables in loops, but it seems that in Java it is quite safe (no memory allocation/deallocation, actually generated bytecode is identical) and I adhere to the pro camp arguing that it has the advantage of limiting the scope of these variables. Coming from the C world, I was initially in the against camp... Cheesy
Re: How to obtain the RGB values
Reply #8 - May 23rd, 2008, 5:51pm
 
Hey Philho, yea like I said, I haven't measured the performance difference myself so can't comment on the nessecity of keeping the vars outside. But I do copy/paste code between C++/AS3/Java quite often so like to keep it as compatible as possible, so will probably carry on keeping it outside. As long as its in a function or other block the scope is still limited - I am quite intrigued that the bytecode is identical, I gotta look into that.

Though I must say, every Java optimization page I've read gives the specific example that for (i = 0; i < someString.length(); i++) is bad because someString.length() is executed every frame. In fact they generally go as far as to say even not to use for (i = 0; i < myObject.customProperty; i++) (in this case customProperty is a property NOT a method), and to cache object properties as well. Memory management may be quite different in C and Java (and AS3), but thats a bit irrelevant when it comes to compiling such code. A function call, or accessing an object property, or multi-dimensional array etc. will always have overheads compared to a local or static variable...

For others reading this thread confused as to what to do - like I said in my first post, all of these are negligable if they will be done a few times a frame. Its only when you are doing them millions of times per frame does it start to make a difference.
Re: How to obtain the RGB values
Reply #9 - May 23rd, 2008, 7:02pm
 
Final words (for general public): don't optimize if it runs fine (ie. at the speed you need); and measure your optimizations: sometime, common sense suggest an improvement, perhaps at the cost of readability, but it is worth only if it really improves speed (or memory in some cases) in a significant way. And make incremental improvements, to see what is really efficient.
I wonder if one have already used a profiler in a Processing sketch. Perhaps just doing time measurement is enough.

Some people might see these (generic, true for all programming languages) advices an overkill for an environment designed for quick sketches, but as you point out, memo, some parts of code are critical, and after all most of the time the source is public, so readability is important (for me, at least).

I fear I went quite off topic...
Page Index Toggle Pages: 1