Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Build an xterm color palette procedurally generated from some given color.
"""Colorscheme builder
Future improvements: Twisting the 'hue' knob in HSV
colorspace isn't necessarily the best way to procedurally
choose colors. I'd like to explore the use of other
"perceptual" colorspaces like L*A*B* for calculating
uniform hue spreads.
"""
from datagrok.misc import color
primary=color.Color(html="#d70751") # Debian red
#primary=color.Color(html="#ff6309") # Ubuntu orange
#primary=color.Color(html="#ffb515") # Ubuntu yellow
#primary=color.Color(html="#C90016") # Ubuntu red
#primary=color.Color(html="#77dddd")
spread=1.0 # cluster of hues, '1' for full spectrum
pos=3 # position your color is within the spread, from 0 to 5
tint=0.0 # apply a "tint" to the foreground and background
contrast=.90
dark=True
if primary.l >= .6:
dark=True
if primary.l <= .4:
dark=False
# lightness.... .2 = #333333 .4 = #666666 .6 .8
spread=float(spread)
huedeltas = [(+5-pos-x)*spread/6 for x in range(6)]
huedeltas.sort()
colornames = ["red","green","yellow","blue","magenta","cyan"]
xtermorder = [0,2,1,5,3,4]
# find which standard color 'primary' is closest to
closest = min([(min(abs(primary.h - (n*1.0/6)), abs(primary.h - 1.0 - (n*1.0/6))), n) for n in range(6)])[1]
print '!', closest
colors = [color.Color(hls=(primary.h + h, primary.l, primary.s)) for h in huedeltas]
print '!', huedeltas
print '!', [c.html for c in colors]
for x in range((closest-pos)%6):
colors.insert(0,colors.pop())
# red green yellow blue magenta cyan
for n,f in zip(xtermorder, colors):
if primary.l >= .5:
print "*VT100*color%d: %s" % (n+9, f)
f.l-=contrast*.5*.8
print "*VT100*color%d: %s" % (n+1, f)
else:
print "*VT100*color%d: %s" % (n+1, f)
f.l+=contrast*.5*.8
print "*VT100*color%d: %s" % (n+9, f)
lowerl=(1.0-contrast)*.5
highl=1.0-lowerl
black = color.Color(hls=(primary.h, 1.0-contrast, tint))
white = color.Color(hls=(primary.h, contrast, tint))
if primary.l >= .5:
dkgrey = color.Color(hls=(primary.h, primary.l-contrast*.5*.8, tint))
ltgrey = color.Color(hls=(primary.h, primary.l, tint))
else:
dkgrey = color.Color(hls=(primary.h, primary.l, tint))
ltgrey = color.Color(hls=(primary.h, primary.l+contrast*.5*.8, tint))
print "*VT100*color0: %s" % black
print "*VT100*color7: %s" % ltgrey
print "*VT100*color8: %s" % dkgrey
print "*VT100*color15: %s" % white
if dark:
print "*VT100*background: %s" % black
print "*VT100*foreground: %s" % ltgrey
else:
print "*VT100*background: %s" % white
print "*VT100*foreground: %s" % dkgrey
@kingcons

This comment has been minimized.

Show comment Hide comment
@kingcons

kingcons Oct 30, 2011

For real? God you're awesome. Crazy...but awesome. +1

For real? God you're awesome. Crazy...but awesome. +1

@datagrok

This comment has been minimized.

Show comment Hide comment
@datagrok

datagrok Oct 30, 2011

@redline6561 Ha! Thanks. I should note that this is at least 4 years old, just posting some old junk I had lying around.

Owner

datagrok commented Oct 30, 2011

@redline6561 Ha! Thanks. I should note that this is at least 4 years old, just posting some old junk I had lying around.

@datagrok

This comment has been minimized.

Show comment Hide comment
@datagrok

datagrok Aug 28, 2014

Note to self: employ http://www.boronine.com/husl/

Owner

datagrok commented Aug 28, 2014

Note to self: employ http://www.boronine.com/husl/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment