Skip to content

Commit

Permalink
Python palette creator
Browse files Browse the repository at this point in the history
  • Loading branch information
FredTingaud authored and Frederic Tingaud committed Jun 11, 2017
1 parent 5742164 commit fbdda08
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tools/generatePalette.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/python2
import sys
import getopt

try:
import hsluv
except ImportError:
import pip
pip.main(['install', 'hsluv'])
import hsluv

def colorMaker(refColor, maxNum):
def nextColor(val):
nextHue = (refColor[0] + val * 360 / maxNum) % 360
return hsluv.hsluv_to_hex([nextHue, refColor[1], refColor[2]])
return nextColor

def generateGrowingList(refColor, maxNum):
nextColor = colorMaker(refColor, maxNum)
return list(map(nextColor, range(1, maxNum)))

def quotes(line):
return '"' + line + '"'

def printPalette(palette):
print('const PALETTE = [')
print(",\n".join(map(quotes, palette)))
print('];')

def cssPrinter(color, i):
return '.linked-code-decoration-inline-' + str(i) + ' {\n background: ' + color + ' !important;\n}\n'

def printCSS(palette):
print( "\n".join([cssPrinter(x, i) for i,x in enumerate(palette)]))

usage = 'Usage: generatePalette.py -c <referencecolor> -n <number>'

def main(argv):
color = "#5ed9cd"
number = 5
try:
opts, args = getopt.getopt(argv,"hc:n:",["color=","number="])
except getopt.GetoptError:
print(usage)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(usage)
sys.exit()
elif opt in ("-c", "--color"):
color = '#' + arg
elif opt in ("-n", "--number"):
number = int(arg)

refColor = hsluv.hex_to_hsluv(color)
palette = [color] + generateGrowingList(refColor, number)
printPalette(palette)
print('')
printCSS(palette)

if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit fbdda08

Please sign in to comment.