Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: possibility to change colors #63

Merged
merged 16 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ venv/
.idea/
.DS_Store
__pycache__/
.pstats
*.egg-info/
dist/
settings.py
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@ game.move_from_coordinates(game.player2, 6, 2, 4, 2)
game.move_from_coordinates(game.player2, 2, 5, 6, 5)
```
There are colors in the command line not showing here in the GIF, though...

## How to change interface colors

With line command you can create new illustrations by specifying the RGB colors you want. You can specify the colors of the cells and of the pieces (outer and inner colors).
Use the following command with the colors you want:
```bash
python interface/colorize.py -ccb 0 191 255 -ccw 207 185 151 -ibp 109 7 26 -obp 0 0 0 -iwp 200 200 200 -owp 255 255 255
```
Arguments are:
- -ccb/--color_cell_black: RGB, default 0, 191, 255
- -ccw/--color_cell_white: RGB, default 207, 185, 151
- -ibp/--inner_black_piece: RGB, default 109, 7, 26
- -obp/--outer_black_piece: RGB, default 0, 0, 0
- -iwp/--inner_white_piece: RGB, default 200, 200, 200
- -owp/--outer_white_piece: RGB, default 255, 255, 255

The command creates (or replaces) the illustrations that are in the temp_images/ folder. If the folder exists, the interface uses theses illustrations. Destroy it to come back to standard illustrations.
Binary file added dev-illustrations/_bishop_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/_bishop_orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/king_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/king_orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/knight_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/knight_orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/pawn_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/pawn_orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/queen_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/queen_orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/rook_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev-illustrations/rook_orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gs_illustrations/bishop_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gs_illustrations/king_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gs_illustrations/knight_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gs_illustrations/pawn_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gs_illustrations/queen_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gs_illustrations/rook_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
205 changes: 205 additions & 0 deletions pyalapin/interface/color_edition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import numpy as np
from PIL import Image

from settings import settings


class ColorEditor(object):
def __init__(
self,
white_cell,
down_white_cell,
black_cell,
down_black_cell,
white_piece_fill,
white_piece_border,
black_piece_fill,
black_piece_border,
):
self.white_cell = white_cell
self.down_white_cell = down_white_cell
self.black_cell = black_cell
self.down_black_cell = down_black_cell
self.white_piece_fill = white_piece_fill
self.white_piece_border = white_piece_border
self.black_piece_fill = black_piece_fill
self.black_piece_border = black_piece_border

def get_edited_black_cell(self):
return np.array([[self.black_cell] * 64] * 64), np.array(
[[self.down_black_cell] * 64] * 64
)

def get_edited_white_cell(self):
return np.array([[self.white_cell] * 64] * 64), np.array(
[[self.down_white_cell] * 64] * 64
)

def get_edited_black_material(self, material_type, cell_color):
if cell_color == "black":
cell_color = self.black_cell
else:
cell_color = self.white_cell

image = Image.open(settings["grayscale_images"][material_type])
r_map = {
0: cell_color[0],
128: self.black_piece_fill[0],
255: self.black_piece_border[0],
}
g_map = {
0: cell_color[1],
128: self.black_piece_fill[1],
255: self.black_piece_border[1],
}
b_map = {
0: cell_color[2],
128: self.black_piece_fill[2],
255: self.black_piece_border[2],
}

edited_img = np.array(image)
edited_img = np.dstack(
[
np.vectorize(r_map.get)(edited_img),
np.vectorize(g_map.get)(edited_img),
np.vectorize(b_map.get)(edited_img),
]
)

if cell_color == "black":
cell_color = self.down_black_cell
else:
cell_color = self.down_white_cell

r_map = {
0: cell_color[0],
128: self.black_piece_fill[0],
255: self.black_piece_border[0],
}
g_map = {
0: cell_color[1],
128: self.black_piece_fill[1],
255: self.black_piece_border[1],
}
b_map = {
0: cell_color[2],
128: self.black_piece_fill[2],
255: self.black_piece_border[2],
}

down_edited_img = np.array(image)
down_edited_img = np.dstack(
[
np.vectorize(r_map.get)(edited_img),
np.vectorize(g_map.get)(edited_img),
np.vectorize(b_map.get)(edited_img),
]
)

return edited_img, down_edited_img

def get_edited_white_material(self, material_type, cell_color):
if cell_color == "black":
cell_color = self.black_cell
else:
cell_color = self.white_cell

image = Image.open(settings["grayscale_images"][material_type])
r_map = {
0: cell_color[0],
128: self.white_piece_fill[0],
255: self.white_piece_border[0],
}
g_map = {
0: cell_color[1],
128: self.white_piece_fill[1],
255: self.white_piece_border[1],
}
b_map = {
0: cell_color[2],
128: self.white_piece_fill[2],
255: self.white_piece_border[2],
}

edited_img = np.array(image)
edited_img = np.dstack(
[
np.vectorize(r_map.get)(edited_img),
np.vectorize(g_map.get)(edited_img),
np.vectorize(b_map.get)(edited_img),
]
)

if cell_color == "black":
cell_color = self.down_black_cell
else:
cell_color = self.down_white_cell

r_map = {
0: cell_color[0],
128: self.white_piece_fill[0],
255: self.white_piece_border[0],
}
g_map = {
0: cell_color[1],
128: self.white_piece_fill[1],
255: self.white_piece_border[1],
}
b_map = {
0: cell_color[2],
128: self.white_piece_fill[2],
255: self.white_piece_border[2],
}

down_edited_img = np.array(image)
down_edited_img = np.dstack(
[
np.vectorize(r_map.get)(edited_img),
np.vectorize(g_map.get)(edited_img),
np.vectorize(b_map.get)(edited_img),
]
)

return edited_img

def generate_all_images(self, path="temp_illustrations"):
b, down_b = self.get_edited_black_cell()
Image.from_array(b).save(os.path.join(path, "b.png"))
Image.from_array(down_b).save(os.path.join(path, "down_b.png"))
w, down_w = self.get_edited_black_cell()
Image.from_array(w).save(os.path.join(path, "w.png"))
Image.from_array(down_w).save(os.path.join(path, "down_w.png"))
for piece in ["pawn", "bishop", "knight", "rook", "king", "queen"]:
wb_p, down_wb_p = self.get_edited_black_material(
material_type=piece, cell_color="white"
)
bb_p, down_bb_p = self.get_edited_black_material(
material_type=piece, cell_color="black"
)
ww_p, down_ww_p = self.get_edited_white_material(
material_type=piece, cell_color="white"
)
bw_p, down_bw_p = self.get_edited_white_material(
material_type=piece, cell_color="black"
)
prefix = "N" if piece == "knight" else piece[0].upper()
Image.from_array(wb_p).save(os.path.join(path, f"wb_{prefix}.png"))
Image.from_array(down_wb_p).save(
os.path.join(path, f"down_wb_{prefix}.png")
)

Image.from_array(bb_p).save(os.path.join(path, f"bb_{prefix}.png"))
Image.from_array(down_bb_p).save(
os.path.join(path, f"down_bb_{prefix}.png")
)

Image.from_array(ww_p).save(os.path.join(path, f"ww_{prefix}.png"))
Image.from_array(down_ww_p).save(
os.path.join(path, f"down_ww_{prefix}.png")
)

Image.from_array(bw_p).save(os.path.join(path, f"bw_{prefix}.png"))
Image.from_array(down_bw_p).save(
os.path.join(path, f"down_bw_{prefix}.png")
)
147 changes: 147 additions & 0 deletions pyalapin/interface/colorize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import argparse
import os

from PIL import Image
import numpy as np


def color_one_img(pil_img, background_color, border_color, inner_color):
img = np.array(pil_img)
x_min = np.min(np.where(img == 255)[0])
x_max = np.max(np.where(img == 255)[0])
img = img[x_min:x_max]

r_dict = {0: background_color[0], 128: inner_color[0], 255: border_color[0]}
g_dict = {0: background_color[1], 128: inner_color[1], 255: border_color[1]}
b_dict = {0: background_color[2], 128: inner_color[2], 255: border_color[2]}

img = [
np.vectorize(r_dict.get)(img),
np.vectorize(g_dict.get)(img),
np.vectorize(b_dict.get)(img),
]
img = np.dstack(img)

img = Image.fromarray(img.astype("uint8"))

img = img.resize((int(img.size[0] / img.size[1] * 450), 450))

x_coordinate = int((500 - img.size[0]) / 2)
y_coordinate = 25

background = np.dstack(
[np.ones((500, 500)) * background_color[i] for i in range(3)]
).astype("uint8")
background = Image.fromarray(background)
background.paste(img, (x_coordinate, y_coordinate))
return background


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-ccb", "--color_cell_black", nargs="+")
parser.add_argument("-ccw", "--color_cell_white", nargs="+")

parser.add_argument("-ibp", "--inner_black_piece", nargs="+")
parser.add_argument("-obp", "--outer_black_piece", nargs="+")

parser.add_argument("-iwp", "--inner_white_piece", nargs="+")
parser.add_argument("-owp", "--outer_white_piece", nargs="+")

args = parser.parse_args()

if args.color_cell_black is None:
color_cell_b = [0, 191, 255]
else:
color_cell_b = [int(i) for i in args.color_cell_black]

if args.color_cell_white is None:
color_cell_w = [207, 185, 151]
else:
color_cell_w = [int(i) for i in args.color_cell_white]

if args.inner_black_piece is None:
inner_color_b = [109, 7, 26]
else:
inner_color_b = [int(i) for i in args.inner_black_piece]
if args.outer_black_piece is None:
border_color_b = [0, 0, 0]
else:
border_color_b = [int(i) for i in args.outer_black_piece]

if args.inner_white_piece is None:
inner_color_w = [200, 200, 200]
else:
inner_color_w = [int(i) for i in args.inner_white_piece]
if args.outer_white_piece is None:
border_color_w = [255, 255, 255]
else:
border_color_w = [int(i) for i in args.outer_white_piece]

down_color_cell_b = np.clip(np.array(color_cell_b) - 70, 0, 255)
down_color_cell_w = np.clip(np.array(color_cell_w) - 70, 0, 255)

for img_name in os.listdir("../gs_illustrations"):
img = Image.open(os.path.join("../gs_illustrations", img_name))
img_bb = color_one_img(img, color_cell_b, border_color_b, inner_color_b)
img_ww = color_one_img(img, color_cell_w, border_color_w, inner_color_w)
img_wb = color_one_img(img, color_cell_w, border_color_b, inner_color_b)
img_bw = color_one_img(img, color_cell_b, border_color_w, inner_color_w)

down_img_bb = color_one_img(
img, down_color_cell_b, border_color_b, inner_color_b
)
down_img_ww = color_one_img(
img, down_color_cell_w, border_color_w, inner_color_w
)
down_img_wb = color_one_img(
img, down_color_cell_w, border_color_b, inner_color_b
)
down_img_bw = color_one_img(
img, down_color_cell_b, border_color_w, inner_color_w
)

name = img_name.split("_")[0]
name = {
"pawn": "P",
"bishop": "B",
"knight": "N",
"rook": "R",
"queen": "Q",
"king": "K",
}[name]
img_bb.save(os.path.join("../temp_images", f"bb_{name}.png"))
img_bw.save(os.path.join("../temp_images", f"bw_{name}.png"))
img_wb.save(os.path.join("../temp_images", f"wb_{name}.png"))
img_ww.save(os.path.join("../temp_images", f"ww_{name}.png"))

down_img_bb.save(os.path.join("../temp_images", f"down_bb_{name}.png"))
down_img_bw.save(os.path.join("../temp_images", f"down_bw_{name}.png"))
down_img_wb.save(os.path.join("../temp_images", f"down_wb_{name}.png"))
down_img_ww.save(os.path.join("../temp_images", f"down_ww_{name}.png"))

bg_w = Image.fromarray(
np.dstack([np.ones((500, 500)) * color_cell_w[i] for i in range(3)]).astype(
"uint8"
)
)
bg_w.save(os.path.join("../temp_images", "w.png"))
bg_b = Image.fromarray(
np.dstack([np.ones((500, 500)) * color_cell_b[i] for i in range(3)]).astype(
"uint8"
)
)
bg_b.save(os.path.join("../temp_images", "b.png"))

down_bg_w = Image.fromarray(
np.dstack(
[np.ones((500, 500)) * down_color_cell_w[i] for i in range(3)]
).astype("uint8")
)
down_bg_w.save(os.path.join("../temp_images", "down_w.png"))
down_bg_b = Image.fromarray(
np.dstack(
[np.ones((500, 500)) * down_color_cell_b[i] for i in range(3)]
).astype("uint8")
)
down_bg_b.save(os.path.join("../temp_images", "down_b.png"))
Loading
Loading