Not that's built-in, as far as I know. It would be kind of a hack, but I think you could create an invisible div, set its CSS background property to the named color, then use JS to get the background color of the div, then delete the div.
Here's an all purpose solution that does exactly what you want in every browser that is 100% programmatic, goodbye giant static table of names and hex values!:
// Here is the solution tied together as a native String native extension using
// MooTools, though you can implement the same thing in whatever framework you prefer:
(function(String){
var valueMatch = {
'rgb(0,0,0)': { 'black': ' ', 'rgb(0,0,0)': ' ' },
'rgba(0,0,0,0)': { 'transparent': ' ', 'rgba(0,0,0,0)': ' ' },
'#ffffff': { 'transparent': ' ' },
'transparent': { 'transparent': ' ' }
},
colorFromProbe = function(color){
color = color.toString();
if(!color.match(/^#.+$|^[^0-9]+$/)) return color;
var probe = ($('moo_color_probe') || new Element('textarea', {
'id': 'moo_color_probe',
'styles': {
'display': 'none',
'color': 'transparent'
}
}).inject(document.body, 'after'));
try{ probe.setStyle('color', color) } catch(e){ return color } //IE throws an error instead of defaulting the style to some color or transparent when the value is unrecognized
var computed = (Browser.ie) ? ieColorToHex(probe) : (Browser.opera) ? probe.style.color : probe.getComputedStyle('color'),
match = valueMatch[computed.replace(/ /g, '')];
probe.setStyle('color', 'transparent');
if((!Browser.ie || Browser.ie9) && color == 'transparent' && (match && match['transparent'])) return 'rgba(0, 0, 0, 0)';
return (computed == 'transparent' || match && !match[color.replace(/ /g, '')]) ? color : computed;
},
ieColorToHex = function(probe){ // Special IE mojo, thanks to Dean Edwards for the inspiration, his code used a pop-up window to test the color, I found you can simply use a textarea instead ;)
var value = probe.set('value', '').createTextRange().queryCommandValue("ForeColor");
value = (((value & 0x0000ff) << 16) | (value & 0x00ff00) | ((value & 0xff0000) >>> 16)).toString(16);
return "#000000".slice(0, 7 - value.length) + value;
};
String.implement({
colorToRgb: function(){
var color = colorFromProbe(this);
return (color.charAt(0) == '#') ? color.hexToRgb() : color;
},
colorToHex: function(){
var color = colorFromProbe(this);
return (color.test('rgb')) ? color.rgbToHex() : color;
}
});
})(String);
function convertToHexColor(englishColor) {
// create a temporary div.
var div = $('<div></div>').appendTo("body").css('background-color', englishColor);
var computedStyle = window.getComputedStyle(div[0]);
// get computed color.
var computedColor = computedStyle.backgroundColor;
// cleanup temporary div.
div.remove();
// done.
return computedColor;
// The above returns "rgb(R, G, B)" on IE9/Chrome20/Firefox13.
};
And to convert "rgb(R, G, B)" to #RRGGBB you can use:
function convertRGBDecimalToHex(rgb)
{
var regex = /rgb *\( *([0-9]{1,3}) *, *([0-9]{1,3}) *, *([0-9]{1,3}) *\)/;
var values = regex.exec(rgb);
if(values.length != 4)
{
return rgb; // fall back to what was given.
}
var r = Math.round(parseFloat(values[1]));
var g = Math.round(parseFloat(values[2]));
var b = Math.round(parseFloat(values[3]));
return "#"
+ (r + 0x10000).toString(16).substring(3).toUpperCase()
+ (g + 0x10000).toString(16).substring(3).toUpperCase()
+ (b + 0x10000).toString(16).substring(3).toUpperCase();
}
Edit: I've cleaned this up a bit and made a gist and demo of it. The basic approach remains the same.
The "add an element to the DOM and check its ComputedStyle" approach seems a little complex to me — you need to add it and check it and remember to remove it and you're changing the DOM just to compute a color and does it cause reflow? So here's a solution based on a temporary (and never rendered) <canvas>:
function colorToRGBA(color) {
// Returns the color as an array of [r, g, b, a] -- all range from 0 - 255
// color must be a valid canvas fillStyle. This will cover most anything
// you'd want to use.
// Examples:
// colorToRGBA('red') # [255, 0, 0, 255]
// colorToRGBA('#f00') # [255, 0, 0, 255]
var cvs, ctx;
cvs = document.createElement('canvas');
cvs.height = 1;
cvs.width = 1;
ctx = cvs.getContext('2d');
ctx.fillStyle = color;
ctx.fillRect(0, 0, 1, 1);
return ctx.getImageData(0, 0, 1, 1).data;
}
function byteToHex(num) {
// Turns a number (0-255) into a 2-character hex number (00-ff)
return ('0'+num.toString(16)).slice(-2);
}
function colorToHex(color) {
// Convert any CSS color to a hex representation
// Examples:
// colorToHex('red') # '#ff0000'
// colorToHex('rgb(255, 0, 0)') # '#ff0000'
var rgba, hex;
rgba = colorToRGBA(color);
hex = [0,1,2].map(
function(idx) { return byteToHex(rgba[idx]); }
).join('');
return "#"+hex;
}
Note that this lets you use anything that's a valid canvas fillStyle, so if you want to gin up a 1 pixel pattern from an image, it'll tell you the color of that as well.
I've tested this in reasonably modern versions of IE, Chrome, Safari, and Firefox.
The color named "Red" has a code of: <span id="myspan1">(requires js)</span>.<br>
...and color "PeachPuff"'s code is: <span id="myspan2">(requires js)</span>.<br>
...and "PaleGoldenRod" is: <span id="myspan3">(requires js)</span>.<br><br>
It works with "RGB(123,234,142)" too: <span id="myspan4">(needs js)</span>
and therefore with: "HSL(284,6%,49%)", which is: <span id="myspan5">(requires js)
</span>. Handy!
This function request a named color and send return a hexadecimal color value.
If the named color not supported (example "whyte") send back black color by default but on consol you can find a warning:
Not supported named color: ${namedColor}
Run the snippet to see how easily "any" valid CSS color (HSL, names, hex, system colors*, etc.) can be converted to R/G/B values using any element you happen to have laying around.
*Enter a value ofbackgroundand you'll get your Windows Desktop background color (in Firefox, anyways).