last 8 colors now vary in luminance to make them more differentiable

This commit is contained in:
whut 2021-09-14 12:53:50 -05:00
parent 561544ffa2
commit 75ce2daab1
2 changed files with 23 additions and 11 deletions

View File

@ -3,6 +3,6 @@ It makes base16 schemes. Pass a background color, an accent color, and optionall
Color arguments are in the form of "hue,sat,lum" for background and "hue,sat" for the other two. All values must be numbers between 0.0 and 1.0 (you're welcome to try other values, just don't be shocked if it breaks). Also, keep in mind that luminance here is *relative* luminance, or the color's brightness as perceived by the human eye. This is different from the usual HSL space. Color arguments are in the form of "hue,sat,lum" for background and "hue,sat" for the other two. All values must be numbers between 0.0 and 1.0 (you're welcome to try other values, just don't be shocked if it breaks). Also, keep in mind that luminance here is *relative* luminance, or the color's brightness as perceived by the human eye. This is different from the usual HSL space.
The script will do its best to make sure everything is readable. It tries to keep a contrast ratio of 7:1 between the foreground and background colors, while trying to stay above 3:1 between foreground and highlight, the "rainbow" colors (i.e. the last 8 colors) and highlight, and the "dark" foreground and background. Usually it does a good job. But it tends not to like a background luminance that hovers in the middle, that's when stuff gets to be an eyesore. Keep your background close to 1.0 or 0.0, that's when it looks okay. The script will do its best to make sure everything is readable. It tries to keep a contrast ratio of 7:1 between the foreground and background colors, while trying to stay above 3:1 between foreground and highlight, the "rainbow" colors (i.e. the last 8 colors) and highlight, and the "dark" foreground and background. Usually it does a good job. But it tends not to like a background luminance that hovers around 0.3, that's when stuff gets to be an eyesore.
For the last 8 colors, you can make them blend with the background hue by passing `-r (amount)`, the amount being between 0.0 and 1.0. 0.0 is no blending, 1.0 makes them all the same hue. For the last 8 colors, you can make them blend with the background hue by passing `-r (amount)`, the amount being between 0.0 and 1.0. 0.0 means no blending, 1.0 makes them all the same hue.

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import colorsys, optparse, collections import colorsys, optparse
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
parser = optparse.OptionParser(usage="usage: %prog [options] background accent [foreground]") parser = optparse.OptionParser(usage="usage: %prog [options] background accent [foreground]")
@ -45,6 +45,9 @@ except:
def lerp(a, b, t): def lerp(a, b, t):
return a + (b - a) * t return a + (b - a) * t
def reposition(a, b, t, c, d):
return lerp(c, d, (t - a) / (b - a))
def mixColors(a, b, t): def mixColors(a, b, t):
return tuple(lerp(a[i], b[i], t) for i in range(3)) return tuple(lerp(a[i], b[i], t) for i in range(3))
@ -93,11 +96,17 @@ lastTwo = [lerp(foreLum, 1.0 if foreLum > bgLum else 0.0, (x + 1) / 3) for x in
foreLight = makeColor(foreHue, lastTwo[0], foreSat) foreLight = makeColor(foreHue, lastTwo[0], foreSat)
backLight = makeColor(bgHue, lastTwo[1], bgSat) backLight = makeColor(bgHue, lastTwo[1], bgSat)
rainHues = [0, 20, 55, 120, 180, 240, 280, 330] rainHues = [0, 20, 60, 120, 180, 240, 300, 20]
backFull = colorsys.hls_to_rgb(bgHue, 0.5, bgSat) backFull = colorsys.hls_to_rgb(bgHue, 0.5, bgSat)
rainbow = [mixColors(colorsys.hls_to_rgb(hue/360.0, 0.5, 1.0), backFull, rainbowBlend) for hue in rainHues] rainbow = [mixColors(colorsys.hls_to_rgb(hue/360.0, 0.5, 1.0), backFull, rainbowBlend) for hue in rainHues]
rainbow = [makeColor(colorsys.rgb_to_hls(*col)[0], getContrast(bgLum, 3.5), 1.0) for col in rainbow]
rainLums = [luminance(col) for col in rainbow]
hiLum, loLum = max(rainLums), min(rainLums)
minLum, maxLum = sorted((getContrast(bgLum, 4), getContrast(bgLum, 6)))
rainHLS = [colorsys.rgb_to_hls(*col) for col in rainbow]
rainbow = [makeColor(rainHLS[c][0], reposition(loLum, hiLum+0.00001, rainLums[c], minLum, maxLum) if c < 7 else minLum, rainHLS[c][2]) for c in range(len(rainbow))]
cols = [background, backLighter, accent, comment, foreDark, foreground, foreLight, backLight] + rainbow cols = [background, backLighter, accent, comment, foreDark, foreground, foreLight, backLight] + rainbow
@ -109,21 +118,24 @@ for i in range(len(cols)):
if options.showPreview: if options.showPreview:
font = ImageFont.truetype("/usr/share/fonts/TTF/LiberationMono-Regular.ttf", 20) font = ImageFont.truetype("/usr/share/fonts/TTF/LiberationMono-Regular.ttf", 20)
h, w = imSize = (640, 640)
im = Image.new("RGB", (640, 640), usable(background)) im = Image.new("RGB", (640, 640), usable(background))
draw = ImageDraw.Draw(im) draw = ImageDraw.Draw(im)
desc = lambda t, a, b: "{} (contrast {:.1f}:1)".format(t, contrast(luminance(a), luminance(b)))
text = lambda x, y, t, col, other: draw.text((x, y), desc(t, col, other), usable(col), font)
draw.rectangle((2, 2, 638, 32), usable(accent)) draw.rectangle((2, 2, 638, 32), usable(accent))
draw.text((5, 5), "Foreground (contrast {:.1f}:1 w/ highlight)".format(contrast(luminance(foreground), luminance(accent))), usable(foreground), font) text(5, 5, "Foreground on highlight", foreground, accent)
draw.text((5, 45), "Foreground (contrast {:.1f}:1 w/ background)".format(contrast(luminance(foreground), luminance(background))), usable(foreground), font) text(5, 45, "Foreground on background", foreground, background)
draw.text((5, 85), "# Comment color (contrast {:.1f}:1)".format(contrast(luminance(comment), luminance(background))), usable(comment), font) text(5, 85, "# Comment color", comment, background)
for i in range(len(rainbow)): for i in range(len(rainbow)):
draw.rectangle((4, 120+i*40, 84, 150+i*40), usable(rainbow[i])) draw.rectangle((4, 120+i*40, 84, 150+i*40), usable(rainbow[i]))
draw.text((90, 125+i*40), "Text (contrast 3.5:1)".format(contrast(luminance(rainbow[i]), luminance(background))), usable(rainbow[i]), font) text(90, 125+i*40, "Colored text", rainbow[i], background)
draw.rectangle((0, 560, 640, 600), usable(backLighter)) draw.rectangle((0, 560, 640, 600), usable(backLighter))
draw.text((5, 565), "'Dark' foreground (contrast {:.2f}:1)".format(contrast(luminance(backLighter), luminance(foreDark))), usable(foreDark), font) text(5, 565, "'Dark' foreground", foreDark, backLighter)
draw.line((0, 598, 640, 598), usable(foreground), 2) draw.line((0, 598, 640, 598), usable(foreground), 2)
for i in range(len(cols)): for i in range(len(cols)):