Generating Random Colors in Python
In the digital world, colors are represented in different formats. RGB, Hexadecimal format are just some of the commonly used formats.
In this tutorial, we will learn how to generate random colors in Python. When we talk about generating random colors, we are going to generate a random code that can represent a color. Different methods will generate color codes in different formats.
Generate random colors in RGB format in Python
RGB stands for Red, Green, and Blue. Together, they represent the color spectrum in the digital world. Red, Green, and Blue together can represent every color, and each color is 8 bits. This means that they have integer values from 0 to 255.
To generate a random color in RGB format, we will generate a list or tuple of random integers from 0 to 255.
The following code shows how to achieve this.
import numpy as np
color = list(np.random.choice(range(256), size=3))
print(color)
Output:
[245, 159, 34]
In the code above, we generate random integers using the random_integer_s NumPy
from the module random
. It simply generates a random integer between 0 and 255 three times and stores it in a list. Since random integers can be generated in many other ways, the main focus should be on the logic of the code.
Generate random colors in hexadecimal format in Python
In hexadecimal, colors are represented by six hexadecimal digits prefixed with a # sign. The format is #RRGGBB
, where R, G, and B represent Red
, Green
and Blue
, respectively, and are hexadecimal numbers.
We can generate random colors in this format using the code shown below.
import random
color = ["#" + "".join([random.choice("0123456789ABCDEF") for j in range(6)])]
print(color)
Output:
['#BE3559']
In the above code, we select six random numbers from the specified hexadecimal number and then combine join()
them with #
the symbol using the function.
There are many other color formats available, and converting between them is very easy.
One thing to remember is that we generated color codes in different formats throughout this tutorial. To actually see these colors, we have to generate some graphics using other modules or draw some graphs.
For example, in the following code, we will use Matplotlib
the scatter plot module to plot simple points with the generated color codes.
import random
import matplotlib.pyplot as plt
color = ["#" + "".join([random.choice("0123456789ABCDEF") for j in range(6)])]
print(color)
plt.scatter(random.randint(0, 10), random.randint(0, 10), c=color, s=200)
plt.show()
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Writing logs to a file in Python
Publish Date:2025/05/06 Views:132 Category:Python
-
This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file
Comparing two dates in Python
Publish Date:2025/05/06 Views:97 Category:Python
-
This tutorial explains how to compare two dates in Python. There are multiple ways to determine which date is greater, so the tutorial also lists different sample codes to illustrate the different methods. Comparing two dates in Python usin
Reload or unimport modules in Python
Publish Date:2025/05/06 Views:58 Category:Python
-
Modules allow us to store definitions of different functions and classes in Python files, which can then be used in other files. pandas , NumPy , scipy , Matplotlib are the most widely used modules in Python. We can also create our own modu
Pausing program execution in Python
Publish Date:2025/05/06 Views:156 Category:Python
-
This tutorial will demonstrate various ways to pause a program in Python. Pausing the execution of a program or application is used in different scenarios, such as when a program requires user input. We may also need to pause the program fo
Importing modules from a subdirectory in Python
Publish Date:2025/05/06 Views:190 Category:Python
-
This tutorial will explain various ways to import modules from subdirectories in Python. Suppose we have a file in a subdirectory of our project directory and we want to import this file and use its methods in our code. We can import files
Sleeping for a number of milliseconds in Python
Publish Date:2025/05/06 Views:124 Category:Python
-
In this tutorial, we will look at various ways to pause or suspend the execution of a program in Python for a given amount of time. Let's say we want to pause the execution of a program for a few seconds to let the user read instructions ab
Python Numpy.pad Function
Publish Date:2025/05/06 Views:104 Category:Python
-
In Python, we have NumPy the array module to create and use arrays. Arrays can have different sizes and dimensions. Padding is a useful method that can be used to compensate for the size of an array. We can mutate an array and add some padd
If not statement in Python
Publish Date:2025/05/06 Views:123 Category:Python
-
The statement in Python if checks a specific condition and executes a block of code if the condition is true. if not The does the opposite of if the statement. It tests if a condition is false and then executes some statements. Using if not
Enumerating a dictionary in Python
Publish Date:2025/05/05 Views:98 Category:Python
-
The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()