4
$\begingroup$

Why does the following not return precisely {0.99,0.5,0.49}?
Is there a way to avoid the discrepancy?

In[2089]:= 
ImageData[
  Show[Graphics[{FaceForm[RGBColor[0.99, 1/2, 0.490000]], 
     Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}]}], ImageSize -> 20, 
   ImagePadding -> None]][[10, 10]]

Out[2089]= {0.988235, 0.501961, 0.490196}
$\endgroup$
2
  • 1
    $\begingroup$ Use c/255 where c is in interval [0,255] for values of colors to be exact. Or use hexadecimal RGBColor["#ff0000"]. $\endgroup$ Commented Mar 15 at 15:59
  • $\begingroup$ Do you want a 20 by 20 image filled with RGBColor[0.99, 1/2, 0.490000]? Try img = Image[ConstantArray[RGBColor[0.99, 1/2, 0.490000], {20, 20}]] . Verify with Flatten[ImageData[img], 1] // DeleteDuplicates is {{0.99, 0.5, 0.49}}. $\endgroup$
    – creidhne
    Commented Mar 15 at 20:12

1 Answer 1

7
$\begingroup$

The real numbers (RGB) spanning [0,1] have to be put into bytes (Range 0..255 permissible levels).

Don't know the actual algorithm, but assuming that the algorithm Rescales:

Rescale[{0.99, 0.5, 0.49}, {0, 1}, {0, 255}] // Round

{252, 128, 125}

which after conversion for display gives:

{252, 128, 125}/255 // N

{0.988235, 0.501961, 0.490196}

$\endgroup$
1
  • $\begingroup$ Thank you! This was helpful. It is somewhat annoying that Mathematica won't accept rgb values in the form of integers in [0,255], but I guess it is easy enough to write a function to do that myself. $\endgroup$
    – Joshua
    Commented Mar 16 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.