"""Shared figure style for all papers.

Figures are drawn at the size they print (A4 text block is 6.27 in wide) and
included in LaTeX at natural size, so 8.5 pt in matplotlib is 8.5 pt on paper.
Computer Modern matches the Latin Modern body text.
"""
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

TEXTW = 6.2      # usable width in inches, a little inside the 6.27 in block
NAVY, MOSS = "#13293D", "#385623"
BLUE, RED, AMBER, PURPLE, GREEN, GREY = "#1f5f9e", "#b03a2e", "#b8860b", "#6a4c93", "#2e8b57", "#7a7a7a"
ORANGE, LIGHTMOSS, SLATE = "#d9822b", "#7a9a45", "#5f8396"

plt.rcParams.update({
    "font.family": "serif",
    "font.serif": ["cmr10"],
    "mathtext.fontset": "cm",
    "axes.formatter.use_mathtext": True,
    "axes.unicode_minus": True,
    "font.size": 8.5,
    "axes.labelsize": 8.5,
    "axes.titlesize": 8.5,
    "xtick.labelsize": 8,
    "ytick.labelsize": 8,
    "legend.fontsize": 8,
    "legend.frameon": False,
    "legend.handlelength": 1.8,
    "legend.columnspacing": 1.4,
    "axes.spines.top": False,
    "axes.spines.right": False,
    "axes.linewidth": 0.6,
    "xtick.major.width": 0.6,
    "ytick.major.width": 0.6,
    "xtick.minor.width": 0.4,
    "ytick.minor.width": 0.4,
    "lines.linewidth": 1.4,
    "lines.markersize": 4.5,
    "savefig.bbox": "tight",
    "savefig.pad_inches": 0.03,
    "pdf.fonttype": 42,
})


def save(fig, name):
    fig.savefig(name)
    fig.savefig(name.replace(".pdf", ".png"), dpi=200)
    plt.close(fig)
