Generating a Random Hex Color in Python

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

    import random
    
    def hex_code_colors():
        a = hex(random.randrange(0,256))
        b = hex(random.randrange(0,256))
        c = hex(random.randrange(0,256))
        a = a[2:]
        b = b[2:]
        c = c[2:]
        if len(a)<2:
            a = "0" + a
        if len(b)<2:
            b = "0" + b
        if len(c)<2:
            c = "0" + c
        z = a + b + c
        return "#" + z.upper()
    

提交回复
热议问题