简体   繁体   English

HTML5 Canvas绘制的颜色与提供的颜色不同

[英]HTML5 Canvas drawing pixels with different color than the one provided

After setting a certain colour as the fillStyle of the canvas and drawing a rectangle with fillRect , the colour of the rectangle sometimes differs a bit from the one provided (the getImageData returns different values - usually one of them is lower by 1). 将某种颜色设置为画布的fillStyle并使用fillRect绘制一个矩形后,矩形的颜色有时会与提供的颜色略有不同( getImageData返回不同的值 - 通常其中一个值低1)。 It seems to happen only when using rgba colours (and not with rgb ) but I actually do need to use the alpha channel. 它似乎只发生在使用rgba颜色时(而不是rgb ),但实际上我确实需要使用alpha通道。

I've made a simple test suite on js fiddle for anyone who would like to look into this issue: http://jsfiddle.net/LaPdP/1/ 对于想要研究这个问题的人,我为js小提琴制作了一个简单的测试套件: http//jsfiddle.net/LaPdP/1/

Any ideas on why is this happening and if there is any way to fix that? 关于为什么会发生这种情况的任何想法,以及是否有任何方法可以解决这个问题? If it at least happened always on the same value then I'd just bypass it by increasing it by 1, but it seems quite random to me. 如果它至少总是发生在相同的值上,那么我只是通过将其增加1来绕过它,但对我来说它似乎很随机。

Update from 2017: I forgot completely about this answer, but the cause is related to pre-multiplying the data, when getting/setting. 2017年更新:我完全忘记了这个答案,但原因与获取/设置时预先倍增数据有关。 As the numbers in a bitmap is always integer there will be rounding errors as the natural result of pre-multiplying often result in non-integer numbers. 由于位图中的数字总是整数,因此预乘会的自然结果通常会导致非整数,因此会出现舍入误差。

There is unfortunately no convenient way to fix this. 遗憾的是,没有方便的方法来解决这个问题。

Just to clarify about gamma below: Gamma (via a gamma setting or an ICC profile) will affect images directly, but for shapes drawn directly to the canvas this should not be a problem per-se as only the display gamma is adjusted on top, not the data itself. 只是为了澄清下面的伽玛:Gamma(通过伽玛设置或ICC配置文件)将直接影响图像 ,但对于直接绘制到画布的形状,这不应该是一个问题本身,因为只有显示器伽玛在顶部调整,不是数据本身。

Old answer: 老答案:

What you are experiencing is probably a result of only a partial implementation of the color and gamma correction section in the canvas standard. 您遇到的可能是仅在画布标准中部分实现颜色和伽马校正部分的结果。

The reason for various color values, at least when it comes to images containing ICC profiles, is due to the built-in color and gamma correction in the browsers: 各种颜色值的原因,至少在涉及包含ICC配置文件的图像时,是由于浏览器中的内置颜色和伽马校正:

4.8.11.1 Color spaces and color correction 4.8.11.1色彩空间和色彩校正

The canvas APIs must perform color correction at only two points: when rendering images with their own gamma correction and color space information onto the canvas, to convert the image to the color space used by the canvas (eg using the 2D Context's drawImage() method with an HTMLImageElement object), and when rendering the actual canvas bitmap to the output device. 画布API必须仅在两点执行颜色校正:将具有自己的伽马校正和颜色空间信息的图像渲染到画布上时,将图像转换为画布使用的颜色空间(例如,使用2D上下文的drawImage()方法使用HTMLImageElement对象),并将实际的画布位图渲染到输出设备。

Source: w3.org 资料来源: w3.org

However, it also states in section 4.8.11.1: 但是,它也在第4.8.11.1节中说明:

Note: Thus, in the 2D context, colors used to draw shapes onto the canvas will exactly match colors obtained through the getImageData() method. 注意:因此,在2D上下文中,用于在画布上绘制形状的颜色将与通过getImageData()方法获得的颜色完全匹配。

As the status as this is written is a work in progress my guess is that the browsers has a "lazy" implementation of color and gamma correction which currently also affects shapes - or - all color information from the canvas is corrected to display profile as the latter point in first quote. 由于写入的状态是work in progresswork in progress我的猜测是浏览器具有颜色和伽马校正的“懒惰”实现,其当前也影响形状 - 或者 - 来自画布的所有颜色信息被校正为显示简档作为第一个引用的后一点。 This will perhaps not change until the standard becomes final. 在标准成为最终标准之前,这可能不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM