Coders Packet

Generate Random Gradient Color Code in Python

By Vijay Viswha

This program code generates a random color gradient code using random module in Python using.

Hello, this program code generates a random gradient color code using random module in Python. The random module is used for various functions that need the generation of random entities. The numbers created occur randomly and do not follow any type of algorithm for its generation. So we begin the program by importing the required module which is the random module. Now we can use the built-in module to perform actions using the built-in functions.

The program starts with declaring a function 'random_gradient()' in which we assign the returned values of another function 'random_color()' to two colors using which we generate our gradient color. We also assign the returned value of another function 'gardient_color()' whose arguments are colors generated from random_color() to 'color_gradient'. And the function ends by returning all 3 assigned values.

Next up, we define the 'random_color()' function. Inside the function, we use the random.randint built-in function 3 times to create random values within the range of 0 to 255(inclusive of 255) for the color variables red, green, and blue. And we return the randomly generated color values to the random_gradient() function.

After that, we define the function gradient_color() with two argument, color1 and color2. Then, we extract and assign individual red, green and blue of both colors to appropriate variables. We take the average of the extracted colors from both color1 and color2 and assign them to gradient variables and return them to the random_gradient() function.

Outside the function, we assign the 3 returned values from the random_gradient() function to three variables, color1, color2, and color_gradient. And we also format the color codes to hexadecimal values. The 02 represents the number of digits, the capital X represents capital Hexa values, and # for format. The formatted value is assigned respective variables.

 

test output of packet

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Vijay Viswha (viswha19123)

Download packets of source code on Coders Packet