0

I want to adjust the opacity of the lines drawn by the pen in turtle to 50% but can't seem to find any solution. This link shows no solutions.

It seems like we can't use rgba as the color choice in this library? If so, any library that is recommended to use?

import turtle


t = turtle.Turtle()
s = turtle.Screen()
s.bgcolor('black')
t.pencolor('white')
t.speed('fastest')
t.pensize(5)


for i in range (100):
    t.forward(500)
    t.left(125)

turtle.done()

wish to change the opacity of the lines drawn

mic
  • 155
  • 2
  • 9

1 Answers1

0

You can convert RGBA values to RGB using Certain Formulae. There is a simplified version of this formula if the background is white

My code:

import turtle

t = turtle.Turtle()
s = turtle.Screen()

bg = (0,0,0)

s.bgcolor(bg)
t.pencolor('white')
t.speed('fastest')
t.pensize(3)

fg = (0 / 255, 255 / 255, 0 / 255, 100 / 255)

r = max(min(((1 - fg[3]) * bg[0]) + (fg[3] * fg[0]), 1), 0)
g = max(min(((1 - fg[3]) * bg[1]) + (fg[3] * fg[1]), 1), 0)
b = max(min(((1 - fg[3]) * bg[2]) + (fg[3] * fg[2]), 1), 0)

for i in range (75):
    t.pencolor(r,g,b) 
    t.forward(500)
    t.left(125)

turtle.done()
Achyut-BK
  • 65
  • 7
  • it doesnt seem to work for me, i.e. I cannot make the foreground color is semi transparent relative to any of the background color. Mind to show how? i.e. semi transparent green foreground vs black background? – mic Feb 05 '21 at 07:07
  • If you are using non-black or non-white backgrounds, you need to use a more complex formula [here](https://stackoverflow.com/a/2049362/13656598). I have changed my answer to reflect this – Achyut-BK Feb 05 '21 at 07:54
  • i cant seem to use other rgb values as bg, for bg = (172, 52, 52), theres error risen up: it gives bad color sequence: (172, 52, 52). Also, how would this solution work for non-solid color background, i.e. an image? – mic Feb 05 '21 at 08:24
  • ok, I need to change bg to (172/255, 52/255, 52/255) to make it work. If I want opacity 50%, does that mean i need to input 255*0.5 /255 as 'a' value? – mic Feb 05 '21 at 08:31
  • **If I want opacity 50%, does that mean i need to input 255*0.5 /255 as 'a' value** Yup. You can also use 50 / 100, if you prefer percentages – Achyut-BK Feb 05 '21 at 12:30