daskol / mpl-typst

Typst backend for matplotlib (Python visualization library).
MIT License
17 stars 1 forks source link

Bug: text is not colored correctly #13

Open cgahr opened 2 weeks ago

cgahr commented 2 weeks ago

Coloring text does not work. Consider the following code:

import matplotlib as mpl
import matplotlib.pyplot as plt
import mpl_typst.as_default  # noqa: F401

fig, ax = plt.subplots(1, 1)

colors = ["C0", "C1", "C2"]
linestyles = ["-", ":", "-."]

legend = ax.legend(
    [
        mpl.lines.Line2D([], [], ls=ls, color=color)
        for ls, color in zip(linestyles, colors, strict=True)
    ],
    ["text0", "text1", "text2"],
    loc="center",
    ncol=3,
    borderaxespad=0.5,
)

for text, color in zip(legend.get_texts(), colors, strict=True):
    text.set_color(color)
fig.savefig("test.typ")

The result should be: image In the created typst file, however, the text is black and not colored.

cgahr commented 2 weeks ago

I think somewhere in the draw_text function, we must call gc.get_rgb() to get the color and adjust draw-text to take a fill argument so that we can do

text(..., fill: color, content)

in prologue.typ.