Python

Generate Random RGB Colors in Python

Colors are represented in several formats in the digital world. The RGB and Hexadecimal formats are just two of the most popular. This lesson will discuss how to generate random RGB colors in Python. We shall construct a random code that can represent color when we talk about producing random colors. Color codes will be generated in a variety of forms using various approaches. Now we’ll look at how to use Python to generate random colors. Colors will be generated in two formats. The first is RGB, and the second is hexadecimal. We use those two forms a lot in the digital world. Colors are typically portrayed in a variety of ways. Numpy, Matplotlib, and turtle are examples of Python libraries that can be used to generate a color.

The RGB color system’s three primary hues are red, green, and blue. Its value ranges from 0 to 255. These three colors can be combined to produce different colors. In the hexadecimal format, the #symbol is followed by six hexadecimal numbers. RGB colors are hexadecimal colors. To acquire a random color, the random() method is used. Random() is a commonly used Python module that can produce random integers or colors. It’s helpful to pick things at random and shuffle the items in the list.

Example 1:

To get the random colors, first import a random module. After that, you’ll need to make a for loop that iterates 20 times to acquire 20 different colors. The join() function is then used to join the # and color code. The Python String join() function is a string technique that returns a string with the str separator used to connect the sequence’s components. The letter # is utilized to start the color code. To iterate, use the for loop. The color codes have now been created.

import random
for j in range(20):
    colors = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]
    print(colors)

https://lh4.googleusercontent.com/4DPuOCgWF9K780_0NaNuUmSkXvwk54sUtetyz0JLKSafhws3TrAYR-uDg3D42iwaEegk2O1HrAGGSI_UWofs8nJSuS4q2QqZ8110c7QibKJK4YSgQUJlIYupe-WnAhDNZazi20rG

Below you can see that the color codes are successfully generated.

https://lh3.googleusercontent.com/TurZW_BlTgWVdkYMhaeg_38Mia2tiUq6ENWwa2TFlDvNGrQ-cViGEyN5kBUq0w3zLUHi2ccil9bh3GNTHfjHPVK_78R15WzCfTNKZnqkzUcI848FeNC2J_R17do0AYIUX7YeNh7y

Example 2:

In this instance, we will talk about the hexadecimal number system. A 16-character, also known as a symbol numbering system, is the hexadecimal number system. It’s also known as hex in the Python language. This example will use a random module to generate a random color in hexadecimal format. The join() function is being utilized to link the # and color code. The letter # will be used to start the color code. To iterate, use the for loop. The color code has now been produced. There are many additional color formats to choose from, and converting between them is simple. One thing to keep in mind is that too generated color codes in a variety of forms can be done. To view these colors, you have to use additional modules to create a visual or plot a graph.

https://lh6.googleusercontent.com/DVyRuYUz41kuEsDqBS3WXBZ74T2pag3bsm_WwQcFJ2t7YwGV_cRP1D3Up0it9nrdLXyq-UJCyJw4KrIEICo_dguGVJ_wb35FV-EvcvwRIvVuOstNw02mF7_EizFB-RW4mr5YaCnr

The output shows that the color is produced following the code above.

https://lh6.googleusercontent.com/fAmOgTluikJWLCXAavkVTytW76YBqaV5N-FC5XIWgUmQqJcdwLVJvcVTWVmkFlzXk8dxbA2BQNPFpnBV7N4zSX99g7ffNY-zQkSxvlC8Joe1_4V07sAcOLKnxSDTsIOGUiOl7f8u

Example 3:

Here we will show you how to generate random colors with the help of the matplotlib library. Matplotlib is a data imagining and graphical scheming package for Python and its arithmetical extension NumPy that runs on all platforms. As a result, it provides an open-source alternative to MATLAB. Matplotlib’s APIs can also be used to embed plots in graphical user interfaces. Importing the matplotlib library is the first step. After that, you’ll need to ingress a random module. Next, put a value in the no of colors variable.

The join() function is then used to link the # and color code. The letter # will always be used to start the color code. To iterate, use the for loop. The color code has now been produced. The color will be available in the search because we decided to declare it as a list—next, print a random color. Other than these methods, the turtle module can also be used for making random colors in Python. Turtle is a module in Python that comes pre-installed that enables users to apply a virtual canvas to create graphics and shapes. It is a reimplementation of the same-named module included in the Python standard package until Python 2.5. The turtle is the name of the onscreen pen that you use to draw with, and it is also the name of the library.

import matplotlib.pyplot as pt
import random
colors=4
color=["#"+''.join([random.choice('0123456789ABCDEF') for i in range(6)])
       for j in range(colors)]
print(color)
for j in range(colors):
pt.scatter(random.randint(0,10),random.randint(0,10),c=color[j],s=200)
pt.show()

https://lh6.googleusercontent.com/Rw5cgK2b5spVo9hSiXLWujW7krthaspPGa3fd7gGfl4QttY7IAVUlv99gnYzGrsF_pynlnFZlc3YhtF2SlyIZAdPVv3_QaGu1zup7unF7hZlLvB7fJtXyiG7d2q_5zFMxNPT_clp

The above code generated this output.

https://lh5.googleusercontent.com/bUzMhM_IwkUEVMOztxwoIQCCrzVvb8eRVYUi0-uphpdBKHHUf7jV1N0tygP-gmjNqnR9gNVDhTk06M2d7u0m51FGiAYn5-fCCE1y-ChAqnQ1YARFrlyMJcF4Oq6Q7BS_3xZqZBfs

Conclusion:

In all browsers, RGB color values are supported. The RGB color code is used to specify an RGB color value (red, green, blue). Each of the three parameters (red, green, and blue) specifies the color’s intensity as a number between 0 and 255. This lesson covered how to generate random RGB colors in Python. To generate colors, we used a variety of methods and formats. These are the ways for generating random colors that are accessible.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content