Skip to content

Instantly share code, notes, and snippets.

@sio
Last active May 8, 2024 13:17
Show Gist options
  • Save sio/5aba6910b7bafa9760132c06ec1522a7 to your computer and use it in GitHub Desktop.
Save sio/5aba6910b7bafa9760132c06ec1522a7 to your computer and use it in GitHub Desktop.
Helper functions for creating Mintty color scheme with Python
"""
Manipulate color schemes for mintty
For simplicity, support only "value = r, g, b" format with decimal values
Copyright 2017 Vitaly Potyarkin
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0
"""
import re
from collections import namedtuple
COLOR_LINE = re.compile(r"^\s*(?P<name>\w+)\s*=\s*"
r"(?P<red>\d+)\s*,\s*"
r"(?P<green>\d+)\s*,\s*"
r"(?P<blue>\d+)\s*$")
RGB = namedtuple("RGB", "red green blue")
def load(filename):
"""Read mintty color theme from file"""
result = dict()
with open(filename) as file:
for line in file:
color = COLOR_LINE.match(line)
if color:
option, *values = color.groups()
result[option] = RGB._make(int(v) for v in values)
return result
def dump(colors, filename):
"""
Write colors dictionary into a file.
colors has to be a mapping where keys are valid minttyrc options, and
values are iterables of three items corresponding to decimal RGB values.
"""
TEMPLATE = "{color: <20} = {red: >3}, {green: >3}, {blue: >3}\n"
with open(filename, "w", newline="\n") as file: # newlines matter for mintty
for name, value in colors.items():
rgb = dict(zip("red green blue".split(), value))
file.write(TEMPLATE.format(color=name, **rgb))
def valid_changer(changer):
"""Add validation to changer function"""
def change(value):
return min(255, max(0, int(changer(value))))
return change
def change_all(colors, changer):
"""Apply changer() function to each value in colors dictionary"""
change = valid_changer(changer)
new_colors = dict()
for color, rgb in colors.items():
new_colors[color] = RGB._make(change(x) for x in rgb)
return new_colors
def change_by_name(colors, changer, name):
"""Apply changer() only to colors that match name"""
colors_done = dict()
colors_to_change = dict()
for color, value in colors.items():
if name.lower() in color.lower():
colors_to_change[color] = value
else:
colors_done[color] = value
colors_done.update(change_all(colors_to_change, changer))
return colors_done
BackgroundColour=255,255,255
ForegroundColour=0,0,0
CursorColour=0,0,0
Black=0,0,0
BoldBlack=58,58,58
Red=135,55,55
BoldRed=135,1,0
Green=55,96,55
BoldGreen=0,95,0
Yellow=175,135,96
BoldYellow=216,134,95
Blue=95,135,174
BoldBlue=0,135,175
Magenta=135,95,135
BoldMagenta=135,2,95
Cyan=95,135,135
BoldCyan=0,135,135
White=100,100,100
BoldWhite=120,120,120
BackgroundColour=255,255,255
ForegroundColour=0,0,0
CursorColour=0,0,0
Black=0,0,0
BoldBlack=58,58,58
Red=135,55,55
BoldRed=135,1,0
Green=55,96,55
BoldGreen=0,95,0
Yellow=175,135,96
BoldYellow=216,134,95
Blue=95,135,174
BoldBlue=0,135,175
Magenta=135,95,135
BoldMagenta=135,2,95
Cyan=95,135,135
BoldCyan=0,135,135
White=228,228,228
BoldWhite=238,238,238
Black = 0, 0, 0
Yellow = 166, 118, 66
Blue = 54, 87, 165
CursorColour = 27, 27, 27
ForegroundColour = 27, 27, 27
BoldRed = 173, 118, 135
Magenta = 132, 54, 165
BoldBlue = 118, 135, 173
BoldWhite = 183, 183, 183
BoldGreen = 135, 173, 118
Green = 87, 165, 54
Cyan = 54, 165, 132
Red = 165, 54, 87
White = 155, 155, 155
BoldYellow = 173, 141, 124
BoldCyan = 118, 173, 157
BackgroundColour = 255, 255, 255
BoldBlack = 27, 27, 27
BoldMagenta = 157, 118, 173
BackgroundColour=255,255,255
ForegroundColour=0,0,0
CursorColour=0,0,0
Black=0,0,0
BoldBlack=29,29,29
Red=115,66,30
BoldRed=222,166,127
Green=30,115,66
BoldGreen=127,222,166
Yellow=80,115,30
BoldYellow=182,222,127
Blue=66,30,115
BoldBlue=166,127,222
Magenta=115,30,80
BoldMagenta=222,127,182
Cyan=30,80,115
BoldCyan=127,182,222
White=202,202,202
BoldWhite=237,237,237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment