Question about Color.fromString

View: New views
3 Messages — Rating Filter:   Alert me  

Question about Color.fromString

by Zak-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to mochikit@...
To unsubscribe from this group, send email to mochikit+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Question about Color.fromString

by Bob Ippolito :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Don't use "new".

On Sun, Apr 19, 2009 at 7:04 AM, Zak <el.zakko@...> wrote:

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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to mochikit@...
To unsubscribe from this group, send email to mochikit+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Question about Color.fromString

by Zak-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks Bob.  I feel like a noob, but that fixed it!

Zak

On Apr 19, 11:30 am, Bob Ippolito <b...@...> wrote:

> Don't use "new".
>
> On Sun, Apr 19, 2009 at 7:04 AM, Zak <el.za...@...> wrote:
>
> > 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());
> > }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to mochikit@...
To unsubscribe from this group, send email to mochikit+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---