如何判断fillStyle是否被指定为非法颜色?

时间:2017-06-02 21:41:58

标签: javascript canvas colors

假设某人尝试按如下方式分配

var c = document.getElementById("canvasID"); var g = c.getContext("2d"); g.fillStyle = "pukeYellow"; //illegal color

可以检测到这个吗? g.fillStyle会成为一些哨兵价值吗?

想象一下,您正在编写一个Web应用程序,要求用户输入命名颜色,然后显示颜色。我们如何告诉用户他做了一个嘘声?

3 个答案:

答案 0 :(得分:1)

根据the HTML Canvas 2D Context specification

  

8填充和描边样式

     

如果值是字符串但无法解析为CSS值,或者既不是字符串,也不是CanvasGradient,也不是CanvasPattern,那么必须忽略,并且属性必须保留以前的值

我假设您只对有效的CSS颜色值as defined here感兴趣。您至少有三个选项来验证CSS颜色值:

  • 通过比较分配前后的context.fillStyle,如果两者相等,则用户提供相同或无效的颜色值
  • 通过手动验证:

    const colors = new Set(["aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkgrey", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkslategrey", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey", "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "green", "greenyellow", "grey", "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgray", "lightgreen", "lightgrey", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightslategrey", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "rebeccapurple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "slategrey", "snow", "springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen"]);
    colors.has(input.toLowerCase());
    
  • 通过setting and checking the style of a temporary HTMLElement

我推荐前两个解决方案中的一个。

答案 1 :(得分:0)

实验揭示了这一点。如果指定非法颜色,则分配失败。图形上下文的状态保持不变。 JavaScript,就像它的方式一样,只是忽略了你的错误和伪造。您也可以尝试this web app中的颜色。如果您在十六进制代码之前加上#。

,该应用程序还将显示与十六进制代码关联的颜色

答案 2 :(得分:0)

无效的颜色字符串将被解释为最后一个有效颜色(或#000000,黑色)。

大多数用例都应该使用这个snipet。



var canvas = document.createElement("canvas")
var context = canvas.getContext("2d")
context.fillStyle = "#ff0000"
console.log(testColor("yellow"))
console.log(testColor("pukeYellow"))
console.log(testColor("red"))
console.log(context.fillStyle)

function testColor(color){
    var tmp = context.fillStyle
    context.fillStyle = color
    var result = context.fillStyle == tmp
    if(result){
        var tmp2 = tmp == '#ffffff' ? '#000000' : '#ffffff'
        context.fillStyle = tmp2
        context.fillStyle = color
        result = (context.fillStyle+'') == (tmp2+'')
    }
    context.fillStyle = tmp
    return !result
}




警告:仅在Chrome上测试过!