Docsity

Turtle Graphics Art with HSV Color Conversion in Python, Essays (high school) of Computer science

Toggle sidebar
bg1
import turtle as tur
screen = tur.Screen()
screen = tur.Screen()
screen.setup(width=5600 , height=4800)
# Custom HSV to RGB function (returns values in 0โ€“255) this is also the regualar rgb
values so dont mess with it
def hsv_to_rgb(h, s, v):
h = float(h)
s = float(s)
v = float(v)
h60 = h * 6
h60f = int(h60)
f = h60 - h60f
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
if h60f == 0:
r, g, b = v, t, p
elif h60f == 1:
r, g, b = q, v, p
elif h60f == 2:
r, g, b = p, v, t
elif h60f == 3:
r, g, b = p, q, v
elif h60f == 4:
r, g, b = t, p, v
elif h60f == 5:
r, g, b = v, p, q
else:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Turtle Graphics Art with HSV Color Conversion in Python and more Essays (high school) Computer science in PDF only on Docsity!

import turtle as tur screen = tur.Screen() screen = tur.Screen() screen.setup(width=5600 , height=4800)

Custom HSV to RGB function (returns values in 0โ€“255) this is also the regualar rgb

values so dont mess with it def hsv_to_rgb(h, s, v): h = float(h) s = float(s) v = float(v) h60 = h * 6 h60f = int(h60) f = h60 - h60f p = v * (1 - s) q = v * (1 - f * s) t = v * (1 - (1 - f) * s) if h60f == 0: r, g, b = v, t, p elif h60f == 1: r, g, b = q, v, p elif h60f == 2: r, g, b = p, v, t elif h60f == 3: r, g, b = p, q, v elif h60f == 4: r, g, b = t, p, v elif h60f == 5: r, g, b = v, p, q else:

r, g, b = 0, 0, 0 return int(r * 255), int(g * 255), int(b * 255)

Turtle setup (0 is the fastest and width 2-7 is good)

tur.speed(0) tur.width(6) screen.bgcolor("black")

Drawing loop (j in rang(25 or 50) for i ing range( has to be 15 to work)

for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(750,0) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color)

tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(0,-750) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(1500,0) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90)

tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(-1500,0) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(0,1500) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90)

tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(-2250,0) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(0,2250) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90)

tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(0,-2250) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(2250,2250) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90)

tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.penup() tur.goto(-2250,-2250) tur.pendown() for j in range(50): for i in range(15): color = hsv_to_rgb(i / 15, j / 25, 1) tur.color(color) tur.right(90) tur.circle(200 - j * 4, 90) tur.left(90) tur.circle(200 - j * 4, 90) tur.left(180) tur.circle(50, 24) tur.hideturtle() tur.done()