Jump to content
xisto Community
Sign in to follow this  
p_a

Get Pixel's Color

Recommended Posts

How to get a pixel's color from any certain pixel at the screen, using Javascript, dhtml, any client-side scripting language?For example how to get pixel's color for 10,10 pixel?

Share this post


Link to post
Share on other sites

My friend made a version of MSPaint in Java for a project in our Computer Science class, but couldn't make a filler because we determined that there was no plausible way to determine a pixel's color. That, the fact that JavaScript is generally weaker than Java, and the fact that pixels on the client's machine would probably not be on the page where the script is contained would lead me to believe that it's not possible. JS always manages to surprise me, though, so I guess you could keep looking. Just don't get your hopes up.

Share this post


Link to post
Share on other sites

hi p_a,I dont think either javascript/vbscript have such a feature incorporated into them. But Java would surely have that feature, which beeseven says to not having found. Even C/C++ have the feature to get the color of the pixed under the mouse cursor. So I do not think that Java would do without such a feature, esp Swing.Cheers.

Share this post


Link to post
Share on other sites

Thanks all! I still believe that there should be a way to get pixel's color using client side scripting like Javascript or VBScript. In my web application I even do not need to have the color under mouse pointer, but just the color at certain position, like 10,10 as I mentioned above. I do not know if PHP or Perl can do it (both are server scripting languages) but I am sure that Java can do it. The only problem is that I really want to avoid Java, not because it is bad, but because of - I already have PHP+Javascript in my application, and Java is sometimes slow to start itself into memory, especially on older PCs. I had really bad experience with JSP, I have even kind of strange anxiety using JSP, cause when I transfered my applications from JSP to PHP I got them work much, much faster and more reliable. Somebody would say I should just make better JSP setings on my web server, but, with PHP everything worked amazingly smooth immidiately.I hope that I'll find a way to get pixel color without using Java.

Share this post


Link to post
Share on other sites

well what i do when i'm trying to get a pixel's color is i press the print screen button on the keyboard, open up photoshop, paste it, zoom in to where the pixels are visible, and use the eyedropper tool to click on the pixel i want the color of. the color goes to foreground color box so click on that twice. a color picker box comes up and at the bottom of it is # beside a textbox. in that textbox is the code of that color so copy and paste that to whereever you need it and there ya go!

Share this post


Link to post
Share on other sites

Is it possible to use a java applet to find out the color of a pixel on a web page?

Did you read two posts up?
Maybe three...

Share this post


Link to post
Share on other sites

How to get a pixel's color from any certain pixel at the screen, using Javascript, dhtml, any client-side scripting language?

 

For example how to get pixel's color for 10,10 pixel?

 


 

 

 

 

Merging various references found here in StackOverflow and in other sites, I did so using javascript and JQuery:

<html>

<body>

<canvas id="myCanvas" width="400" height="400" style="border:1px solid #c3c3c3;">

Your browser does not support the canvas element.

</canvas>

<script src="jquery.js"></script>

<script type="text/javascript">

window.onload = function(){

var canvas = document.getElementById('myCanvas');

var context = canvas.getContext('2d');

var img = new Image();

img.src = 'photo_apple.jpg';

context.drawImage(img, 0, 0);

};

 

function findPos(obj){

var current_left = 0, current_top = 0;

if (obj.offsetParent){

do{

current_left += obj.offsetLeft;

current_top += obj.offsetTop;

}while(obj = obj.offsetParent);

return {x: current_left, y: current_top};

}

return undefined;

}

 

function rgbToHex(r, g, B){

if (r > 255 || g > 255 || b > 255)

throw "Invalid color component";

return ((r << 16) | (g << 8) | B).toString(16);

}

 

$('#myCanvas').click(function(e){

var position = findPos(this);

var x = e.pageX - position.x;

var y = e.pageY - position.y;

var coordinate = "x=" + x + ", y=" + y;

var canvas = this.getContext('2d');

var p = canvas.getImageData(x, y, 1, 1).data;

var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);

alert("HEX: " + hex);

});

</script>

<img src="photo_apple.jpg"/>

</body>

</html>

 

This is my complete solution.. Here I only used canvas and one image, but if you need to use <map>over the image, it's possible too. I hope I have helped, any problems, please contact me at my e-mail (ebfjunior@gmail.com).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.