下面是源码 需要浏览器支持html5 复制代码代码如下: <br />if(document.createElement("canvas")){ <br />if(document.getElementById("colorPicker").getContext){ <br />var can = document.getElementById("colorPicker"); <br />can.setAttribute("height",300); <br />var cxt = can.getContext("2d"); <br />var gradient = cxt.createLinearGradient(0.5,0.5,0,150); <br />gradient.addColorStop(0,'#00ff00'); <br />gradient.addColorStop(1,'#ff0000'); <br />cxt.fillStyle=gradient; <br />cxt.fillRect(0,0,60,200); <br />var ox= can.offsetLeft <br />var oy = can.offsetTop; <br />var span = document.createElement("input"); <br />span.setAttribute("id","rgba"); <br />document.getElementById("textResult").appendChild(span); <br />} <br />} <br />function showCurrentColor(e){ <br />var x = e.clientX - 8; <br />var y = e.clientY - 29; <br />var w = 10; <br />if(document.createElement("canvas")){ <br />if(document.getElementById("colorPicker").getContext){ <br />var can = document.getElementById("colorPicker"); <br />var cxt = can.getContext("2d"); <br />var gradient = cxt.createLinearGradient(0.5,0.5,0,150); <br />var span = document.getElementById("rgba"); <br />var imgDatas = cxt.getImageData(ox,oy,10,200); <br />var imgData = imgDatas.data; <br />var g = imgData[4 *(w)*(y)+(x)*4 + 1]; <br />var r = imgData[4 *(w)*(y)+(x)*4]; <br />var b = imgData[4 *(w)*(y)+(x)*4 + 2]; <br />var a = imgData[4 *(w)*(y)+(x)*4 + 3]; <br />span.value="r="+r+" g="+ g+" b="+b +" a="+a; <br />document.getElementById("textResult").appendChild(span); <br />} <br />} <br />} <br />