Generating a Random Hex Color in Python

前端 未结 15 1785
悲&欢浪女
悲&欢浪女 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:17

    This has been done before. Rather than implementing this yourself, possibly introducing errors, you may want to use a ready library, for example Faker. Have a look at the color providers, in particular hex_digit.

    In [1]: from faker import Factory
    
    In [2]: fake = Factory.create()
    
    In [3]: fake.hex_color()
    Out[3]: u'#3cae6a'
    
    In [4]: fake.hex_color()
    Out[4]: u'#5a9e28'
    

提交回复
热议问题