?
Solved

Get colour of single pixel in image

Posted on 2003-03-15
10
Medium Priority
?
391 Views
Last Modified: 2008-02-01
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
Comment
Question by:mac1416
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8142736
0
 

Author Comment

by:mac1416
ID: 8142754
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
 
LVL 35

Accepted Solution

by:
TimYates earned 120 total points
ID: 8142912
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
Industry Leaders: 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!

 
LVL 35

Expert Comment

by:TimYates
ID: 8142916
Obviously, if you do the pixel grab only once (when you load the image), then this will speed things up considerably ;-)
0
 

Author Comment

by:mac1416
ID: 8144961
How do I get the seperate RGB values from the int returned or what do I need to do get them?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8144978
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8144981
the int is 32 bit, and stores the values like this:

0xAARRGGBB

where A = alpha, r = red, g = green and b = blue
0
 

Author Comment

by:mac1416
ID: 8145012
How do I get the seperate RGB values from the int returned or what do I need to do get them?
0
 

Author Comment

by:mac1416
ID: 8145164
ok got it. thanks alot for your help.
0
 

Author Comment

by:mac1416
ID: 8145167
thanks alot!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question