Skip to content

Instantly share code, notes, and snippets.

@filipgorczynski
Created June 9, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipgorczynski/7e572ad2b8367eb47baa73d83913fab3 to your computer and use it in GitHub Desktop.
Save filipgorczynski/7e572ad2b8367eb47baa73d83913fab3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import math
import re
from PIL import Image, ImageColor, ImageDraw
COLORS_RE = r'(?:#|0x)(?:[a-f0-9]{3}|[a-f0-9]{6})\b|(?:rgb|hsl)a?\([^\)]*\)'
def create_color_palette(project_name, colors_set):
size = len(colors_set)
width = 74
rows = math.ceil(size / 6)
height = 10 * rows + (2 * (rows + 1))
img = Image.new('RGB', (width, height), '#000')
x_pos, y_pos = 2, 2
for color in colors_set:
img_draw = ImageDraw.Draw(img)
img_draw.rectangle((x_pos, y_pos, x_pos + 10, y_pos + 10), fill=color)
x_pos = x_pos + 12
if x_pos >= 74:
x_pos = 2
y_pos += 12
img.save(project_name + '.png', "PNG")
def extract_colors(project):
colors = re.compile(COLORS_RE)
colors_set = set()
project_name = project.split('.')[0].title()
with open('./' + project) as css:
content = css.read()
matches = colors.findall(content, re.MULTILINE)
for match in matches:
if not match.startswith('rgba'):
colors_set.add(match)
create_color_palette(project_name, colors_set)
def main():
for project in ('django.project.css', 'vue.project.css'):
extract_colors(project)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment