I've been looking for a way to keep a unified selection of colors that I can access in different projects and use with different frameworks for a while.
The idea is to define a color palette such as:
palette = {
"orange": "#ce8964",
"yellow": "#eaf27c",
"green": "#71b48d",
"blue": "#454ade"
}
Which translates to these colors:
But then instead of re-defining these tuples everytime I want to use the palette, I wanted to be able to load them from somewhere when needed with one line of code like palette = load_colors()
.
This would be useful since I can't remember the values of the colors I used in previous projects, so I find myself frequently searching old scripts for them.
When I load the colors, they should also change format to be understood by the framework I'm using:
In tkinter colors are hex strings:
palette["orange"] = '#ce8964'
canvas.create_line(0, 0, 100, 100, fill=palette["orange"])
In pygame they are RGB tuples:
palette["orange"] = (206, 137, 100)
pygame.draw.line(win, palette["orange"], (0, 0), (100, 100))
But I wanted to have orange
be universally understood so that it can be used for any targeted framework.
Is there a way to implement a system like that?
colorir exists for this very purpose.
A palette is a collection of named colors, in this case:
from colorir import *
palette = Palette(
name="my_palette",
orange"="#ce8964",
yellow"="#eaf27c",
green"="#71b48d",
blue"="#454ade"
)
The palette can then be saved with palette.save()
.
And later be loaded (even from a different project) with palette = Palette.load("my_palette")
.
The saving-loading process is entirely managed by colorir
, so you don't have to deal with any files or directories.
Colors are then accessed as such:
>>> palette.orange
Hex('#ce8964')
And they can be provided directly to frameworks (no need to convert to string before):
# Tkinter
win = Tk()
win.config(bg=palette.orange) # Supply orange as background color
win.mainloop()
To ensure that palette
is compatible with other frameworks, you just have to specify the desired format when loading it:
>>> palette = Palette.load("my_palette", PYGAME_COLOR_FORMAT)
>>> palette.orange # This can be passed to pygame functions now
sRGB(206, 137, 100)
colorir
also has features like swatching palettes (to see what they look like), creating palettes from gradients, and many more things that make choosing a color palette for a project much easier.
Disclaimer: I'm the developer of colorir
.