admin管理员组文章数量:1322526
I have a list of colours (created from values) and want to display them in a colourbar (as a legend what each colour means). Something like this.
One way would be a table with 1 row / n columns (n = 25-100), each column representing one colour. Is there any better way to do this?
I have a list of colours (created from values) and want to display them in a colourbar (as a legend what each colour means). Something like this.
One way would be a table with 1 row / n columns (n = 25-100), each column representing one colour. Is there any better way to do this?
Share Improve this question edited May 23, 2017 at 11:48 CommunityBot 11 silver badge asked Aug 28, 2011 at 13:30 Horst WalterHorst Walter 14.1k34 gold badges137 silver badges239 bronze badges 2- Use an image, way better than tables. The position of the mouse pointer hover the image will determine wich color you're dealing with. – yoda Commented Aug 28, 2011 at 13:36
- I would need to create the image dynamically. I have the colour table, which is dynamically created. – Horst Walter Commented Aug 28, 2011 at 13:57
1 Answer
Reset to default 8Canvas is a powerful API for this: http://jsfiddle/pimvdb/eGjak/89/.
var cv = document.getElementById('cv'),
ctx = cv.getContext('2d');
for(var i = 0; i <= 255; i++) { // fill strokes
ctx.beginPath();
var color = 'rgb(100, ' + i + ', ' + i + ')';
ctx.fillStyle = color;
ctx.fillRect(i * 2, 0, 2, 50);
}
cv.onclick = function(e) {
var x = e.offsetX, // mouse x
y = e.offsetY, // mouse y
p = ctx.getImageData(x, y, 1, 1),
x = p.data; // pixel at mouse (x, y) - contains [r, g, b, a]
alert('Color: rgb(' + x[0] + ', ' + x[1] + ', ' + x[2] + ')');
};
本文标签: htmlJavascript colorbarbest way to create itStack Overflow
版权声明:本文标题:html - Javascript colorbar - best way to create it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742110764a2421245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论