termlib.js home | multiple terminals | parser | faq | documentation | samples
Color Sample
 
> open terminal  
 
 
(c) mass:werk,
N. Landsteiner 2005-2007
http://www.masswerk.at

Color Sample Page
 

With version 1.2 termlib.js introduces support for colors.

Colors are controlled using the styles interface of the type() and write() methods.

Usage:

As any style settings colors are controlled by the %<style> markup of the write() method, where <style> starts with a "c" for "color".

"termlib.js" supports 3 different color systems:

1) The first approach mimics the ANSI color approach as known from most terminals:

There is a set of 16 colors (1 default color and 15 configurable colors):

color name  code  color string  synonyms
default  0  empty  clear, empty string
black  1  #000000   
red  2  #ff0000  red1
green  3  #00ff00  green1
yellow  4  #ffff00  yellow1
blue  5  #0066ff  blue1
magenta  6  #ff00ff  magenta1
cyan  7  #00ffff  cyan1
white  8  #ffffff   
grey  9  #808080  gray
darkred  A  #990000  red2
darkgreen  B  #009900  green2
darkyellow  C  #999900  yellow2
darkblue  D  #003399  blue2
darkmagenta  E  #990099  magenta2
darkcyan  F  #009999  cyan2
 

"default" or "clear" refers always to the configured default color.

Code values from A to F (may be lower case) indicate hex values from 10 to 15.

You may change the color string using the method TermGlobals.setColor( <label>, <colorstring> ), where <label> is the name or code of a color and <colorstring> a valid CSS color value.

Examples:

  // changing the color string used for "red"
  TermGlobals.setColor( "red", "#880000" );
  TermGlobals.setColor( "2", "#880000" );
  TermGlobals.setColor( 2, "#880000" );

  // changing darkred
  TermGlobals.setColor( "darkred", "#440000" );
  TermGlobals.setColor( "A", "#440000" );
  TermGlobals.setColor( 10, "#440000" );

You may also access the currently used color string using the method TermGlobals.getColorString( <label> ) or look up a color's code using TermGlobals.getColorCode( <label> ).

 

Writing with color:

You may switch the color using the markup %c(<color name>) and switch back to the default color using %c(default) or %c(clear) or just %c() or %c0.

Alternatively to color names you may use a one-digit hex code %+c<color code> where <color code> is in the range from 0 to F (case insensitive).

Examples:

  myTerm.write("Switching to %c(red)RED%c(default) and back again.");
  myTerm.write("Switching to %c2RED%c0 and back again.");
  
  myTerm.write("Switching to %c(darkred)DARKRED%c() and back again.");
  myTerm.write("Switching to %caDARKRED%c0 and back again.");

With version 1.4 or higher you may also use a color code (hex or decimal) inside brackets (resulting in a unified color markup syntax "%c(...)"):

  myTerm.write("Switching to %c(2)RED%c(0) and back again.");
  myTerm.write("Switching to %c(02)RED%c(0) and back again.");
  
  myTerm.write("Switching to %c(a)DARKRED%c() and back again.");
  myTerm.write("Switching to %c(10)DARKRED%c() and back again.");
 

Note that %c0, %c(0), %c(), %c(default), %c(clear) are just synonyms for the default color. For the following examples we'll use "%c()" for this.

All the color names and codes are case insensitive ("RED" == "red").
As with all styles, setting a color applies to the single write() only.

Using the type() method the color values correspond to the bits 8 to 11 (bit-mask xf00) of the style-vector. (This means you have to multiply the color code by 256 or 0xff.)

Examples:

  myTerm.type("This is green.", 3*256);
  myTerm.type("This is green and reverse.", 3*256 + 1);

 

2) Using named web colors (Netscape colors)

As a second approach you may use any of the 16 standard named colors or any of the 120 additional Netscape colors.

All you have to do to access this second color set is to prefix the color name with "@".

Examples:

  myterm.write("Switching to %c(@burlywood)burlywood%c() and back again.");
  myterm.write("Switching to %c(@orange)orange%c() and back again.");

There are no color codes associated with this. Internally the bits 8 to 15 of the style vector (bit-mask 0xff00) are used for these colors. (Values start with or 0x1000.)

The names of these 136 colors are:

aliceblue antiquewhite aqua aquamarine azure beige black blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson darkblue darkcyan darkgoldenrod darkgray darkgreen darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkturquoise darkviolet deeppink deepskyblue dimgray dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod 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 maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream 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

As above all color names are not case sensitive.

