hue blend target now selectable

This commit is contained in:
whut 2022-07-09 10:56:42 -05:00
parent 9900c2626a
commit a0ff7dd482
2 changed files with 22 additions and 9 deletions

View File

@ -30,7 +30,8 @@ Here's the preview it'd generate:
- `-a author`: Set the author name.
- `-b amount`: Make the hue colors blend with the background's hue. 0.0 (default) means no blending, 1.0 makes them the same exact color. If you'd like to have them be the same hue but with different brightnesses, use a value like 0.999.
- `-b amount`: Make the hue colors blend with the color specified by `-B`. 0.0 (default) means no blending, 1.0 makes them the same exact color. If you'd like to have them be the same hue but with different brightnesses, use a value like 0.999.
- `-B target`: What color to blend the hues with: `foreground`, `background`, `accent`.
- `-c contrast`: Desired contrast ratio between the foreground and background colors. Default is 7. 21 guarantees the highest possible contrast (i.e. the foreground will either be white or black).
- `-e`: Equiluminant mode; uses the same luminance for the hues. This keeps their brightness uniform but can make them harder to tell apart.
- `--hsl`: Treat the background color as being gamma-encoded HSL (i.e. what most people are used to). Wouldn't recommend it, but if it works for you...

View File

@ -4,6 +4,7 @@ from PIL import Image, ImageDraw, ImageFont
modes = ["base16","xresources"]
RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA = 0, 60, 120, 200, 240, 300
blendTargets = ["background","foreground","accent"]
parser = optparse.OptionParser(usage="usage: %prog [options] background accent [foreground]")
parser.add_option("-a", "--author",
@ -16,6 +17,11 @@ parser.add_option("-b", "--hue-blend",
dest="hueBlend",
default=0.0,
help="mix ratio between hues and background")
parser.add_option("-B", "--hue-blend-target",
action="store",
dest="hueBlendTarget",
default="background",
help="mix ratio between hues and background")
parser.add_option("-c", "--contrast",
action="store",
dest="foreContrast",
@ -57,10 +63,13 @@ base16Options.add_option("-l", "--light-brown",
parser.add_option_group(base16Options)
(options, args) = parser.parse_args()
if options.mode not in modes:
parser.error("unknown mode {}".format(options.mode))
if len(args) < 2:
parser.error("need background and accent colors minimum")
def dieIf(cond, message):
cond and parser.error(message)
dieIf(options.mode not in modes, "unknown mode \"{}\"".format(options.mode))
dieIf(len(args) < 2, "need background and accent colors minimum")
dieIf(options.hueBlendTarget not in blendTargets, "unknown hue blend target \"{}\"".format(options.hueBlendTarget))
def parseTuple(st):
return tuple(float(x) for x in st.split(","))
@ -106,7 +115,7 @@ accentHue, accentSat = parseTuple(args[1])
foreHue, foreSat = parseTuple(args[2]) if len(args) > 2 else (0.0, 0.0)
hueBlend = floatOrDie(options.hueBlend, "rainbow blend must be a number")
hueBlend = floatOrDie(options.hueBlend, "hue blend must be a number")
def lerp(a, b, t):
return a + (b - a) * t
@ -160,7 +169,10 @@ def constrain(orig, compare, limit, mode=CONSTRAIN_UPPER):
return lumFromContrast(compare, limit) if comp(contrast(orig, compare), limit) else orig
def genRainbow(hues, minCon, maxCon):
backFull = colorsys.hls_to_rgb(bgHue, 0.5, bgSat)
target = {"background": (bgHue,bgSat),
"foreground": (foreHue, foreSat),
"accent": (accentHue, accentSat)}[options.hueBlendTarget]
backFull = colorsys.hls_to_rgb(target[0], 0.5, target[1])
rainbow = [mixColors(colorsys.hls_to_rgb(hue/360.0, 0.5, 1.0), backFull, hueBlend) for hue in hues]
@ -182,8 +194,8 @@ def printBanner(commentChar):
say("background {}: {} {} {}".format("HSL" if options.hsl else "HCL", bgHue, bgSat, origLum))
say("accent HC : {} {}".format(accentHue, accentSat))
say("foreground HC : {} {}".format(foreHue, foreSat))
say("hue blend : {}".format(hueBlend))
say("fg contrast : {}".format(foreContrast))
sayIf(hueBlend > 0.0, "hue blend : {}, with {}".format(hueBlend, options.hueBlendTarget))
sayIf(foreContrast != 7, "fg contrast : {}".format(foreContrast))
sayIf(options.equiluminant, "equiluminant hues")
sayIf(options.hsl, "using gamma-encoded HSL")