Question about Color.fromString

4 views
Skip to first unread message

Zak

unread,
Apr 19, 2009, 10:04:05 AM4/19/09
to MochiKit
I can't get a null result from the fromString method. No matter what
I put in, it's never null. Generally, what I'm expecting it to do is
if I pass in a value that's not hex, not hsl, not rgb, that it should
drop through to the namedColor check and when it doesn't find the
named color, it should return null. But this just never seems to
happen.

Also, it looks like there's not much validation once it's determined
that a color starts with #, rgb or hsl, not too worried about this as
I'll be checking my inputs before creating the color object, but is
this by design?

Here's a short example:

createLoggingPane(true);

var myColor = new Color.fromString("xyz");
log(myColor == null);

^^^

This always returns false for me, no matter what I put in the
constructor. Perhaps there's something wrong with my code? Here's
what I'm actually doing with it so far:

function findColorsClick(text) {
createLoggingPane(true);

var myColor = new Color.fromString("123lksdjf098234lkj");

if (myColor){
var colorNameHash = Color.namedColors();
var myColorName = "[No color name found]";
for (var i in colorNameHash)
{
if (myColor.toHexString() == colorNameHash[i]){
myColorName = i;
break;
}
}
alert("Color Name: " + myColorName + "\nRGB: " + myColor.toRGBString
() + "\nHSL: " + myColor.toHSLString() + "\nHEX: " +
myColor.toHexString() + "\nIs light: " + myColor.isLight() + "\nIs
dark: " + myColor.isDark());
}

Bob Ippolito

unread,
Apr 19, 2009, 12:30:56 PM4/19/09
to Zak, MochiKit
Don't use "new".

Zak

unread,
Apr 19, 2009, 2:28:50 PM4/19/09
to MochiKit
Thanks Bob. I feel like a noob, but that fixed it!

Zak

On Apr 19, 11:30 am, Bob Ippolito <b...@redivi.com> wrote:
> Don't use "new".
>
Reply all
Reply to author
Forward
0 new messages