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.
IndexDiscussionGeneral Discussion,  Status › Color Selector HELP
Page Index Toggle Pages: 1
Color Selector HELP (Read 2430 times)
Color Selector HELP
Apr 10th, 2010, 2:07pm
 
Hi, how can i get the RGB component from color selector tool.
When i click the mouse on the color of color selector, i want to get from code the red, green and blue component.
Can anyone help me?
Re: Color Selector HELP
Reply #1 - Apr 10th, 2010, 3:28pm
 
I dont get your question. If you use the color selector and choose a color. you see the R: G: B: values on the right side.
if this is not what you want, please describe your question a bit clearer.
Re: Color Selector HELP
Reply #2 - Apr 10th, 2010, 4:19pm
 
Sorry...
I want to write a code to get the value of red green and blue of color cicked on color selector
Code:

draw()
{
   int red = getRedComponentFromColorSelector...
   int green = getGreenComponentFromColorSelector...
   int red = getBlueComponentFromColorSelector...
}
Re: Color Selector HELP
Reply #3 - Apr 11th, 2010, 1:12am
 
The color selector is part of the IDE, so it cannot be called from a Processing sketch, even less outside of the IDE.
Either you call the AWT or Swing color selector (with precautions described elsewhere for the file chooser, to avoid freezing the sketch) or you make your own using one of the existing GUI libraries (G4P has nice sliders...).
Re: Color Selector HELP
Reply #4 - Apr 11th, 2010, 4:52am
 
ah ok, i get it.
Yeah like PhiLho said, its not possible.
But if you want to build your own, there are several examples on open processing. or take a look at this code. Dont know where i got it from but i saved it as i thought it might be useful.

Code:
ColorPicker cp;

void setup()
{
size( 500, 500 );
frameRate( 100 );

cp = new ColorPicker( 10, 10, 400, 400, 255 );
}

void draw ()
{
background( 80 );
cp.render();
}

public class ColorPicker
{
int x, y, w, h, c;
PImage cpImage;

public ColorPicker ( int x, int y, int w, int h, int c )
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.c = c;

cpImage = new PImage( w, h );

init();
}

private void init ()
{
// draw color.
int cw = w - 60;
for( int i=0; i<cw; i++ )
{
float nColorPercent = i / (float)cw;
float rad = (-360 * nColorPercent) * (PI / 180);
int nR = (int)(cos(rad) * 127 + 128) << 16;
int nG = (int)(cos(rad + 2 * PI / 3) * 127 + 128) << 8;
int nB = (int)(Math.cos(rad + 4 * PI / 3) * 127 + 128);
int nColor = nR | nG | nB;

setGradient( i, 0, 1, h/2, 0xFFFFFF, nColor );
setGradient( i, (h/2), 1, h/2, nColor, 0x000000 );
}

// draw black/white.
drawRect( cw, 0, 30, h/2, 0xFFFFFF );
drawRect( cw, h/2, 30, h/2, 0 );

// draw grey scale.
for( int j=0; j<h; j++ )
{
int g = 255 - (int)(j/(float)(h-1) * 255 );
drawRect( w-30, j, 30, 1, color( g, g, g ) );
}
}

private void setGradient(int x, int y, float w, float h, int c1, int c2 )
{
float deltaR = red(c2) - red(c1);
float deltaG = green(c2) - green(c1);
float deltaB = blue(c2) - blue(c1);

for (int j = y; j<(y+h); j++)
{
int c = color( red(c1)+(j-y)*(deltaR/h), green(c1)+(j-y)*(deltaG/h), blue(c1)+(j-y)*(deltaB/h) );
cpImage.set( x, j, c );
}
}

private void drawRect( int rx, int ry, int rw, int rh, int rc )
{
for(int i=rx; i<rx+rw; i++)
{
for(int j=ry; j<ry+rh; j++)
{
cpImage.set( i, j, rc );
}
}
}

public void render ()
{
image( cpImage, x, y );
if( mousePressed &&
mouseX >= x &&
mouseX < x + w &&
mouseY >= y &&
mouseY < y + h )
{
c = get( mouseX, mouseY );
}
fill( c );
rect( x, y+h+10, 20, 20 );
}
}
Re: Color Selector HELP
Reply #5 - Apr 12th, 2010, 2:12pm
 
THANKS  Smiley
Re: Color Selector HELP
Reply #6 - Apr 12th, 2010, 3:41pm
 
There's also this if you want more sophisticated control over colours.
Re: Color Selector HELP
Reply #7 - Apr 12th, 2010, 4:40pm
 
looks good. does it include a colorpicker ?
Re: Color Selector HELP
Reply #8 - Apr 13th, 2010, 5:12am
 
Yes - see the ColourPickerExample sketch in the 'examples' folder of the library.

Jo.
Page Index Toggle Pages: 1