|
| 1 | +"""Builds a logo variant by recolouring the block texture of an existing one. |
| 2 | +
|
| 3 | +Several Minecraft blocks are the same speckled stone in a different colour, so |
| 4 | +a variant for one of those is a recolour rather than a new drawing. The prompt |
| 5 | +glyph is left alone: it is near-grey, and the texture is not, which is enough |
| 6 | +to tell them apart without hand-marking pixels. |
| 7 | +
|
| 8 | + cd docs/logo |
| 9 | + python recolor_variant.py |
| 10 | +
|
| 11 | +Writes into variants/. Existing files are overwritten, so it is safe to rerun |
| 12 | +after changing a ramp. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from PIL import Image |
| 18 | + |
| 19 | +SOURCE = os.path.join("variants", "dirt.png") |
| 20 | + |
| 21 | +# Darkest and lightest tone of each block. The shades in between are taken from |
| 22 | +# the source's own brightness, which is what keeps the speckle pattern. |
| 23 | +RAMPS = { |
| 24 | + # Gold is a flatter block in game, so its ramp is deliberately short: the |
| 25 | + # same speckle at full strength reads as sand. |
| 26 | + "gold": ((0xC2, 0x9E, 0x1A), (0xFA, 0xF0, 0x62)), |
| 27 | + # Stone is speckled exactly like this, so it takes the full contrast. |
| 28 | + "stone": ((0x5E, 0x5E, 0x5E), (0xA8, 0xA8, 0xA8)), |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +def luminance(pixel): |
| 33 | + r, g, b = pixel[:3] |
| 34 | + return 0.299 * r + 0.587 * g + 0.114 * b |
| 35 | + |
| 36 | + |
| 37 | +def is_texture(pixel): |
| 38 | + """True for block pixels, false for the glyph drawn on top of them. |
| 39 | +
|
| 40 | + The glyph is white or near-grey; every texture tone is saturated, so the |
| 41 | + spread between the channels separates the two cleanly. |
| 42 | + """ |
| 43 | + r, g, b = pixel[:3] |
| 44 | + return max(r, g, b) - min(r, g, b) > 25 |
| 45 | + |
| 46 | + |
| 47 | +def recolor(image, dark, light): |
| 48 | + # Read through tobytes rather than getdata, which Pillow has deprecated in |
| 49 | + # favour of a method too new to rely on here. |
| 50 | + raw = image.tobytes() |
| 51 | + pixels = [tuple(raw[i:i + 4]) for i in range(0, len(raw), 4)] |
| 52 | + texture = [p for p in pixels if is_texture(p)] |
| 53 | + low = min(luminance(p) for p in texture) |
| 54 | + high = max(luminance(p) for p in texture) |
| 55 | + span = high - low or 1 |
| 56 | + |
| 57 | + out = [] |
| 58 | + for pixel in pixels: |
| 59 | + if not is_texture(pixel): |
| 60 | + out.append(pixel) |
| 61 | + continue |
| 62 | + t = (luminance(pixel) - low) / span |
| 63 | + out.append( |
| 64 | + ( |
| 65 | + round(dark[0] + (light[0] - dark[0]) * t), |
| 66 | + round(dark[1] + (light[1] - dark[1]) * t), |
| 67 | + round(dark[2] + (light[2] - dark[2]) * t), |
| 68 | + pixel[3], |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + return Image.frombytes( |
| 73 | + "RGBA", image.size, bytes(channel for pixel in out for channel in pixel) |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + source = Image.open(SOURCE).convert("RGBA") |
| 79 | + for name, (dark, light) in RAMPS.items(): |
| 80 | + path = os.path.join("variants", f"{name}.png") |
| 81 | + recolor(source, dark, light).save(path) |
| 82 | + print(f"wrote {path}") |
| 83 | + source.close() |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments