I am attempting to generate a new colour and its contrasting colour with this python script.
I plan to try to make a web app that is different every time it is loaded (i.e. background and text colour)
I am wondering if there is a better and more efficient way to do this?
Here is my code:
import random
values = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
value_dict = [['A','10'],['B','11'],['C','12'],['D','13'],['E','14'],['F','15']]
start = "#"
def contrastcolour(colour):
if colour[0] == '#':
colour = colour[1:]
rgb = (colour[0:2], colour[2:4], colour[4:6])
comp = ['%02X' % (255 - int(a, 16)) for a in rgb]
return ''.join(comp)
def startcolour():
colour = "#"
for i in range(6):
x = values[random.randint(0,len(values)-1)]
for thing in value_dict:
if x == thing[1]:
x = thing[0]
colour = colour + x
return colour
base = startcolour()
contrast = start + contrastcolour(base)
print("The colours: {0}".format([base, contrast]))
The sample output is:
The colours: ['#2EF7F3', '#D1080C']