Anti-aliasing of red text on white background
As explained in the comments, text rendering is special and has sub-pixel anti-aliasing which introduces the colours. This does not occur with other graphics primitives like lines, circles, polygons etc. just text.
ImageResize[Rasterize[Text["Hello"], RasterSize -> 16], 100,
Resampling -> "Nearest"]
ImageResize[
Rasterize[Graphics[Line[{{0, 0}, {1, Sqrt[2]}}]],
RasterSize -> 16], 100, Resampling -> "Nearest"]
I don't know how to turn off the subpixel anti-aliasing but this work-around can turn the text into a mesh which will then render without the subpixel anti-aliasing colours appearing:
mesh = ImageMesh[
ColorNegate@
Rasterize[
Graphics[
Text[Style["Mathematica", RGBColor[0, 0, 0], Italic, 30]]],
RasterSize -> 1500]]
gr = Style[Graphics[{EdgeForm[None], FaceForm[Red], mesh}],
Antialiasing -> True] // Rasterize
colors = Flatten[ImageData[gr], 1] // DeleteDuplicates;
Graphics[MapIndexed[{RGBColor @@ #1, Disk[{8 First[#2]/10, 0}]} &,
colors], ImageSize -> {Automatic, 100}, Background -> Black]
Graphics[Table[{Blend[{Red, White}, x], Disk[{8 x, 0}]}, {x, 0, 1,
1/10}], ImageSize -> {Automatic, 100}, Background -> Black]
Another trick you can use is to export it as SVG then re-import it using ResourceFunction["SVGImport"]
. This has the effect of breaking the text down into FilledCurve
primitives which will not render with subpixel anti-aliasing:
svgtricktext =
ResourceFunction["SVGImport"][
ExportString[Text[Style["Hello", Red]], "SVG"]]
I've been searching around and apparently there's this trick where you can set the opacity to 0.999 and it either triggers greyscale sub-pixel rendering or turns it off instead of doing RGB subpixel rendering. See here, and here
Rasterize[Style["Hello", Red, FontOpacity -> .999], RasterSize -> 32]
Another standard way:
graphicsText[x_] := ImportString[ExportString[x, "PDF"], "PDF"][[1, 1]];
gr = Graphics@ graphicsText[
Text[Style["Mathematica", RGBColor[1, 0, 0], Italic, 30]]]
colors = Flatten[ImageData[gr, "Byte"], 1] // DeleteDuplicates;
Graphics[Table[{RGBColor @@ (colors[[x]]/255),
Disk[{8 Mod[x, 40]/10, -2 Floor[x/40]}]}, {x, 1, Length[colors]}],
ImageSize -> 400, Background -> Black]
Graphics[Table[{Blend[{Red, White}, x], Disk[{8 x, 0}]}, {x, 0, 1,
1/10}], ImageSize -> 400, Background -> Black]