Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

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.
0
mac1416
Asked:
mac1416
  • 5
  • 4
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now