From e789f760d11a01d24f8e8cc91cf7cfb93fbc4a60 Mon Sep 17 00:00:00 2001 From: "Yi Yang @ Anteros" Date: Fri, 6 Oct 2023 19:11:03 +0800 Subject: [PATCH] (RV) blend colours --- agrf/graphics/blend.py | 23 +++++++++++++++++++++++ agrf/graphics/palette.py | 23 +++++++++++++++++++++++ industry/industries/farm.py | 11 ++++++++++- road_vehicle/lib/roster.py | 8 ++++++-- 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 agrf/graphics/blend.py create mode 100644 agrf/graphics/palette.py diff --git a/agrf/graphics/blend.py b/agrf/graphics/blend.py new file mode 100644 index 00000000..1ea57c02 --- /dev/null +++ b/agrf/graphics/blend.py @@ -0,0 +1,23 @@ +from PIL import Image, ImageChops +import numpy as np + + +def blend(image, mask): + v = ImageChops.lighter(ImageChops.lighter(image.getchannel("R"), image.getchannel("G")), image.getchannel("B")) + mask_colour = np.uint16(mask.convert("RGBA")) + mask_colour *= np.uint16(v)[:, :, np.newaxis] + mask_colour //= 2**7 + r_ob = np.maximum(mask_colour[:, :, 0], 255) - 255 + g_ob = np.maximum(mask_colour[:, :, 1], 255) - 255 + b_ob = np.maximum(mask_colour[:, :, 2], 255) - 255 + ob = (r_ob + g_ob + b_ob) // 2 + + above = mask_colour >= 255 + below = np.minimum(mask_colour + ob[:, :, np.newaxis] * (255 - mask_colour) // 256, 255) + + image_np = np.uint8(image) + below[:, :, 3] = image_np[:, :, 3] + mask_pal = np.uint8(mask) + blended = np.where(np.broadcast_to((mask_pal == 0)[:, :, np.newaxis], (*mask_pal.shape, 4)), image_np, below) + + return Image.fromarray(blended.astype("uint8")) diff --git a/agrf/graphics/palette.py b/agrf/graphics/palette.py new file mode 100644 index 00000000..23f4ac2a --- /dev/null +++ b/agrf/graphics/palette.py @@ -0,0 +1,23 @@ +import json + +with open("files/ttd_palette.json") as f: + PALETTE = json.load(f)["entries"] + + +class CompanyColour: + DARK_BLUE = 0xC6 + PALE_GREEN = 0x60 + PINK = 0x2A + YELLOW = 0x3E + RED = 0xB3 + LIGHT_BLUE = 0x9A + GREEN = 0x52 + DARK_GREEN = 0x58 + BLUE = 0x92 + CREAM = 0x72 + MAUVE = 0x80 + PURPLE = 0x88 + ORANGE = 0x40 + BROWN = 0x20 + GREY = 0x4 + WHITE = 0x8 diff --git a/industry/industries/farm.py b/industry/industries/farm.py index 08a1ef5d..83464217 100644 --- a/industry/industries/farm.py +++ b/industry/industries/farm.py @@ -21,6 +21,15 @@ ] ) +small_set = symmetrize( + [ + ( + "abd", + "ecf", + ), + ] +) + tiny_set = [ ("ab",), ] @@ -46,7 +55,7 @@ 0: transcribe(medium_set, tile_map), 1: transcribe(medium_set, tile_map), 2: transcribe(medium_set, tile_map), - 3: transcribe(medium_set, tile_map), + 3: transcribe(small_set, tile_map), 4: transcribe(tiny_set, tile_map), 5: transcribe(one_tile_set, tile_map), 6: [[{"xofs": 0, "yofs": 0, "gfx": grf.NewIndustryTileID(0x23)}]], diff --git a/road_vehicle/lib/roster.py b/road_vehicle/lib/roster.py index 9f0d81a2..96fc34bd 100644 --- a/road_vehicle/lib/roster.py +++ b/road_vehicle/lib/roster.py @@ -4,6 +4,7 @@ from tabulate import tabulate from road_vehicle.lib import ARoadVehicle, supported_techclasses from agrf.strings import get_translation +from agrf.graphics.blend import blend def dimens_repr(dimensions): @@ -206,8 +207,11 @@ def gen_docs(self, string_manager): # Prepare graphics sprite = v.graphics_helper.doc_graphics() - img, bpp = sprite.get_zoom_bpp(grf.ZOOM_NORMAL, 32).get_image() - img.save(os.path.join(prefix, "img", f'{v["translation_name"]}.png')) + masked_sprite = sprite.get_zoom_bpp(grf.ZOOM_NORMAL, 32) + img, _ = masked_sprite.get_image() + mask, _ = masked_sprite.mask.get_image() + masked_img = blend(img, mask) + masked_img.save(os.path.join(prefix, "img", f'{v["translation_name"]}.png')) # Dump with open(os.path.join(prefix, f'{v["translation_name"]}.md'), "w") as f: