[cairo] Getting wrong cairo pixel color.

Bill Spitzak spitzak at gmail.com
Mon Mar 28 10:44:45 PDT 2011



Monil Parmar wrote:
> Hi,
>  
> Iam using cairo in my application to set and get the color.
> Values which Iam getting for particular pixel is not exactly same which 
> we are setting, it is nearabout of that value.
> I made a sample in which, For example if we are setting the (r=10, b=20, 
> g=30, a=40) to the pixel,  when I tried to get the color value from that 
> pixel. 
> Iam getting following values in return (r=6, b=19, g=26, a=40).

When you set the color it multiplies it by the alpha and it does floor 
(rather than round). Floor is pretty much a requirement to make cairo 
match other graphics libraries and to avoid some numeric problems. This 
produces:

	r = floor(10*40/255) = floor(1.56...) = 1
	g = floor(20*40/255) = floor(3.13...) = 3
	b = floor(30*40/255) = floor(4.70...) = 4

When you read the values back you then multiply by 255/40 to undo the 
premultiply and round to the nearest multiple of 255 resulting in:

	r = round(1*255/40) = round(6.375) =  6
	g = round(3*255/40) = round(19.125)= 19
	b = round(4*255/40) = round(25.5)  = 26 (round to even)


More information about the cairo mailing list