【问题标题】:Get all named CSS colors via JavaScript [duplicate]通过 JavaScript 获取所有命名的 CSS 颜色 [重复]
【发布时间】:2018-10-05 18:26:07
【问题描述】:

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

注意:我不是在要求pre-compiled 列表,我在寻找更类似于document.body.style 的东西,它返回一个具有浏览器支持的所有css 属性的对象。

【问题讨论】:

  • 您可以使用 rgb 生成它们。 0 到 255. RGB(0,1,3)
  • 没有列表。
  • @rmn OP 想要一个从 JavaScript 代码生成的列表,而不是第 3 方列表。 OP 可以获取一个预编译的列表并运行它并查看颜色名称是否与十六进制代码匹配,但仍然不想要他们想要的,但可能是 OP 可以获得的最好的东西。
  • 识别的颜色名称列表必须在浏览器代码中的某个位置,但是 AFAIK 没有任何东西可以让 JS 使用它。
  • 这是来自铬回购github.com/chromium/chromium/blob/…的线索

标签: javascript css colors


【解决方案1】:

我猜你可以得到的关闭是从一个预编译的颜色名称列表开始,并检查浏览器是否支持该颜色。您可以分配颜色与

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

并使用

检索实际颜色信息
var actualColorString = window.getComputedStyle(div).background; 

如果分配的颜色无效(或黑色),颜色字符串以

开头
rgba(0, 0, 0, 0)

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

这里有一些 jsfiddle 来演示颜色检查:

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

我使用这种方法为我的项目创建了一个 Color 枚举:

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

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

<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);
  }
  
  
}

