Perl/Fun with Named Colors

The Wikitable at Named Colors was generated by a simple Perl program we can call colors.pl. If you have Perl installed, you should be able to run this basic script while learning a bit of Perl.

The list edit

First we want to populate the list of – Named Colors;

@Colors = ("AliceBlue".."YellowGreen");

We begin with a plain text file that lists all 139 Named Colors, each on its own line with no spaces:

AliceBlue	 
AntiqueWhite 
Aqua 
Aquamarine 
Azure 
Beige 
Bisque 
Black 
BlanchedAlmond 
Blue 
BlueViolet 
Brown 
BurlyWood 
CadetBlue 
Chartreuse 
Chocolate 
Coral 
CornflowerBlue 
Cornsilk 
Crimson 
Cyan 
DarkBlue 
DarkCyan 
DarkGoldenrod 
DarkGray 
DarkGreen 
DarkKhaki 
DarkMagenta 
DarkOliveGreen 
DarkOrange 
DarkOrchid 
DarkRed 
DarkSalmon 
DarkSeaGreen 
DarkSlateBlue 
DarkSlateGray 
DarkTurquoise 
DarkViolet 
DeepPink 
DeepSkyBlue 
DimGray	 
DodgerBlue 
FireBrick 

Gray 
Green 
GreenYellow 
Honeydew 
HotPink 
IndianRed 
Indigo 
Ivory 
Khaki 
Lavender 
LavenderBlush 
LawnGreen 
LemonChiffon 
LightBlue 
LightCoral 
LightCyan 
LightGoldenrodYellow 
LightGreen 
LightGrey 
LightPink 
LightSalmon 
LightSeaGreen 
LightSkyBlue 
LightSlateGray 
LightSteelBlue 
LightYellow 
Lime 
LimeGreen 
Linen 
Magenta	 
Maroon 
MediumAquamarine 
MediumBlue 
MediumOrchid 
MediumPurple 
MediumSeaGreen 
MediumSlateBlue 
MediumSpringGreen 
MediumTurquoise 
MediumVioletRed        
MidnightBlue  
MistyRose 
Moccasin 
NavajoWhite 
Navy 
OldLace 
Olive 
OliveDrab 
Orange 
OrangeRed 
Orchid 
PaleGoldenrod 
PaleGreen 
PaleTurquoise 
PaleVioletRed 
PapayaWhip 
PeachPuff 
Peru 
Pink 
Plum 
PowderBlue 
Purple 
Red 
RosyBrown 
RoyalBlue 
SaddleBrown 
Salmon 
SandyBrown 
SeaGreen 
Seashell 
Sienna 
Silver 
SkyBlue 
SlateBlue 
SlateGray 
Snow 
SpringGreen 
SteelBlue 
Tan 
Teal 
Thistle 
Tomato 
Turquoise 
Violet 
Wheat 
White 
WhiteSmoke 
Yellow 
YellowGreen

If you want to play with these colors, copy the list and save it in your working directory as NamedColors.txt

The script edit

Now, in that same directory, make a new file called colors.pl or whatever you like. Start with the usual opening line:

#!/usr/bin/perl -w

The -w switch tells your compiler to warn you of errors in your syntax.

# colors.pl - a simple script for handling named colors.

It's nice to add the script's filename and a brief description of what it does.

Slurping in the list edit

Many perl coders refer to populating an array (@ArrayName) from an external file as the process of "slurping". Perl has built in functions, open and close that give your scripts access to files on your local machine.

open (COLORS, "NamedColors.txt") or die;
@Colors=<COLORS>;
close COLORS;

We open (read-only) the bare list of NamedColors and slurp it into an array, @Colors. We do this via a filehandle called <COLORS>. Then we close the unaltered file.

Iterating the list edit

Now we use a foreach loop statement to break each $color out of @Colors and wrap it in wiki syntax for tables, stuffing each color into a new array, @NamedColors:

foreach $color (@Colors) {
	chomp$color;
	@NamedColors=("@NamedColors",
	"\n|- style=\"background:$color\"",
	"\n|style=\"color:black\"|$color",
	"\n|style=\"color:white\"|$color")
	}

Notice that the "s (double quotes) for the style attributes in the wiki syntax must be escaped with \s (backslashes).

The chomp function removes the newline characters from each item in the list, which were included in NamedColors.txt to make it more readable.

The colors.pl script produces wiki table syntax foreach color appending @NamedColors with:

|- style="background:AliceBlue" 
|style="color:black"|AliceBlue	 
|style="color:white"|AliceBlue

Printing the table edit

When the loop has iterated the whole list, we now use a new file handle COLORTABLE to hold and print the newly composed table rows and cells:

open (COLORTABLE, ">ColorTable.txt") or die;
print {COLORTABLE} "@NamedColors";
close COLORTABLE;

The open statement this time is write-enabled (>). The colors.pl script has now created a new file in your working directory – ColorTable.txt

You can add a little confirmation message at the end:

print "\n A file: ColorTable.txt
has been written to your working directory\n";

Running the script edit

To run the script make sure you have NamedColors.txt in your current working directory - the same directory from which you run colors.pl. In a terminal, you type: perl colors.pl

Now you can open ColorTable.txt and enclose it with {| and |}. Once you get the hang of it, you can make all sorts of nifty tools for manipulating colors with wiki syntax and perl scripts.

More information edit

This lesson assumes that you have Perl up and running on your local machine and that you know something about working from a command line or terminal and editing with a text editor. The above script is very basic. If you need more background on Perl, feel free to ask questions at Topic talk:Perl.

For ideas on how to alter the script to do other things with Named Colors, you may find or post some on Talk:Perl/Fun with Named Colors.

See also edit