The values of this color set is fixed, you cannot change them using TermGlobals.setColor(). (The codes of this set are mapped internally to color strings. Changing these would be possible, but would not make much sense.)

 

3) Using the web color set

Finally you may use any of the 216 standard web colors.
You access these by a leading "# and the 6-hex-digit color code of the color.
(You may also use the 3-digit CSS format for these colors).

Examples:

  myterm.write("Switching to %c(#ff0000)red%c() and back again.");
  myterm.write("Switching to %c(#f00)red%c() and back again.");

Again there are no accessible color codes associated to these. Internally these colors are mapped to the bit-mask 0xff0000 of the style vector.

If the given color code is not part of the web color set, but represents a valid color in 6-digit format or in short 3-digit notation, the color will be matched to the nearest web color available (e.g. "#9a44ee" => "#9933ff").

The available color codes are (in long notation):

000000 000033 000066 000099 0000cc 0000ff 003300 003333 003366 003399 0033cc 0033ff 006600 006633 006666 006699 0066cc 0066ff 009900 009933 009966 009999 0099cc 0099ff 00cc00 00cc33 00cc66 00cc99 00cccc 00ccff 00ff00 00ff33 00ff66 00ff99 00ffcc 00ffff 330000 330033 330066 330099 3300cc 3300ff 333300 333333 333366 333399 3333cc 3333ff 336600 336633 336666 336699 3366cc 3366ff 339900 339933 339966 339999 3399cc 3399ff 33cc00 33cc33 33cc66 33cc99 33cccc 33ccff 33ff00 33ff33 33ff66 33ff99 33ffcc 33ffff 660000 660033 660066 660099 6600cc 6600ff 663300 663333 663366 663399 6633cc 6633ff 666600 666633 666666 666699 6666cc 6666ff 669900 669933 669966 669999 6699cc 6699ff 66cc00 66cc33 66cc66 66cc99 66cccc 66ccff 66ff00 66ff33 66ff66 66ff99 66ffcc 66ffff 990000 990033 990066 990099 9900cc 9900ff 993300 993333 993366 993399 9933cc 9933ff 996600 996633 996666 996699 9966cc 9966ff 999900 999933 999966 999999 9999cc 9999ff 99cc00 99cc33 99cc66 99cc99 99cccc 99ccff 99ff00 99ff33 99ff66 99ff99 99ffcc 99ffff cc0000 cc0033 cc0066 cc0099 cc00cc cc00ff cc3300 cc3333 cc3366 cc3399 cc33cc cc33ff cc6600 cc6633 cc6666 cc6699 cc66cc cc66ff cc9900 cc9933 cc9966 cc9999 cc99cc cc99ff cccc00 cccc33 cccc66 cccc99 cccccc ccccff ccff00 ccff33 ccff66 ccff99 ccffcc ccffff ff0000 ff0033 ff0066 ff0099 ff00cc ff00ff ff3300 ff3333 ff3366 ff3399 ff33cc ff33ff ff6600 ff6633 ff6666 ff6699 ff66cc ff66ff ff9900 ff9933 ff9966 ff9999 ff99cc ff99ff ffcc00 ffcc33 ffcc66 ffcc99 ffcccc ffccff ffff00 ffff33 ffff66 ffff99 ffffcc ffffff

As above all color codes are not case sensitive.

The values of this color set is fixed, you cannot change them using TermGlobals.setColor(). (The codes of this set are mapped internally to color strings. Changing these would be possible, but would not make much sense.)

 

All this adds up to the complete color-markup syntax:

  <color-markup>     ::= "%c"<color-expression>
  <color-expression> ::= <hex-digit> | <label-expression>
  <label-expression> ::= "("<internal-color>|"@"<netscape-color>"|#"<web-color>")"
  <hex-digit>        ::= "0" - "F"
  <internal-color>   ::= any of the names referring to the 16 internal colors
                         or any decimal or hex value 0 <= n < 15
  <netscape-color>   ::= any of the names of the 136 named netscape colors
  <web-color>        ::= any of the codes of the 216 standard web colors

 

In short, there are three color sets available:

  • One for configurable colors, you access them by a hex digit or (<name>)
  • One for netscape color names, you access them by (@<name>)
  • One for web colors, you access them by (#<code>)

Generally any color-markup begins with "%c" and is followed by one of the three notations. "%c0", "%c()", or "%c(default)" switch back to the default color.

Supplying an invalid or non-matching color label or value will result in switching back to the default color. (e.g. "%c(xxx)" == "%c()")