Get colour of single pixel in image

can some please help me!! I need to get the colour of a single pixel in an Image. Has to work basicly like eye dropper tool so when mouse is moving over image RGB values are displayed on label and colour can be changed to whatever is selected. can someone please show me a simple function that i could call on say mouseMove or mouseDragged event that grabs pixel colour at mouse co-ords and outputs rgb values. Thanks.
mac1416Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mac1416Author Commented:
i'm still not sure how to get the colour of pixel that mouse is over. can you give me an example of using it with mouse x,y co-ordinates and get the invidual rgb values. thanks. just want something that ultimatly works something like Color col = getColour(iX, iY)
0
TimYatesCommented:
Assuming your image is stored in an Image variable called im,

  public int getColorAt( Image im, int x, int y )
  {
    int iw = im.getWidth( parent ) ;
    int ih = im.getHeight( parent ) ;

    int[] imagearr = new int[ iw * ih ] ;

    PixelGrabber pg = new PixelGrabber( im, 0, 0, iw, ih, imagearr, 0, iw );
    try
    {
      pg.grabPixels();
    }
    catch (InterruptedException e)
    {
    }
    return imagearr[ x + ( y * iw ) ] ;
  }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

TimYatesCommented:
Obviously, if you do the pixel grab only once (when you load the image), then this will speed things up considerably ;-)
0
mac1416Author Commented:
How do I get the seperate RGB values from the int returned or what do I need to do get them?
0
TimYatesCommented:
if your color is stored in the variable:

int color ;

then;

int red = ( 0xFF0000 & color ) >> 16 ;
int gre = ( 0x00FF00 & color ) >> 8 ;
int blu = ( 0x0000FF & color ) ;

0
TimYatesCommented:
the int is 32 bit, and stores the values like this:

0xAARRGGBB

where A = alpha, r = red, g = green and b = blue
0
mac1416Author Commented:
How do I get the seperate RGB values from the int returned or what do I need to do get them?
0
mac1416Author Commented:
ok got it. thanks alot for your help.
0
mac1416Author Commented:
thanks alot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.