yahh
yahh

Reputation: 1195

Mouse shows color

i am trying to make an application which would show which color my mouse is pointing to, i dont mean in my own application but anywhere in windows on any screen, kind of like a tag beside my mouse pointer which shows the exact color.

example

I am a Java developer but i dont think this could be done in java i am thinking maybe i need some sort of script but i have no idea any help would be really appriciated

Upvotes: 1

Views: 617

Answers (3)

jumperchen
jumperchen

Reputation: 1473

Here is the runnable example,

import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
public class Main {

public static String getHexString(int rgb) {
    String hexString = Integer.toHexString(rgb);
    hexString = hexString.length() > 1 ? hexString : "0" + hexString;
    return hexString;
}

public static void main(String[] a) throws AWTException {
    Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
    Color color = new Robot().getPixelColor(mouseLocation.x,
            mouseLocation.y);
    System.out.println(getHexString(color.getRed())
            + getHexString(color.getGreen())
            + getHexString(color.getBlue()));
}

}

Upvotes: 2

Daniel Rikowski
Daniel Rikowski

Reputation: 72544

The solution consists of two parts:

Part 1: Retrieving the color:

Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
Color color = new Robot().getPixelColor(mouseLocation.x, mouseLocation.y);

Part 2: Getting the color name:

You can get a list of many colors and their names from Wikipedia's List of colors. You can create a mapping in Java given the data on Wikipedia.

Perhaps you can start with a few colors, and provide a generic hex representation for unknown colors, for example #rrggbb.

Upvotes: 4

Alex K.
Alex K.

Reputation: 176016

Take your pick: http://rosettacode.org/wiki/Color_of_a_screen_pixel

There is a Java/AWT example, an AutoHotKey is a simple scripted option.

The second C example shows the 3 API calls you need GetDC/GetCursorPos/GetPixel and their support code, these can be used from most languages that compile for windows.

Upvotes: 1

Related Questions