This is a simple example of how to generate random colors in RGB, RGBA, and Hex formats in Python.
Feel free to use this as a starting point for your own projects!(Credits are appreciated, but not required.)
import random
def random_rgb_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return f"rgb({r}, {g}, {b})"
print(random_rgb_color()) # => rgb(222, 67, 93)
import random
def random_rgba_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
a = random.random() # 0.0 - 1.0
return f"rgb({r}, {g}, {b}, {a:.2f})"
print(random_rgba_color()) # => rgb(238, 225, 138, 0.29)
import random
def random_hex_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return f"#{r:02x}{g:02x}{b:02x}"
print(random_hex_color()) # => #7869de