Generating a Random Hex Color in Python

前端 未结 15 1716
悲&欢浪女
悲&欢浪女 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()
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-11-29 02:20
    hex_digits = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
    
    digit_array = []
    
    for i in xrange(6):
        digit_array.append(hex_digits[randint(0,15)])
    joined_digits = ''.join(digit_array)
    
    color = '#' + joined_digits
    
    0 讨论(0)
  • Here is a simple way:

    import random
    color = "%06x" % random.randint(0, 0xFFFFFF)
    

    To generate a random 3 char color:

    import random
    color = "%03x" % random.randint(0, 0xFFF)
    

    %x in C-based languages is a string formatter to format integers as hexadecimal strings while 0x is the prefix to write numbers in base-16.

    Colors can be prefixed with "#" if needed (CSS style)

    0 讨论(0)
  • 2020-11-29 02:26

    Here's a simple code that I wrote based on what hexadecimal color notations represent:

    import random 
    
    def getRandomCol():
    
        r = hex(random.randrange(0, 255))[2:]
        g = hex(random.randrange(0, 255))[2:]
        b = hex(random.randrange(0, 255))[2:]
    
        random_col = '#'+r+g+b`enter code here`
    
        return random_col
    

    The '#' in the hexadecimal color code just represents that the number represented is just a hexadecimal number. What's important is the next 6 digits. Pairs of 2 digits in those 6 hexadecimal digits represent the intensity of RGB (Red, Green, and Blue) each. The intensity of each color ranges between 0-255 and a combination of different intensities of RGB produces different colors.

    For example, in #ff00ff, the first ff is equivalent to 255 in decimal, the next 00 is equivalent to 0 in decimal, and the last ff is equivalent to 255 in decimal. Therefore, #ff00ff in hexadecimal color coding is equivalent to RGB(255, 0, 255).

    With this concept, here's the explanation of my approach:

    1. Generated intensities of random numbers for each of r, g and b
    2. Converted those intensities into hexadecimal
    3. Ignored the first 2 characters of each hexadecimal value '0x'
    4. Concatenated '#' with the hexadecimal values r, g and b intensities.

    Feel free to refer to this link if you wanna know more about how colors work: https://hackernoon.com/hex-colors-how-do-they-work-d8cb935ac0f

    Cheers!

    0 讨论(0)
  • 2020-11-29 02:28
    import random
    
    def get_random_hex:
        random_number = random.randint(0,16777215)
    
        # convert to hexadecimal
        hex_number = str(hex(random_number))
    
        # remove 0x and prepend '#'
        return'#'+ hex_number[2:]
    
    0 讨论(0)
提交回复
热议问题