-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
from collections import OrderedDict, defaultdict | ||
from copy import copy | ||
from tempfile import TemporaryFile | ||
from typing import Tuple | ||
|
||
import svgwrite | ||
|
||
colors: OrderedDict = OrderedDict( | ||
the_palette=OrderedDict( | ||
dark0="#0d0e0f", | ||
dark="#202020", | ||
background_dark="#1d2021", | ||
background="#282828", | ||
background_light="#32302f", | ||
foreground="#ebdbb2", | ||
gray="#dedede", | ||
medium_gray="#504945", | ||
comment="#665c54", | ||
milk="#e7d7ad", | ||
error_red="#cc241d", | ||
red="#fb4934", | ||
orange="#d65d0e", | ||
bright_yellow="#fabd2f", | ||
soft_yellow="#eebd35", | ||
pink="#d4879c", | ||
magenta="#b16286", | ||
soft_green="#98971a", | ||
forest_green="#689d6a", | ||
clean_green="#8ec07c", | ||
blue_gray="#458588", | ||
dark_gray="#83a598", | ||
light_blue="#7fa2ac", | ||
), | ||
intensities=OrderedDict( | ||
dark=OrderedDict( | ||
dark="#0d0e0f", | ||
background_dark="#171A1A", | ||
background="#1d2021", | ||
), | ||
soft_flat=OrderedDict( | ||
background="#32302f", background_dark="#32302f", background_light="#3c3a39" | ||
), | ||
soft=OrderedDict( | ||
background="#32302f", background_dark="#282626", background_light="#3c3a39" | ||
), | ||
medium=OrderedDict(background="#282828", background_dark="#242424"), | ||
), | ||
) | ||
|
||
|
||
text_colors: dict = defaultdict(lambda: None) | ||
|
||
INVERSE_COLOR = "milk" | ||
text_colors["dark0"] = colors["the_palette"][INVERSE_COLOR] | ||
text_colors["dark"] = colors["the_palette"][INVERSE_COLOR] | ||
text_colors["background_dark"] = colors["the_palette"][INVERSE_COLOR] | ||
text_colors["background"] = colors["the_palette"][INVERSE_COLOR] | ||
text_colors["background_light"] = colors["the_palette"][INVERSE_COLOR] | ||
|
||
X_PLANE = 1920 | ||
Y_PLANE = 1080 | ||
COLOR_BOX_X = 280 | ||
COLOR_BOX_Y = 160 | ||
START_X = 0 | ||
START_Y = 0 | ||
X_MARGIN_RIGHT = 80 | ||
Y_MARGIN_TOP = 45 | ||
COLOR_BOX_RX = 20 | ||
COLOR_BOX_RY = 20 | ||
|
||
|
||
def hex_to_rgb(hex_string: str) -> str: | ||
hex_ = hex_string.lstrip("#") | ||
rgb = tuple(int(hex_[i : i + 2], 16) for i in (0, 2, 4)) | ||
return f"rgb({rgb[0]},{rgb[1]},{rgb[2]})" | ||
|
||
|
||
def draw_background( | ||
dwg: svgwrite.Drawing, | ||
color: str, | ||
) -> None: | ||
dwg.add( | ||
dwg.rect( | ||
insert=(0, 0), | ||
size=(X_PLANE, Y_PLANE), | ||
fill=hex_to_rgb(color), | ||
) | ||
) | ||
|
||
|
||
def draw_text( | ||
dwg: svgwrite.Drawing, | ||
color_name: str, | ||
color_value: str, | ||
default_text_color: str, | ||
x: int, | ||
y: int, | ||
): | ||
text_color = text_colors[color_name] | ||
if text_color is None: | ||
text_color = default_text_color | ||
dwg.add( | ||
svgwrite.text.Text( | ||
color_name.replace("_", " "), | ||
insert=(x + 25, y + 35), | ||
fill=hex_to_rgb(text_color), | ||
font_size="28px", | ||
font_weight="bold", | ||
) | ||
) | ||
dwg.add( | ||
svgwrite.text.Text( | ||
color_value, | ||
insert=(x + 25, y + COLOR_BOX_Y - 15), | ||
fill=hex_to_rgb(text_color), | ||
font_size="28px", | ||
) | ||
) | ||
|
||
|
||
def create_color_box( | ||
dwg: svgwrite.Drawing, | ||
color_name: str, | ||
color_value: str, | ||
default_text_color: str, | ||
x: int, | ||
y: int, | ||
) -> None: | ||
dwg.add( | ||
dwg.rect( | ||
insert=(x, y), | ||
size=(COLOR_BOX_X, COLOR_BOX_Y), | ||
rx=COLOR_BOX_RX, | ||
ry=COLOR_BOX_RY, | ||
fill=hex_to_rgb(color_value), | ||
stroke=hex_to_rgb(colors["the_palette"]["comment"]), | ||
stroke_width=5, | ||
) | ||
) | ||
draw_text(dwg, color_name, color_value, default_text_color, x, y) | ||
|
||
|
||
def max_x() -> int: | ||
return X_PLANE - COLOR_BOX_X - X_MARGIN_RIGHT | ||
|
||
|
||
def max_y() -> int: | ||
return Y_PLANE - COLOR_BOX_Y - Y_MARGIN_TOP | ||
|
||
|
||
def move_position(x: int, y: int) -> Tuple[int, int]: | ||
new_x = x + COLOR_BOX_X + X_MARGIN_RIGHT | ||
new_y = y | ||
if new_x > max_x(): | ||
new_y = y + Y_MARGIN_TOP + COLOR_BOX_Y | ||
new_x = START_X + X_MARGIN_RIGHT | ||
return new_x, new_y | ||
|
||
|
||
def draw_intensity_map_text( | ||
dwg: svgwrite.Drawing, intensity_string: str, x: int, y: int | ||
) -> None: | ||
dwg.add( | ||
svgwrite.text.Text( | ||
intensity_string, | ||
insert=(x + 25, y + 3 * (COLOR_BOX_Y / 4)), | ||
fill=colors["the_palette"]["milk"], | ||
font_size="96px", | ||
font_weight="bold", | ||
) | ||
) | ||
|
||
|
||
def create_palette(palette_dict: dict, name: str) -> None: | ||
svg_file = TemporaryFile("r+", encoding="utf-8") | ||
dwg = svgwrite.Drawing(svg_file, profile="tiny", size=(1920, 1080)) | ||
|
||
current_x = START_X + X_MARGIN_RIGHT | ||
current_y = START_Y + Y_MARGIN_TOP | ||
|
||
draw_background(dwg, palette_dict["background"]) | ||
for color_name, color_value in palette_dict.items(): | ||
create_color_box( | ||
dwg, color_name, color_value, palette["background"], current_x, current_y | ||
) | ||
current_x, current_y = move_position(current_x, current_y) | ||
draw_intensity_map_text(dwg, name, current_x, current_y) | ||
with open(f"palettes/{name}.svg", "w", encoding="utf-8") as file: | ||
file.write(dwg.tostring()) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("To run the 'svgwrite' package needs to be installed!") | ||
print("To install: pip install svgwrite") | ||
print("https://pypi.org/project/svgwrite/") | ||
for intensity, value in colors["intensities"].items(): | ||
print(f"Creating palette for intensity: {intensity} ...") | ||
palette = copy(colors["the_palette"]) | ||
palette.update(value) | ||
create_palette(palette, intensity) | ||
print("Done!") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.