Search code examples
pythonturtle-graphicsskulpt

What's the effect of passing a scalar in turtle.color() in Skulpt?


When I try running my python code below, how does turtle.color interpret the input as a number?

import turtle

for i in ['red', 'blue', 'green', 'pink', 8, 10]:
       turtle.color(i)
       turtle.forward(100)
       turtle.right(90)

Actual:

Output I am getting is a square with sides in the given order. When it reaches turtle.color(8), one of the sides is overwritten with black, followed by next side (turtle.color(10)).

Expected:

The code should error out as turtle.color(8), doesn't make sense!!

I am actually using an online turtle compiler to test my code (repl.it/languages/python_turtle).


Solution

  • From their blog, repl.it mentions they use skulpt as their webIDE.

    Skulpt's Github page shows the following function which suggests that 'Black' is the default. This explains the odd behavior you are seeing vs others while debugging.

    function createColor(color, g, b, a) {
        var i;
    
        if (g !== undefined) {
            color = [color, g, b, a];
        }
    
        if (color.constructor === Array && color.length) {
            for(i = 0; i < 3; i++) {
                color[i] = (typeof color[i] === "number") ?
                    Math.max(0, Math.min(255, parseInt(color[i]))) :
                    0;
            }
            if (typeof color[i] === "number") {
                color[3] = Math.max(0, Math.min(1, color[i]));
                color = "rgba(" + color.join(",") + ")";
            }
            else {
                color = "rgb(" + color.slice(0,3).join(",") + ")";
            }
        }
        else if (typeof color === "string" && !color.match(/\s*url\s*\(/i)) {
            color = color.replace(/\s+/g, "");
        }
        else {
            return "black";
        }
    
        return color;
    }