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,0,0,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0],
# [0,0,I,0,0,0,0,0,0,0],
# [O,O,I,T,0,0,0,J,J,0],
# [O,O,I,T,T,L,L,J,L,0],
# [O,O,I,T,I,I,L,J,L,0],
# [S,S,S,S,0,S,S,S,S,S]
# ]
board_width = len(board[0])
board_height = len(board)
class color:
def __init__(self, color=None, red=None, green=None, blue=None, alpha=255) -> None:
# Allow hex string input like "#ff8800" or "ff8800"
if isinstance(color, str):
hexcode = color.lstrip("#")
if len(hexcode) not in (6, 8):
raise ValueError("Hex color must be 6 or 8 characters long")
self.r = int(hexcode[0:2], 16)
self.g = int(hexcode[2:4], 16)
self.b = int(hexcode[4:6], 16)
if len(hexcode) == 8:
self.a = int(hexcode[6:8], 16)
else:
self.a = alpha
else:
# Regular RGB(A) input
self.r = red
self.g = green
self.b = blue
self.a = alpha
def get(self) -> tuple[int, int, int]:
return (self.r, self.g, self.b)
def sub(self, n) -> tuple[int, int, int]:
if self.r:
r=self.r-n
else:
r=0
if self.g:
g=self.g-n
else:
g=0
if self.b:
b=self.b-n
else:
b=0
return (r,g,b)
def multiply(self, other) -> tuple[int, int, int]:
r = other[0]
g = other[1]
b = other[2]
return (((self.r * r) >> 8), ((self.g * g) >> 8), ((self.b * b)>>8))
def shiftr(self, n):
return ((self.r >> n), (self.g >> n), (self.b >>n))
def mask(self, n):
return ((self.r & n), (self.g & n), (self.b & n))
black = color("#000000")
white = color("#ffffff")
colors = [
color("#000000"),
color("#00ffff"),
color("#fff200"),
color("#9d00ff"),
color("#00ff00"),
color("#ff0000"),
color("#0000ff"),
color("#ff8000")
]
width = board_width*32
height = board_height*32
from PIL import Image
img = Image.new("RGB", (width, height))
pixels = img.load()
for y in range(height):
for x in range(width):
xd = x
yd = y
color_internal = colors[board[yd>>5][xd>>5]]
if board[yd>>5][xd>>5] != 0:
xs = xd&0x1F
ys = yd&0x1F
if (xs > 7 and xs < 24) and (ys > 7 and ys < 24): # middle
pixels[x, y] = color_internal.get()
elif (xs > 5 and xs < 26) and (ys > 5 and ys < 26): # mid boarder
pixels[x, y] = color_internal.mask(0xFFFFFFB0)
elif xs+ys<31: # top left edge
if xs>ys:
pixels[x, y] = color_internal.get()
else:
pixels[x, y] = color_internal.mask(0xFFFFFFEF)
else:
if xs>ys:
pixels[x, y] = color_internal.mask(0xFFFFFFD7)
else:
pixels[x, y] = color_internal.mask(0xFFFFFFC0)
else:
# pixels[x, y] = (0, 0, 0)
if (x+y)&1:
pixels[x, y] = (0, 0, 255)
else:
pixels[x, y] = (int((x + y) * 255 / ((width - 1) + (height - 1))), int((1 - (x + y) / (width + height - 2)) * 255), 0)
img.save("output/folie_16_background.png")
img.show()