代码之家  ›  专栏  ›  技术社区  ›  Minifyre Nicolas NZ

通过JavaScript获取所有命名的CSS颜色

  •  0
  • Minifyre Nicolas NZ  · 技术社区  · 8 年前

    有没有办法通过JavaScript以编程方式生成浏览器支持的所有命名CSS颜色的列表?)例如。 Red Green , AliceBlue )

    注: pre-compiled 列表,我正在寻找更类似于 document.body.style ,它返回一个包含浏览器支持的所有css属性的对象。

    0 回复  |  直到 8 年前
        1
  •  1
  •   Stefan    5 年前

    我猜您可以从预编译的颜色名称列表开始,并检查浏览器是否支持该颜色。可以使用指定颜色

    div.stlye.backgroundColor = '';
    div.style.backgroundColor = potentialColor;
    

    并使用

    var actualColorString = window.getComputedStyle(div).background; 
    

    如果指定的颜色无效(或为黑色),则颜色字符串以开头

    rgba(0, 0, 0, 0)
    

    否则它是一个已知的css颜色名称。

    下面是一些JSFIDLE来演示颜色检查:

    https://jsfiddle.net/tc8f5pgy/4/

    我使用此方法为我的项目创建颜色枚举:

    https://github.com/stefaneidelloth/treezjs/blob/master/src/components/color/color.js

    下面是作为JSFIDLE备份的部分代码:

    <div id='color-element'></div>
    

    //source of color names: https://simple.wikipedia.org/wiki/List_of_colors
    
    const colorNames = [
        'Amaranth',
        //...
    ];
    
    //another source of color names: https://gist.github.com/bobspace/2712980
    const cssColorNames = [ 
        "AliceBlue", 
        "AntiqueWhite",
        //...
    ];
    
    //another source of color names: https://chir.ag/projects/ntc/ntc.js
    var extendedColors =  [
        ["000000", "Black"],
        ["000080", "Navy Blue"],
        //...
    ];
    
    function camelize(str) { //source: https://stackoverflow.com
    
    /questions/2970525/converting-any-string-into-camel-case
      var camelString= str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {
        return index === 0 ? word.toLowerCase() : word.toUpperCase();
      }).replace(/\s+/g, '');
      return camelString.replace(/\//g,'').replace(/-/g,'').replace(/'/g,'');
    }
    
    function rgba2hex(orig) { //source: https://stackoverflow.com/questions/49974145/how-to-convert-rgba-to-hex-color-code-using-javascript
      var a, isPercent,
        rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i),
        alpha = (rgb && rgb[4] || "").trim(),
        hex = rgb ?
        (rgb[1] | 1 << 8).toString(16).slice(1) +
        (rgb[2] | 1 << 8).toString(16).slice(1) +
        (rgb[3] | 1 << 8).toString(16).slice(1) : orig;
    
      if (alpha !== "") {
        a = alpha;
      } else {
        a = 01;
      }
      // multiply before convert to HEX
      a = ((a * 255) | 1 << 8).toString(16).slice(1)
      hex = hex + a;
    
      return hex;
    }
    
    function handleActualColor(name, rgbaColorString){
      var hexColorString = rgba2hex(rgbaColorString);
      var output = "Color." + name + " = new Color('"+ name +"','#"+ hexColorString + "');"
      console.log(output);
    }
    
    var potentialColorSet = new Set();
    for(var colorName of colorNames){
      potentialColorSet.add(camelize(colorName));
    }
    for(var colorName of cssColorNames){
      potentialColorSet.add(camelize(colorName));
    }
    for(var entry of extendedColors){
     var colorName = entry[1];
     potentialColorSet.add(camelize(colorName));
    }
    
    var potentialColors = Array.from(potentialColorSet).sort();
    
    var div = document.getElementById('color-element');
    
    
    
    for(var potentialColor of potentialColors){
      div.style.backgroundColor = '';
      div.style.backgroundColor = potentialColor;
      var actualColorString = window.getComputedStyle(div).background;  
      var endIndex = actualColorString.indexOf(')');
      var rgbaColorString = actualColorString.substring(0, endIndex+1);
      if(rgbaColorString !== 'rgba(0, 0, 0, 0)'){
        handleActualColor(potentialColor, rgbaColorString);
        if(potentialColor == 'screaminGreen'){
            throw new Error('foo');
        }
      }
      if(potentialColor.toLowerCase() === 'black'){
        handleActualColor(potentialColor, rgbaColorString);
      }
      
      
    }