Generating a Random Hex Color in Python

前端 未结 15 1783
悲&欢浪女
悲&欢浪女 2020-11-29 01:36

For a Django App, each \"member\" is assigned a color to help identify them. Their color is stored in the database and then printed/copied into the HTML when it is needed. T

15条回答
  •  温柔的废话
    2020-11-29 02:11

    Basically, this will give you a hashtag, a randint that gets converted to hex, and a padding of zeroes.

    from random import randint
    color = '#{:06x}'.format(randint(0, 256**3))
    #Use the colors wherever you need!
    

提交回复
热议问题