TAGS :Viewed: 3 - Published at: a few seconds ago

[ Creating hex color series ]

Is there a way to create a series of hex colors in python not mannually, for example ranging from dark green through light green and light yellow to dark yellow. Something like the following but not writing down all the hex code:

#from dark yellow ---> dark green
colors = ['#ffff00','#ffffcc','#d6f5d6','#99e699','#85e085','#70db70','#5cd65c','#47d147','#33cc33','#2eb82e','#248f24','#1f7a1f','#196619','#145214','#0f3d0f']

I would imagine something like range() for numbers.

Answer 1


Well the hex color is just a hex representation of 3 decimal values(0 to 255). For instance for green you could do something like:

>>> ["#{val}FF{val}".format(val=hex(i)[2:].zfill(2)) for i in range(55, 255, 50)]
['#37FF37', '#69FF69', '#9bFF9b', '#cdFFcd']