简体   繁体   English

在 Skulpt 中的turtle.color() 中传递标量有什么效果?

[英]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?当我尝试运行下面的 python 代码时, turtle.color如何将输入解释为数字?

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) ).当它到达turtle.color(8) ,其中一侧被黑色覆盖,然后是下一侧( turtle.color(10) )。

Expected:预期的:

The code should error out as turtle.color(8) , doesn't make sense!!代码应该出错为turtle.color(8) ,没有意义!!

I am actually using an online turtle compiler to test my code ( repl.it/languages/python_turtle ).我实际上正在使用在线海龟编译器来测试我的代码( repl.it/languages/python_turtle )。

From their blog , repl.it mentions they use skulpt as their webIDE.从他们的博客中,repl.it 提到他们使用skulpt作为他们的 webIDE。

Skulpt's Github page shows the following function which suggests that 'Black' is the default. Skulpt 的Github页面显示了以下函数,表明“黑色”是默认值。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM