Skip to content

Commit

Permalink
(RV) blend colours
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi committed Oct 6, 2023
1 parent bba64cb commit e789f76
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
23 changes: 23 additions & 0 deletions agrf/graphics/blend.py
Original file line number Diff line number Diff line change
@@ -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"))
23 changes: 23 additions & 0 deletions agrf/graphics/palette.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion industry/industries/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
]
)

small_set = symmetrize(
[
(
"abd",
"ecf",
),
]
)

tiny_set = [
("ab",),
]
Expand All @@ -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)}]],
Expand Down
8 changes: 6 additions & 2 deletions road_vehicle/lib/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e789f76

Please sign in to comment.