gpt4 book ai didi

javascript - 如何判断 fillStyle 是否被分配了非法颜色?

转载 作者:太空宇宙 更新时间:2023-11-04 15:36:08 25 4
gpt4 key购买 nike

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


var c = document.getElementById("canvasID");
var g = c.getContext("2d");
g.fillStyle = "pukeYellow";//非法颜色

这能被检测到吗? g.fillStyle 会成为某个哨兵值吗?

假设您正在编写一个 Web 应用程序,要求用户提供命名颜色,然后显示该颜色。我们如何告诉用户他犯了一个错误?

最佳答案

根据the HTML Canvas 2D Context specification :

8 Fill and stroke styles

If the value is a string but cannot be parsed as a CSS value, or is neither a string, a CanvasGradient, nor a CanvasPattern, then it must be ignored, and the attribute must retain its previous value.

我假设您只对有效的 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 .

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

关于javascript - 如何判断 fillStyle 是否被分配了非法颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44337821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com