6
$\begingroup$

If you look closely at the colors used with a simple polygon graphic in Mathematica, they feather the colors at the edge to make for a nicer graphic, but I don't want that for my project. I want to control the number of total colors that will be used in the exported bitmap. How can I create a graphic of multiple different colored solid polygons and end with a final result that has that exact number of colors. For example:

image=Graphics[{Red, Triangle[{{0, 0}, {1, 0}, {0, 1}}], 
  Blue, Triangle[{{1, 0}, {2, 0}, {2, 1}}], 
  Green, Triangle[{{2, 1}, {2, 2}, {1, 2}}], 
  Yellow, Triangle[{{1, 2}, {0, 2}, {0, 1}}]}]

If you analyze the output from ImageData[image] you can see shades of red, green, blue and yellow are used on the edges. Exporting this to a bitmap creates a file with many colors. I want to export this to a bitmap with exactly 5 colors: white, red, blue, green and yellow. I have played with EdgeForm with no luck. Thanks!

$\endgroup$
1
  • 3
    $\begingroup$ You could use ColorQuantize[Rasterize[image], 5] to produce an image with exactly 5 colors. $\endgroup$
    – MarcoB
    Mar 21, 2021 at 14:48

1 Answer 1

11
$\begingroup$

You can use the Antialiasing option to get what you want:

image = Graphics[
  {
   Red, Triangle[{{0, 0}, {1, 0}, {0, 1}}],
   Blue, Triangle[{{1, 0}, {2, 0}, {2, 1}}],
   Green, Triangle[{{2, 1}, {2, 2}, {1, 2}}],
   Yellow, Triangle[{{1, 2}, {0, 2}, {0, 1}}]
   },
  BaseStyle -> Antialiasing -> False
  ]

Rasterize@image

enter image description here

$\endgroup$

Your Answer

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

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