Is there a way to programmatically generate a list of all named CSS colors supported by the browser via JavaScript? (.e.g. Red, Green, AliceBlue etc.)
Note: I'm not asking for a pre-compiled list, I'm looking for something more akin to document.body.style, which returns an object with all css properties supported by the browser.
I guess the closed you can get is starting from a pre-compiled color name list and check if the browser supports the color. You can assign the color with
div.stlye.backgroundColor = '';
div.style.backgroundColor = potentialColor;
and retrieve the actual color information with
var actualColorString = window.getComputedStyle(div).background; 
If the assigned color is not valid (or black), the color string starts with
rgba(0, 0, 0, 0)
Otherwise it is a known css color name.
Here is some jsfiddle to demonstrate the color check:
https://jsfiddle.net/tc8f5pgy/4/
I used this method to create a Color enum for my project:
https://github.com/stefaneidelloth/treezjs/blob/master/src/components/color/color.js
And here is part of the code as a backup for the 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);
  }
  
  
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With