​fly-in /
src / render / palette.py
63 commits
09-08-2026
159 lines 4.3 kB
  1"""The fixed color palette the graphical view draws with.
  2
  3Everything on screen comes from here, which is what keeps a map full
  4of arbitrary color= words looking like one picture instead of a
  5handful of clashing primaries. The slots are split in two: terrain
  6colors are dark and muted so they stay in the background, and zone
  7colors are bright so they never disappear into the grass.
  8"""
  9
 10from typing import Final
 11
 12RGB = tuple[int, int, int]
 13
 14GRASS_DARK: Final[RGB] = (48, 96, 60)
 15GRASS: Final[RGB] = (66, 122, 72)
 16GRASS_LIGHT: Final[RGB] = (92, 148, 88)
 17DIRT_DARK: Final[RGB] = (86, 62, 42)
 18DIRT: Final[RGB] = (122, 92, 62)
 19STONE: Final[RGB] = (110, 110, 118)
 20INK: Final[RGB] = (28, 26, 34)
 21PAPER: Final[RGB] = (236, 236, 228)
 22HINT: Final[RGB] = (240, 216, 96)
 23MUTED: Final[RGB] = (152, 152, 160)
 24
 25TERRAIN: Final[tuple[RGB, ...]] = (
 26    GRASS_DARK,
 27    GRASS,
 28    GRASS_LIGHT,
 29    DIRT_DARK,
 30    DIRT,
 31    STONE
 32)
 33
 34ZONE_COLORS: Final[dict[str, RGB]] = {
 35    "red": (208, 70, 72),
 36    "green": (148, 224, 108),
 37    "blue": (90, 140, 226),
 38    "yellow": (240, 216, 96),
 39    "orange": (232, 148, 62),
 40    "purple": (160, 108, 208),
 41    "magenta": (214, 106, 176),
 42    "pink": (240, 160, 180),
 43    "cyan": (104, 208, 208),
 44    "brown": (168, 116, 76),
 45    "crimson": (188, 48, 74),
 46    "darkred": (140, 42, 46),
 47    "maroon": (112, 40, 52),
 48    "gold": (232, 190, 72),
 49    "lime": (176, 232, 88),
 50    "violet": (176, 132, 232),
 51    "teal": (72, 160, 160),
 52    "navy": (58, 82, 168),
 53    "silver": (200, 200, 208),
 54    "white": PAPER,
 55    "gray": (152, 152, 160),
 56    "grey": (152, 152, 160),
 57    "black": (56, 54, 64),
 58}
 59
 60UNNAMED: Final[tuple[RGB, ...]] = (
 61    (152, 152, 160),
 62    (186, 168, 140),
 63    (140, 168, 186),
 64    (186, 140, 168),
 65)
 66
 67ALL: Final[tuple[RGB, ...]] = TERRAIN + tuple(ZONE_COLORS.values()) + (
 68    INK,
 69    PAPER,
 70) + UNNAMED
 71
 72FIRE: Final[tuple[RGB, ...]] = (
 73    INK,
 74    (208, 70, 72),
 75    (232, 148, 62),
 76    (240, 216, 96),
 77    PAPER,
 78)
 79
 80
 81RAINBOW: Final[tuple[RGB, ...]] = (
 82    ZONE_COLORS["red"],
 83    ZONE_COLORS["orange"],
 84    ZONE_COLORS["yellow"],
 85    ZONE_COLORS["green"],
 86    ZONE_COLORS["cyan"],
 87    ZONE_COLORS["blue"],
 88    ZONE_COLORS["purple"],
 89)
 90
 91
 92def zone_rgb(color: str | None) -> RGB:
 93    """Pick the palette color a zone is drawn in.
 94
 95    A word the palette does not know isn't an error: the subject
 96    fixes no list of color names. Unknown words are spread over a few
 97    neutral slots by their letters, so the same word always lands on
 98    the same color and two different words rarely collide.
 99
100    Args:
101        color: The map's color= value, or None when it gave none.
102
103    Returns:
104        The color to fill the zone with.
105    """
106    if color is None:
107        return STONE
108    known = ZONE_COLORS.get(color.lower())
109    if known is not None:
110        return known
111    spread = sum(ord(letter) for letter in color)
112    return UNNAMED[spread % len(UNNAMED)]
113
114
115def snap(color: RGB) -> RGB:
116    """Round a color to the nearest one the palette holds.
117
118    Sprites are drawn at a much higher resolution than the view uses,
119    and shrinking them blends colors the palette never contained.
120    Snapping afterwards restores the hard edges pixel art needs.
121
122    Args:
123        color: The color to round.
124
125    Returns:
126        The closest palette entry.
127    """
128    red, green, blue = color
129    best = ALL[0]
130    shortest = -1
131    for candidate in ALL:
132        gap = (
133            (candidate[0] - red) ** 2
134            + (candidate[1] - green) ** 2
135            + (candidate[2] - blue) ** 2
136        )
137        if shortest < 0 or gap < shortest:
138            shortest = gap
139            best = candidate
140    return best
141
142
143def ember(color: RGB) -> RGB:
144    """Recolor a gray pixel as a shade of fire.
145
146    The drone artwork is drawn in grays, which makes an explosion look
147    like scattered soot rather than a bang. Reading the brightness of
148    each pixel and looking it up on a fire ramp turns the same drawing
149    into flame without touching the artwork.
150
151    Args:
152        color: The gray pixel to recolor.
153
154    Returns:
155        The palette color it burns as.
156    """
157    red, green, blue = color
158    light = (red * 299 + green * 587 + blue * 114) // 1000
159    return FIRE[min(light * len(FIRE) // 256, len(FIRE) - 1)]