【讨论】:

    【解决方案2】:

    我想出了可以用于 TEXT 或 Backgrounds 的颜色列表

    只需按名称称呼他们,即:&lt;div class="black_bg silver_text"&gt;&lt;/div&gt;

    .black_text{color:#000000;}.black_bg{background-color:#000000;} 
    .silver_text{color:#c0c0c0;}.silver_bg{background-color:#c0c0c0;}   
    .gray_text{color:#808080;}.gray_bg{background-color:#808080;}   
    .white_text{color:#ffffff;}.white_bg{background-color:#ffffff;} 
    .maroon_text{color:#800000;}.maroon_bg{background-color:#800000;}   
    .red_text{color:#ff0000;}.red_bg{background-color:#ff0000;} 
    .purple_text{color:#800080;}.purple_bg{background-color:#800080;}   
    .fuchsia_text{color:#ff00ff;}.fuchsia_bg{background-color:#ff00ff;} 
    .green_text{color:#008000;}.green_bg{background-color:#008000;} 
    .lime_text{color:#00ff00;}.lime_bg{background-color:#00ff00;}   
    .olive_text{color:#808000;}.olive_bg{background-color:#808000;} 
    .yellow_text{color:#ffff00;}.yellow_bg{background-color:#ffff00;}   
    .navy_text{color:#000080;}.navy_bg{background-color:#000080;}   
    .blue_text{color:#0000ff;}.blue_bg{background-color:#0000ff;}   
    .teal_text{color:#008080;}.teal_bg{background-color:#008080;}   
    .aqua_text{color:#00ffff;}.aqua_bg{background-color:#00ffff;}   
    .orange_text{color:#ffa500;}.orange_bg{background-color:#ffa500;}
    .aliceblue_text{color:#f0f8ff;}.aliceblue_bg{background-color:#f0f8ff;} 
    .antiquewhite_text{color:#faebd7;}.antiquewhite_bg{background-color:#faebd7;}   
    .aquamarine_text{color:#7fffd4;}.aquamarine_bg{background-color:#7fffd4;}   
    .azure_text{color:#f0ffff;}.azure_bg{background-color:#f0ffff;} 
    .beige_text{color:#f5f5dc;}.beige_bg{background-color:#f5f5dc;} 
    .bisque_text{color:#ffe4c4;}.bisque_bg{background-color:#ffe4c4;}   
    .blanchedalmond_text{color:#ffebcd;}.blanchedalmond_bg{background-color:#ffebcd;}   
    .blueviolet_text{color:#8a2be2;}.blueviolet_bg{background-color:#8a2be2;}   
    .brown_text{color:#a52a2a;}.brown_bg{background-color:#a52a2a;} 
    .burlywood_text{color:#deb887;}.burlywood_bg{background-color:#deb887;} 
    .cadetblue_text{color:#5f9ea0;}.cadetblue_bg{background-color:#5f9ea0;} 
    .chartreuse_text{color:#7fff00;}.chartreuse_bg{background-color:#7fff00;}   
    .chocolate_text{color:#d2691e;}.chocolate_bg{background-color:#d2691e;} 
    .coral_text{color:#ff7f50;}.coral_bg{background-color:#ff7f50;} 
    .cornflowerblue_text{color:#6495ed;}.cornflowerblue_bg{background-color:#6495ed;}   
    .cornsilk_text{color:#fff8dc;}.cornsilk_bg{background-color:#fff8dc;}   
    .crimson_text{color:#dc143c;}.crimson_bg{background-color:#dc143c;} 
    .darkblue_text{color:#00008b;}.darkblue_bg{background-color:#00008b;}   
    .darkcyan_text{color:#008b8b;}.darkcyan_bg{background-color:#008b8b;}   
    .darkgoldenrod_text{color:#b8860b;}.darkgoldenrod_bg{background-color:#b8860b;} 
    .darkgray_text{color:#a9a9a9;}.darkgray_bg{background-color:#a9a9a9;}   
    .darkgreen_text{color:#006400;}.darkgreen_bg{background-color:#006400;} 
    .darkgrey_text{color:#a9a9a9;}.darkgrey_bg{background-color:#a9a9a9;}   
    .darkkhaki_text{color:#bdb76b;}.darkkhaki_bg{background-color:#bdb76b;} 
    .darkmagenta_text{color:#8b008b;}.darkmagenta_bg{background-color:#8b008b;} 
    .darkolivegreen_text{color:#556b2f;}.darkolivegreen_bg{background-color:#556b2f;}   
    .darkorange_text{color:#ff8c00;}.darkorange_bg{background-color:#ff8c00;}   
    .darkorchid_text{color:#9932cc;}.darkorchid_bg{background-color:#9932cc;}   
    .darkred_text{color:#8b0000;}.darkred_bg{background-color:#8b0000;} 
    .darksalmon_text{color:#e9967a;}.darksalmon_bg{background-color:#e9967a;}   
    .darkseagreen_text{color:#8fbc8f;}.darkseagreen_bg{background-color:#8fbc8f;}   
    .darkslateblue_text{color:#483d8b;}.darkslateblue_bg{background-color:#483d8b;} 
    .darkslategray_text{color:#2f4f4f;}.darkslategray_bg{background-color:#2f4f4f;} 
    .darkslategrey_text{color:#2f4f4f;}.darkslategrey_bg{background-color:#2f4f4f;} 
    .darkturquoise_text{color:#00ced1;}.darkturquoise_bg{background-color:#00ced1;} 
    .darkviolet_text{color:#9400d3;}.darkviolet_bg{background-color:#9400d3;}   
    .deeppink_text{color:#ff1493;}.deeppink_bg{background-color:#ff1493;}   
    .deepskyblue_text{color:#00bfff;}.deepskyblue_bg{background-color:#00bfff;} 
    .dimgray_text{color:#696969;}.dimgray_bg{background-color:#696969;} 
    .dimgrey_text{color:#696969;}.dimgrey_bg{background-color:#696969;} 
    .dodgerblue_text{color:#1e90ff;}.dodgerblue_bg{background-color:#1e90ff;}   
    .firebrick_text{color:#b22222;}.firebrick_bg{background-color:#b22222;} 
    .floralwhite_text{color:#fffaf0;}.floralwhite_bg{background-color:#fffaf0;} 
    .forestgreen_text{color:#228b22;}.forestgreen_bg{background-color:#228b22;} 
    .gainsboro_text{color:#dcdcdc;}.gainsboro_bg{background-color:#dcdcdc;} 
    .ghostwhite_text{color:#f8f8ff;}.ghostwhite_bg{background-color:#f8f8ff;}   
    .gold_text{color:#ffd700;}.gold_bg{background-color:#ffd700;}   
    .goldenrod_text{color:#daa520;}.goldenrod_bg{background-color:#daa520;} 
    .greenyellow_text{color:#adff2f;}.greenyellow_bg{background-color:#adff2f;} 
    .grey_text{color:#808080;}.grey_bg{background-color:#808080;}   
    .honeydew_text{color:#f0fff0;}.honeydew_bg{background-color:#f0fff0;}   
    .hotpink_text{color:#ff69b4;}.hotpink_bg{background-color:#ff69b4;} 
    .indianred_text{color:#cd5c5c;}.indianred_bg{background-color:#cd5c5c;} 
    .indigo_text{color:#4b0082;}.indigo_bg{background-color:#4b0082;}   
    .ivory_text{color:#fffff0;}.ivory_bg{background-color:#fffff0;} 
    .khaki_text{color:#f0e68c;}.khaki_bg{background-color:#f0e68c;} 
    .lavender_text{color:#e6e6fa;}.lavender_bg{background-color:#e6e6fa;}   
    .lavenderblush_text{color:#fff0f5;}.lavenderblush_bg{background-color:#fff0f5;} 
    .lawngreen_text{color:#7cfc00;}.lawngreen_bg{background-color:#7cfc00;} 
    .lemonchiffon_text{color:#fffacd;}.lemonchiffon_bg{background-color:#fffacd;}   
    .lightblue_text{color:#add8e6;}.lightblue_bg{background-color:#add8e6;} 
    .lightcoral_text{color:#f08080;}.lightcoral_bg{background-color:#f08080;}   
    .lightcyan_text{color:#e0ffff;}.lightcyan_bg{background-color:#e0ffff;}
    

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 1970-01-01
      • 2019-01-29
      • 2019-03-02
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2011-02-02
      相关资源
      最近更新 更多