Created
December 22, 2020 16:49
-
-
Save Benbb96/3bd7d8fc15560898a5cbf99520ec9d74 to your computer and use it in GitHub Desktop.
Use this function to generate a random HEX color in Python. You can set limit for the brightest or darkest possible colors thanks to the parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
def generate_random_hex_color(minimum=50, maximum=255): | |
""" | |
Generate a random HEX color (with a minimum brightness by default) | |
:param int minimum: the minimum value of each color (Red, Green, Blue) | |
:param int maximum: the maximum value of each color | |
:return: for example #B4F714 | |
:rtype: str | |
""" | |
assert 0 <= minimum <= 255 and 0 <= maximum <= 255 | |
assert minimum <= maximum | |
return '#%02X%02X%02X' % tuple(randint(minimum, maximum) for _ in range(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment