Color Name Class (SVG 1.0 – X11 and VGA)

UPDATE (2009-05-17):
  1. Renamed class from Color to ColorName
  2. Changed package to yourpalmark
  3. hexValue property has been renamed to color
  4. Added a toString method to trace out the properties of the ColorName object
  5. Changed license from New BSD to MIT

One small thing I’ve always found a bit handy with CSS in XHTML is the ability to type familiar names for colors rather than the hex value.

Unfortunately, Flash’s StyleSheet does not support color names at all and Flex’s StyleManager only supports the 16 basic VGA color names.

I decided to write a class that adds static properties for all of the color names defined by SVG 1.0 (The colors in SVG 1.0 include the X11 colors with the addition of gray/grey variants. X11 includes the 16 basic VGA, or HTML4, colors as well). This class also adds two static convenience methods for returning a ColorName object by passing in the color name or hexadecimal color value.

While this class does not extend the CSS parsing capabilities of Flash, it could be used by an advanced StyleSheet class that overwrites the parseCSS method using this class as a lookup.
This class could also be extended to add (or overwrite) color values specific to a project you are working on. By setting up static properties for a project color set, the project team could be assured that the colors being used are consistent between team members.

Description:
The ColorName class defines the names and hexadecimal values of the colors defined in SVG 1.0.

Documentation:
ColorName Class Documentation

Download ColorName Class

This entry was posted in Flash. Bookmark the permalink. Trackbacks are closed, but you can post a comment.

10 Comments

  1. Alex Unicorn
    Posted June 20, 2008 at 11:17 am | Permalink

    Finally I will be able to simply write “majesticunicornwhite” and flash will know what I am talking about. Thank you Mark Walters. I owe you one million thanks. If only you had a link to unicornpaypal.com I could donate to your magnificence. Stay strong my flash brother.

  2. Posted June 20, 2008 at 11:26 am | Permalink

    Thanks pal!
    Sometimes you’ve just got to take action on these kinds of things.

  3. Posted April 5, 2009 at 2:45 am | Permalink

    Sweet. I knew someone must have taken the initiative. Thanks yous.

  4. Bug
    Posted July 27, 2009 at 7:29 am | Permalink

    Just what I needed, thanks.

  5. Posted December 21, 2009 at 11:02 am | Permalink

    Hi Mark,

    This looks pretty handy. Can you provide an example of how to use it?

    I’ve tried the following code but I get an error …

    package
    {
    import flash.display.MovieClip;
    import flash.display.Sprite;

    import com.yourpalmark.utils.ColorName;

    public class Main extends MovieClip
    {
    private var canvas:Sprite;

    public function Main():void
    {
    canvas = new Sprite();
    canvas.graphics.beginFill(BLUE, 1);
    canvas.graphics.drawRect(0, 0, 500, 250);
    canvas.graphics.endFill();
    addChild(canvas);
    }
    }
    }

  6. Posted December 22, 2009 at 7:12 am | Permalink

    Do I need to instantiate the class first?

  7. Posted January 6, 2010 at 10:18 am | Permalink

    Hi Mark,

    Are you there?

  8. Posted February 8, 2010 at 2:50 pm | Permalink

    Hi Mark,

    I think I have figured out how to use this class and have managed to get it to work with colours that use a name without underscores. Like this …

    import com.yourpalmark.utils.ColorName;
    var canvas:Sprite;
    canvas = new Sprite();
    canvas.graphics.beginFill(ColorName.getColorNameByName(“LAVENDER”).color, 1);
    canvas.graphics.drawRect(0, 0, 550, 400);
    canvas.graphics.endFill();
    addChild(canvas);

    But when I try to get a colour that has underscores in it’s name like this …

    import com.yourpalmark.utils.ColorName;
    var canvas:Sprite;
    canvas = new Sprite();
    canvas.graphics.beginFill(ColorName.getColorNameByName(“LAVENDER_BLUSH”).color, 1);
    canvas.graphics.drawRect(0, 0, 550, 400);
    canvas.graphics.endFill();
    addChild(canvas);

    I get the following error message …

    “TypeError: Error #1009: Cannot access a property or method of a null object reference. at colorNameDemo_fla::MainTimeline/frame1()”

    Any ideas why this is?

    Any help would be gratefully appreciated.

    Cheers,

    Adrian

  9. Posted February 28, 2010 at 8:42 pm | Permalink

    Hey Adrian,
    Sorry for the really late reply…
    You do not need to instantiate the class, all of the properties and methods are static.
    Each color in the class is an instance of the ColorName class. So, BLUE, for example, has a color and name property.
    To use a color, you would just need to say ColorName.BLUE.color.
    You don’t actually need to use the getColorNameByName() method. If you do use the method, there’s a reason that it doesn’t work for the colors with an underscore.
    The name of LAVENDER_BLUSH is actually “lavenderBlush”. You can find that out by tracing out ColorName.LAVENDER_BLUSH.name.
    But again, the easiest way to get the color would just be to say ColorName.LAVENDER_BLUSH.color.
    Hope that helps!!

  10. Posted August 31, 2010 at 8:17 am | Permalink

    Hi Mark,

    I just though I’d check back here and was happy to see your reply from February. Thought I’d let you know that thanks to your reply I got it to work. If anyone else is interested here is my demo code …

    package
    {
    import flash.display.Sprite;
    import com.yourpalmark.utils.ColorName;

    public class Main extends Sprite
    {
    private var canvas:Sprite;

    public function Main():void
    {
    canvas = new Sprite();
    canvas.graphics.beginFill(ColorName.LAVENDER_BLUSH.color, 1);
    canvas.graphics.drawRect(0, 0, 500, 250);
    canvas.graphics.endFill();
    addChild(canvas);
    }

    }

    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Pages

  • Categories

  • Archives