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 & HelpSyntax Questions › Accessing 1 color component
Page Index Toggle Pages: 1
Accessing 1 color component (Read 493 times)
Accessing 1 color component
Jun 7th, 2009, 8:05am
 
Hi, I've got a simple question, how can I access a single color component and put it into a variable? (int value for example).

Either in RGB or HSB. Each variable has 3 components. Actually, Id like to access B from HSB.

Thanx in advance.
Re: Accessing 1 color component
Reply #1 - Jun 7th, 2009, 8:53am
 
i use this convert from HSB to RGB : (java.awt.color.Color)
   rgb = new Color(Color.HSBtoRGB(fgH.v(),fgS.v(),fgB.v()));
   foregroundR = rgb.getRed();
   foregroundG = rgb.getGreen();
   foregroundB = rgb.getBlue();

the other way around :

 float[] hsb = Color.RGBtoHSB(r, g, b, null);

Re: Accessing 1 color component
Reply #2 - Jun 7th, 2009, 9:04am
 
Im not sure I fully understand your code :S. Still what I meant is, not converting from one to another, just something like:

Imagine I had:

color currColor = video.pixels[100];

(that would give me color in RGB , range from 0 to 255, of a specific pixel from the video Im working with)

Now I want to get JUST the red componente from that variable (currColor). And put into an int variable.
How could I get it?

It tried:

colorR = currColor.getRed();

after seeing you code, but that wont work, do you know how I can do it?

Thank you.
Re: Accessing 1 color component
Reply #3 - Jun 7th, 2009, 12:18pm
 
See the red(), green() and blue() functions.

If you need speed, you must use binary operators: & mask and >> shift.
Re: Accessing 1 color component
Reply #4 - Jun 7th, 2009, 2:04pm
 
PhiLho  wrote on Jun 7th, 2009, 12:18pm:
If you need speed, you must use binary operators: & mask and >> shift.


Shortcut to make it easy:

Code:

int r = img.pixels[pix] >> 16 & 0xFF;
int g = img.pixels[pix] >> 8 & 0xFF;
int b = img.pixels[pix] & 0xFF;


with the loadPixels() on img.
Re: Accessing 1 color component
Reply #5 - Jun 7th, 2009, 4:49pm
 
Those were easy operators haha. Dunno why I didnt find them :S I've even got a processing book with me xD (Im new at it) but I was looking in the wrong place...

Thanx a lot Smiley
Page Index Toggle Pages: 1