What is the minimal difference in RGB color values which Mathematica renders and exports as different colors?
Asked Answered
A

3

5

I was amazed when I found that Mathematica gives True for the following code (on 32 bit Windows XP with Mathematica 8.0.1):

Rasterize[Graphics[{RGBColor[0, 0, 0], Disk[]}]] === 
 Rasterize[Graphics[{RGBColor[0, 0, 1/257], Disk[]}]]

What is the minimal difference in RGB color values which Mathematica renders and exports as different colors? Is it machine-dependent?

Apprehensible answered 15/10, 2011 at 20:7 Comment(10)
I get False on Mathematica 8.0.1 on Mac OS X 10.6 32-bitBookcraft
@yoda Please try 1/258, 1/259, 1/260 and so on. What is the minimal difference on your system?Apprehensible
True In Mma 8.0 WinXP. I guess 256 distinguishable values ...Six
@yoda does the Mac use something beyond "32-bit" (8 bits per channel) color?Euthenics
@AlexeyPopkov 1/511 is when it hits TrueBookcraft
@yoda Thank you! So it is OS- or machine-dependent. It is interesting to clear-up this behavior. How is it when you Export to PNG? Do such files identical?Apprehensible
@Euthenics I believe it's 32-bit (8/color channel + 8 for alpha).Bookcraft
Maybe 1/511 because of just rounding to the closest /256?Bolingbroke
And this rounding occures in Rasterize I think.Bolingbroke
@Bolingbroke That is correct. The rounding difference is exactly 1/255 as I've shown in my answer.Bookcraft
B
4

I believe this behaviour is machine dependent, but I do not know how exactly it depends on the OS. On my machine, it evaluates to True only when the denominator is 511.

n = 257; 
While[(Rasterize[Graphics[{RGBColor[0, 0, 0], Disk[]}]] === 
    Rasterize[Graphics[{RGBColor[0, 0, 1/n], Disk[]}]]) != True, 
 n++]; 
Print@n

Out[1]=511

There is a difference between the two images for n<511

p1 = ImageData@Rasterize[Graphics[{RGBColor[0, 0, 0], Disk[]}]];
p2 = ImageData@Rasterize[Graphics[{RGBColor[0, 0, 1/257], Disk[]}]];
ArrayPlot[p1 - p2]

enter image description here

This difference is constant all the way through n=510 and is equal to 1/255.

Max[p2 - p1] === N[1/255]
Out[1]=True
Bookcraft answered 15/10, 2011 at 21:0 Comment(4)
It is also interesting to compare a set of rectangles of different colors as they are shown on-screen (by taking a screenshot with the PrtSc key). Probably the results will be the same as with Rasterize - just for the check. For example: Graphics[{RGBColor[0,0,#],Rectangle[]},ImageSize->40]&/@{0,1/256,1/257,1/510,1/511}. Please post the screenshot saved in PNG format. On my machine only the second rectangle has RGB value {0,0,1} - others have {0,0,0}.Apprehensible
@AlexeyPopkov Here's what I get: i.sstatic.net/mCCoc.png It's interesting how you get {0,0,1}...Bookcraft
In your screenshot rectangles have RGB value {0,0,0} for the first and the last rectangles but for others it has RGB value {0,0,1}. I get it with Adobe Photoshop CS4. It is as expected from your answer. Thank you.Apprehensible
@AlexeyPopkov Oh, you mean RGB value of {0,0,1/255}. That is correct, and is what I mentioned in the answer. I was very surprised when you said {0,0,1}, because that's Blue (I understand now that you were giving the 8-bit value)...Bookcraft
B
3

Looks like Rasterize rounds each pixel's R G B channels to the closest 8bit value (to the closest 1/256).

image = Image[{{{0, 0, .2/256}, {0, 0, .7/256}, {0, 0, 1.2/256}, {0, 
     0, 1.7/256}}}, ImageSize -> 4]
ImageData@image
Rasterize@image
ImageData@Rasterize@image

enter image description here

So the minimal difference, rasterizing into different colors should be around 0.000000000000000000000000000...

Bolingbroke answered 15/10, 2011 at 22:19 Comment(0)
F
1

Guilty party here is Rasterize, which chops off color precision. Get help on ImageType[] to see that Mathematica actually recognizes other bit depths, but Rasterize[] vandalizes anything beyond Byte.

Felisha answered 5/12, 2011 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.