Simple Python Turtle Graphic and Code: Color Array

preview_player
Показать описание
A grid-like structure made with color boxes that show slightly-differentiated colors. In this case, the value of r (red) in the (r, g, b) was fixed at 1 while those of g (green) and b (blue) increase from 0 to 1 in constant increment of 0.1. The g values are 0, 0.1, 0.2, ,,, from left to right, while those for b are the same from top to bottom. That makes the color box on the upper left (1, 0, 0), upper right (1, 1, 0), lower left (1, 0, 1), and lower right (1, 1, 1).

Feel free to copy the simple Python Turtle code that is given below. Don't hesitate to ask questions about the code if you have any. Enjoy! Please comment, like, or subscribe :)

import turtle
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
number_of_g_values = number_of_b_values = 11 #Changeable to an integer close to 11
r = 1 #Fixed value for red (Note that in this case, red is the color with
#fixed value. If blue or green is fixed instead, changes in the
#variables involving the remaining colors need to be done. For
#example, if the value of blue is fixed, change variables to
#reflect corresponding adjustments for red r and green g.)
g = b = 0 #Initial value for green and blue, the colors other than the fixed red
graphic_length = 600 #Dimension of graphic from left to right; Changeable
graphic_height = 500 #Dimension of graphic from top to bottom; Changeable
change_in_g = 1 / (number_of_g_values - 1) #Stepwise increment in value of g
change_in_b = 1 / (number_of_b_values - 1) #Stepwise increment in value of b; (In this case, r value is fixed.)
change_in_y = graphic_height / (number_of_b_values) #Stepwise decrease in y-coordinate to position next color box downward
change_in_x = graphic_length / (number_of_g_values) #Stepwise increase in x-coordinate to position next color box rightward.
def color_box(): #Procedure for drawing a color box with indicated r, g, b values
for i in range(2): #Loop to draw 2 pairs of sides for current color box
for j in range(number_of_g_values): #Python_Graphic start of drawing procedure
g = j * change_in_g #Assignment of color value for green using the loop index j
for k in range(number_of_b_values): #Embedded loop to draw color boxes with given g and variable b's
b = k * change_in_b #Assignment of color value for blue using k, the index of embedded loop
#to upper-left vertex of new color box
color_box() #Function call to draw a color-filled box defined by updated r, g, and b
Рекомендации по теме
Комментарии
Автор

For manually colorable graphics, please visit

basicpythonturtleart
visit shbcf.ru