0

I have a string from a texarea, now I want to find out if the string is one of the HTML color names here). I know I could simply compare my string to every single one, but that uses up quite some space and isn't very good. Is there a function like maybe iscolorname(color)? Or won't I get around checking every single one?

MrCode
  • 63,975
  • 10
  • 90
  • 112
Wingblade
  • 9,585
  • 10
  • 35
  • 48
  • What framework our you using, depending on the framework this may or may not be possible – pat8719 Jun 07 '12 at 16:00
  • none to my knowledge, checking is easy, just a single line of `if ($htmlcolors[$htmlcolorname]) ...` with $htmlcolors array having color name as the key and color value as the value – LeleDumbo Jun 07 '12 at 16:00
  • I am coding in javascript. So I got all default javascript functions a browser has. – Wingblade Jun 07 '12 at 16:01

2 Answers2

1

This question offers some partial anwsers : Javascript function to convert color names to hex codes

A complete one : You could simply create an in memory canvas and do this :

if (suspectedColor.indexOf('(')>=0 || suspectedColor.indexOf('#')>=0) {
   // see http://www.w3.org/TR/2dcontext/#dom-context-2d-fillstyle
   isColorName = false;
} else {
    context.fillStyle = suspectedColor;
    isColorName = context.fillStyle!="#000000" ; // ok, I let as an exercice the check that colorName isn't black
}

It works because of this :

context . fillStyle [ = value ] Returns the current style used for filling shapes.

Can be set, to change the fill style.

The style can be either a string containing a CSS color, or a CanvasGradient or CanvasPattern object. Invalid values are ignored.

(from http://www.w3.org/TR/2dcontext/#dom-context-2d-fillstyle)

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • You seem to be aware that this question is a duplicate - why not answer the original instead? – finnw Jun 07 '12 at 16:31
  • Because in fact this isn't a duplicate (it took me a moment to see it). I linked the old question because it could help find some answers (not mine) but it doesn't directly answer. You have for example to check (hence the reference I give) that the string isn't simply "#aaa" or "rgba(1,1,1,1)". – Denys Séguret Jun 07 '12 at 16:32
  • Right now I am using the array provided on the linked post since it allows me to add custom color names when I modify it slightly. I might use the fillStyle thingy too if I ever want my user to be able to use rgb(0,0,0) or rgba. – Wingblade Jun 11 '12 at 07:41
0

You can try to set a color and read it back. Browsers besides IE ignore invalid colors, older IE's throw an error you can catch if you set an invalid property.

(If this is used often, use the object test instead.)

function validColor(cstring){
    var check= false, dummy= document.createElement('span');
    try{
        dummy.style.color= cstring;
        return !!dummy.style.color;
    }
    catch(er){
        return false;
    }
    finally{
        dummy= null;
    }
}
validColor('reddish')
kennebec
  • 102,654
  • 32
  • 106
  • 127