mardi 20 juillet 2021

How can I assign the pen color of a turtle using a web-safe color stored as a string in a list in a dictionary?

I'm using Python v3.9.5 to write a plotting program, something I've posted on before. I have most of it done, but I'm running into a lot of snags with color scheme selection, something I really want to add to the program since not all colors are good for plotting points, and I want my plotter to be as customizable as possible. Like I did in my previous question, I'll be demonstrating everything with a color scheme called 'Red'. To start off, the guys in the last question wanted me to define the color scheme 'Red' like this:

ColorSchemes = {}
ColorSchemes['Red'] = ["IndianRed", "LightCoral", "Crimson", "Red", "FireBrick"]

This is defined within a function that takes the parameter "scheme", which is defined in a global variable called "colorscheme".

colorscheme = input("What color scheme would you like to use? ")
#Later...
getColors(colorscheme)

Here is the getColors() function:

def getColors(scheme):
    ColorSchemes = {}
    ColorSchemes['Red'] = ["IndianRed", "LightCoral", "Crimson", "Red", "FireBrick"]
    random.shuffle(ColorSchemes[scheme])
    plotter.color(str(ColorSchemes[scheme(len([0]))]))

You can see that the place where I attempt to call the first element of the chosen color scheme is an absolute mess; the shell insists I can't call a string, even though the string should be a perfectly valid name of a web-safe color. It won't even allow me to use print() to check how the rest of the code is working. I'd hoped to get the colors to cycle through as well, i.e. going from Indian Red to Light Coral to Crimson in order, but I'd dropped that idea and tried to use random.shuffle() to at least make them random. I was originally going to pick a random color from the lists by putting a randint() where the index goes, except I couldn't do that with a range, because the largest color scheme list holds 50 colors and the program would pitch a fit if I chose a color scheme like 'Red' that only has five colors and then tried to find a color 17 that doesn't exist. Instead, I tried to shuffle the colors and pick the first color of the mixed-up list. This would run every time I plotted a point, in theory changing the point color each time.

Currently, the rest of my program works; before choosing the color, a function correctly draws a grid, and afterward, another function plots a point. Everything works smoothly, except for the fact that now my points stay black instead of being colored. I've already tried defining the color schemes elsewhere, integrating this function with the point plotting function (actually what I had before), and experimenting with other things like using randint(). If there's anything else I should try, another method to get randomized or cycling colors, or a better, different idea entirely, I'm all ears. Thanks in advance for your suggestions!




Aucun commentaire:

Enregistrer un commentaire