|
| 1 | +""" |
| 2 | +The Pattern class for specifying GMT filling patterns. |
| 3 | +""" |
| 4 | + |
| 5 | +import dataclasses |
| 6 | + |
| 7 | +from pygmt.alias import Alias |
| 8 | +from pygmt.params.base import BaseParam |
| 9 | +from pygmt.exceptions import GMTValueError |
| 10 | + |
| 11 | + |
| 12 | +@dataclasses.dataclass(repr=False) |
| 13 | +class Pattern(BaseParam): |
| 14 | + """ |
| 15 | +
|
| 16 | + Examples |
| 17 | + -------- |
| 18 | + >>> from pygmt.params import Pattern |
| 19 | + >>> str(Pattern(id=1)) |
| 20 | + 'p1' |
| 21 | + >>> str(Pattern(id=1, bgcolor="red", fgcolor="blue")) |
| 22 | + 'p1+bred+fblue' |
| 23 | + >>> str(Pattern(id=1, bgcolor="red", fgcolor="blue", dpi=300)) |
| 24 | + 'p1+bred+fblue+r300' |
| 25 | + >>> str(Pattern(id="my_pattern.png")) |
| 26 | + 'pmy_pattern.png' |
| 27 | + >>> str(Pattern(id=2, reversed=True)) |
| 28 | + 'P2' |
| 29 | + >>> str(Pattern(id=100)) |
| 30 | + Traceback (most recent call last): |
| 31 | + ... |
| 32 | + pygmt.exceptions.GMTValueError: Invalid pattern id: 100... |
| 33 | + """ |
| 34 | + |
| 35 | + id: int | str |
| 36 | + reversed: bool = False |
| 37 | + bgcolor: str | None = None |
| 38 | + fgcolor: str | None = None |
| 39 | + dpi: int | None = None |
| 40 | + |
| 41 | + def __post_init__(self): |
| 42 | + """ |
| 43 | + Validate the id and set the reversed flag. |
| 44 | + """ |
| 45 | + if isinstance(self.id, int) and not (1 <= self.id <= 90): |
| 46 | + raise GMTValueError( |
| 47 | + self.id, |
| 48 | + description="pattern id", |
| 49 | + reason=( |
| 50 | + "Pattern id must be an integer in the range 1-90 " |
| 51 | + "or the name of a 1-, 8-, or 24-bit image raster file" |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + @property |
| 56 | + def _aliases(self): |
| 57 | + """ |
| 58 | + Aliases for the Pattern class. |
| 59 | + """ |
| 60 | + return [ |
| 61 | + Alias(self.id, name="id", prefix="P" if self.reversed else "p"), |
| 62 | + Alias(self.bgcolor, name="bgcolor", prefix="+b"), |
| 63 | + Alias(self.fgcolor, name="fgcolor", prefix="+f"), |
| 64 | + Alias(self.dpi, name="dpi", prefix="+r"), |
| 65 | + ] |
0 commit comments