admin管理员组文章数量:1355679
I want to know how to make a color picker with jQuery that will allow you to click somewhere on the page and return the hex color value of the color that you clicked on.
I know that it is possible with either javascript or jquery as not only do they have lots of color picker plugins, but I have and extension for Chrome that has that same exact functionality.
Any ideas?
I want to know how to make a color picker with jQuery that will allow you to click somewhere on the page and return the hex color value of the color that you clicked on.
I know that it is possible with either javascript or jquery as not only do they have lots of color picker plugins, but I have and extension for Chrome that has that same exact functionality.
Any ideas?
Share Improve this question asked Oct 7, 2011 at 16:47 watzonwatzon 2,5593 gold badges36 silver badges66 bronze badges 3- Related: stackoverflow./questions/1740700/… – Colin Brock Commented Oct 7, 2011 at 16:50
- but what if the item you click on doesn't have a background color? Or is an image? – watzon Commented Oct 7, 2011 at 16:51
- Admittedly, the question I linked to doesn't provide everything you're looking for, but I thought it was relevant enough to add as a ment. – Colin Brock Commented Oct 7, 2011 at 17:09
3 Answers
Reset to default 5Bind a global click
or mouseup
event listener. Then, use canvas
to obtain the pixel information. The pixel positions can be retrieved through the event
object (event.pageX
, event.pageY
).
See below for an example, which should work in future versions of FireFox. Currently, for security reasons, the drawWindow
method is disabled for web pages. It should work in extensions, though. If you're truly interested, see the links for the similar methods in Chrome, Safari and Internet Explorer.
var canvas = $("<canvas>"); //Create the canvas element
//Create a layer which overlaps the whole window
canvas.css({position:"fixed", top:"0", left:"0",
width:"100%", height:"100%", "z-index":9001});
//Add an event listener to the canvas element
canvas.click(function(ev){
var x = ev.pageX, y = ev.pageY;
var canvas = this.getContext("2d");
canvas.drawWindow(window, x, y, 1, 1, "transparent");
var data = canvas.getImageData(0, 0, 1, 1).data;
var hex = rgb2hex(data[0], data[1], data[2]);
alert(hex);
$(this).remove();
});
canvas.appendTo("body:first"); //:first, in case of multiple <body> tags (hmm?)
//Functions used for conversion from RGB to HEX
function rgb2hex(R,G,B){return num2hex(R)+num2hex(G)+num2hex(B);}
function num2hex(n){
if (!n || !parseInt(n)) return "00";
n = Math.max(0,Math.floor(Math.round(n),255)).toString(16);
return n.length == 1 ? "0"+n : n;
}
References
- Canvas examples - Learn more about
canvas
- drawWindow - FireFox method
- visibleContentAsDataURL - Safari extensions
- chrome.tabs.captureVisibleTab - Chrome extensions
- HTA ActiveX control - Internet Explorer
Those plugins don't work by knowing the color of the pixel under the mouse; they work because the colors in the picker are laid out according to a mathematical formula, and by knowing that formula and where you clicked the mouse, the plugin can find out what color belongs there. JavaScript doesn't give you any way to get an image of the page or the "color under the cursor".
I had this problem recently, by default IE returns hex colours whereas all the good browsers return rgb values, I simply put conditions to deal with both strings, this would be more efficient..
however I think this function does the trick if you really need it
function RGBToHex(rgb) {
var char = "0123456789ABCDEF";
return String(char.charAt(Math.floor(rgb / 16))) + String(char.charAt(rgb - (Math.floor(rgb / 16) * 16)));
}
本文标签: javascriptGet hex value of clicked on color with jQueryStack Overflow
版权声明:本文标题:javascript - Get hex value of clicked on color with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744035696a2579730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论