更改 Html5 Canvas 元素的颜色深度
我想知道是否有一种方法可以改变 HTML5 Canvas 元素中图像的颜色深度,例如,图像中每个像素的颜色将“四舍五入”到较小颜色位深度中最接近的等效值。谢谢。
I was wondering if there was a way to alter the color depth of the image in an HTML5 Canvas Element, in that the color of each pixel in the image will get "rounded" to the nearest equivalent in a lesser color bit depth, for instance. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是可以做到的,而且并不是太难。在这里查看我的答案:如何使用渐变贴图为 HTML5 画布和画布中的图像调色。
就像着色一样,您所要做的就是检查每个像素并更改RGB 值较小(步骤 8 或 16,而不是步骤 1)
因此,对于 8 个步骤,您可以执行以下操作:
哪个应该有效,但您应该检查边缘情况。
示例:
http://jsfiddle.net/gbBz7/
Yes it can be done, and isn't too difficult. See my answer here: How can I use a gradient map to tone a HTML5 canvas with an image in the canvas.
Just like in tinting, simply all you must do is go over each pixel and change the RGB values to be lesser (steps of 8 or 16 instead of steps of 1)
So for 8 steps you could do:
Which ought to work, but you should check the edge cases.
Example:
http://jsfiddle.net/gbBz7/
您可以使用 getImageData 迭代每种颜色的每个字节,因此您可以对其应用一些数学函数来表示 8 位颜色:http://jsfiddle.net/pimvdb/eGjak/191/。
You can iterate through each byte of each color with
getImageData
, so you could apply some math functions to it to e.g. represent 8-bit colors: http://jsfiddle.net/pimvdb/eGjak/191/.我认为没有内置的设置。但是您应该能够将每个像素通道值减少到一组更有限的值。像这样的东西:
I don't think there is built in setting for this. But you should be able to reduce each pixel channel value to a more limited set of values. Something like: