0

I am using python under the turtle module. In this code I am drawing multiple squares at a certain point of the window. However, I would like to change the colors of the lines that is dependent on the value I want to give. For example, from value 0-10 I want it to be blue, and from 11-20 I want it to be red. I am not familiar with turtle. Henceforth, I am not too sure how it works. Is there another module or extension I could use in python to help draw out colored lines that are based on values?

Screen = turtle.Screen()
Screen.bgcolor('White')
Screen.setup(width= 1200, height= 700, startx= 200, starty =0) 

#First Square
turtle.colormode(255)
my_pen = turtle.Turtle()
my_pen.color(randint(0,255),randint(0,255),randint(0,255))
my_pen.up()
my_pen.setpos(-599,341)
my_pen.down()
my_pen.speed(10)
my_pen.width(4)

def form_sq(side):
      for i in range(4):
        my_pen.fd(side)
        my_pen.right(90)
        side -= 5

side = 200
 
for i in range(10):
    form_sq(side)
    side-= 20

This is how it looks like currently I have assigned random colors to display but I do not know how to specify the colors and I have not added the list of values into this code yet.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Zacker Yeo
  • 11
  • 2
  • What colors are you expecting to show on the image? The right side is all one color, or colors alternate, or each box is a different color, or ...? – ggorlen Aug 08 '22 at 22:28
  • I am have not assigned my range of values to any colors yet, this is just me trying to play around with the color drawing. I am expecting the draw function to draw the lines based on the values from my list. Example would be if the value is between 10-50 the line color will be blue if it is 100-200 the value will be red. However, i am unsure how to assign colors to a range of values then drawing them out in turtle. – Zacker Yeo Aug 09 '22 at 09:11
  • Further more I have to read the values from a matrix which I have converted to a list of values. – Zacker Yeo Aug 09 '22 at 09:12
  • I don't really follow the "range of values" scheme exactly. What about 0-10, 50-100 or >200? Are the color ranges mapped to some standard color like HSL or RGB? Are all of the values between 10-50 the same shade of blue or some gradation? Might be an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/233676#233676) that'd be easier to answer if you gave the desired outcome and fundamental problem X rather than strange problem Y. – ggorlen Aug 09 '22 at 14:27
  • hmm, yeah something like RGB it would be good if there are some gradation for example 10 is dark blue and 1 is light blue something along that shade. The desired outcome would be able to read the list of number such as [133,10,23,4,5,6,6,7,7,8] for example and display them through colored lines based on the assigned color the range provides such as 0-10 are shades of blue and 50-100 are shades of red. – Zacker Yeo Aug 09 '22 at 14:37
  • HSL offers an easy way to map 0-360 to colors, although not necessarily 0-10 = blue and 50-100 = red. You could rotate and scale your scheme, but I'm still not quite following why it's so important that 10-50 is blue and 50-100 is red rather than an easier-to-implement/more standard scheme that'll look the same for the user and be more natural to represent in code. If you have to use specific numbers rather than adapting to HSL, a precise definition for the scheme (which colors map to which ranges, what are the bounds) as well as motivation for why you have to do it that way would be helpful. – ggorlen Aug 09 '22 at 14:57
  • And once again, it's not really clear what the result drawing should look like based on these numbers. – ggorlen Aug 09 '22 at 15:00
  • It is roughly the same as this discussion. here is the link https://stackoverflow.com/questions/497635/mapping-a-list-of-numeric-values-to-colors – Zacker Yeo Aug 09 '22 at 15:21
  • Same problem as that question: there's no complete specification really, just "I have a bunch of numbers and I need to represent them as colors" more or less. What's the range and what colors correspond to what parts of the range? Why do you need to do this exact approach, and other ways of achieving some user-visible result are unnacceptable? Is the data coming from some third-party API or something that forces your hand? Without that info, I can only say "use HSL". Do the answers in that thread solve the problem for you? If so, I can vote to close as a dupe. – ggorlen Aug 09 '22 at 15:43
  • Hi @ggorlen, I am working on a hollowness detection project, hence I need to be able to display how hollow it is dependent on the numerical value I have received from my code which is in an array which I have changed to a list. I am currently using the turtle module to draw out as you can see from above but I am having trouble with reading the numbers in the list into RGB values. Unless there is a way for me to read the numbers one by one in the list then input them into one of the rgb colors then I read them into here my_pen.color(r:= 255, g:= (value in list) , b:=0) – Zacker Yeo Aug 10 '22 at 03:12

2 Answers2

0

You should be able to move your my_pen.color(R, G, B) call inside the form_sq function. The R, G, and B values can be dependent on the value of side.

JJ Brosnan
  • 11
  • 1
  • Hi, thank you for your suggestion but do you have an example you could show me as I am not too efficient in this language. Thanks – Zacker Yeo Aug 09 '22 at 09:13
0

Is there a way to assign a color to a range of numbers in a list? ... For example, from value 0-10 I want it to be blue, and from 11-20 I want it to be red.

There are multiple ways to approach this problem, here's one:

COLORS = [
    (lambda x: 1 <= x <= 10, 'red'),
    (lambda x: 11 <= x <= 20, 'blue'),
    (lambda x: 21 <= x <= 30, 'yellow')
]

def get_color(x):
    for predicate, color in COLORS:
        if predicate(x):
            return color

    return 'black'

for number in (29, 13, 7, 31):
    print(number, '->', get_color(number))

Here's another:

COLORS = [
    (range(1, 11), 'red'),
    (range(11, 21), 'blue'),
    (range(21, 31), 'yellow')
]

def get_color(x):
    for sequence, color in COLORS:
        if x in sequence:
            return color

    return 'black'
cdlane
  • 40,441
  • 5
  • 32
  • 81