Create: Computing AgesBrass Docs
Reference

Colours

The 16 colours of the screens (names, numbers, sprite digits, exact shades), and any #rrggbb colour in drawings.

Every colour screen has the same 16 colours, the colours of Minecraft's dyes. Each colour has three spellings, used in different places:

  • a name, like "red", for term.colors and for drawing with gfx;
  • a number from 0 to 15, which the terminal functions take: term.colors.red is 14;
  • a hex digit, 0 to f, one per pixel in the sprites of gfx.image: e is red.

Drawings are not limited to these 16: they also take any colour written "#rrggbb" (see below).

ColourNameNumberSprite digitShade
white00#F0F0F0
orange11#F2B233
magenta22#E57FD8
light_blue33#99B2F2
yellow44#DEDE6C
lime55#7FCC19
pink66#F2B2CC
gray77#4C4C4C
light_gray88#999999
cyan99#4C99B2
purple10a#B266E5
blue11b#3366CC
brown12c#7F664C
green13d#57A64E
red14e#CC4C4C
black15f#111111

The names are written in English, in lowercase, with an underscore: light_blue, light_gray. Watch the spelling of gray (not grey). The shades are the exact colours the screens show; black is a very dark gray, the colour of an empty screen.

Brass
local NAMES = {"white", "orange", "magenta", "light_blue", "yellow", "lime", "pink", "gray",
  "light_gray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
local DARK = {[7] = true, [10] = true, [11] = true, [12] = true, [14] = true, [15] = true}
gfx.clear("black")
gfx.text(4, 6, "THE 16 COLOURS: NUMBER, SPRITE DIGIT, NAME", "white")
for n = 0, 15 do
  local x = 4 + (n % 4) * 76
  local y = 20 + (n // 4) * 38
  gfx.rect(x, y, 72, 34, n, true)
  if n == 15 then gfx.rect(x, y, 72, 34, "gray") end
  local ink = "black"
  if DARK[n] then ink = "white" end
  gfx.text(x + 4, y + 4, tostring(n), ink, 2)
  gfx.text(x + 62, y + 4, string.format("%x", n), ink)
  gfx.text(x + 4, y + 24, NAMES[n + 1], ink)
end
Screen
Screen

In the terminal: numbers

term.set_fg (the text) and term.set_bg (the cells behind it) take a number. Write it with term.colors, which is easier to read than the number itself:

Brass
term.set_fg(term.colors.orange)
print("Furnace lit")
term.set_fg(term.colors.white)
print(term.colors.red, term.colors.black)
Screen
Furnace lit
14  15

A name is not accepted there, and neither is a number outside 0 to 15:

Brass
term.set_fg("red")
Screen
snippet:1: bad argument #1 to 'set_fg' (number expe
cted, got string)

term.set_fg(16) stops with bad argument #1 to 'set_fg' (color must be 0..15). See term.

In drawings: names or numbers

Every function of gfx takes a name ("red"), a number (14) or term.colors.red: all three are the same colour. The special name "none" (or the number -1) erases, leaving the pixel empty. An unknown name stops the program with an error that lists the 16 names, none and "#rrggbb".

gfx.get_pixel answers with a number (or with the "#rrggbb" text of a pixel drawn that way, see below). To turn the number back into a name, keep the names in a list in the order of the table: the name of colour n is at index n + 1.

Brass
local NAMES = {"white", "orange", "magenta", "light_blue", "yellow", "lime", "pink", "gray",
  "light_gray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
gfx.rect(1, 1, 10, 10, "cyan", true)
print(NAMES[gfx.get_pixel(5, 5) + 1])
Screen
cyan

In sprites: hex digits

gfx.image draws a list of strings, one character per pixel. The character is the number of the colour written in hexadecimal: 0 to 9, then a (10) to f (15), capitals accepted. Any other character, usually ., leaves the pixel as it is.

Brass
local sprite = {
  "0123",
  "4567",
  "89ab",
  "cdef",
}
gfx.clear("black")
gfx.rect(9, 9, 66, 66, "white")                  -- a frame, to see the black pixel
gfx.image(10, 10, sprite, 16)
gfx.rect(99, 9, 194, 14, "white")
gfx.image(100, 10, {"0123456789abcdef"}, 12)
gfx.rect(100, 40, 192, 12, "yellow", true)       -- a yellow strip under the next sprite
gfx.image(100, 40, {"e.e.e.e.e.e.e.e."}, 12)
gfx.text(100, 58, "DOTS LEAVE THE PIXEL UNTOUCHED", "white")
Screen
Screen

In drawings, any colour: "#rrggbb"

The drawing functions of gfx also take any colour, written "#rrggbb": #, then two hex digits each for red, green and blue, from 00 to ff, like "#ff8800" (capitals work too). The shades of the table above are written this way. Only drawings have them: the text (term.set_fg, term.set_bg) and the sprites keep the 16 colours.

The Modern Computer and the LCD Monitor show such a colour exactly. The other screens show the closest of the 16. Every colour, hue across and light to dark downwards, on a Modern Computer:

Brass
local s = gfx.size()
local function clamp(v)
  return math.max(0, math.min(1, v))
end
for x = 1, s.w do
  local h = (x - 1) / s.w * 6                      -- the hue, 0 to 6
  local rgb = {clamp(math.abs(h - 3) - 1), clamp(2 - math.abs(h - 2)), clamp(2 - math.abs(h - 4))}
  for y = 1, s.h, 4 do
    local t = (y - 1) / (s.h - 1)                  -- 0 at the top, 1 at the bottom
    local c = {}
    for i = 1, 3 do
      if t < 0.5 then
        c[i] = rgb[i] + (1 - rgb[i]) * (1 - 2 * t) -- towards white
      else
        c[i] = rgb[i] * (2 - 2 * t)                -- towards black
      end
      c[i] = math.floor(c[i] * 255)
    end
    gfx.rect(x, y, 1, 4, string.format("#%02x%02x%02x", c[1], c[2], c[3]), true)
  end
end
Screen
Screen

The same chart on a Personal Computer, each shade replaced by the closest of its 16 colours:

Brass
local s = gfx.size()
local function clamp(v)
  return math.max(0, math.min(1, v))
end
for x = 1, s.w do
  local h = (x - 1) / s.w * 6                      -- the hue, 0 to 6
  local rgb = {clamp(math.abs(h - 3) - 1), clamp(2 - math.abs(h - 2)), clamp(2 - math.abs(h - 4))}
  for y = 1, s.h, 4 do
    local t = (y - 1) / (s.h - 1)                  -- 0 at the top, 1 at the bottom
    local c = {}
    for i = 1, 3 do
      if t < 0.5 then
        c[i] = rgb[i] + (1 - rgb[i]) * (1 - 2 * t) -- towards white
      else
        c[i] = rgb[i] * (2 - 2 * t)                -- towards black
      end
      c[i] = math.floor(c[i] * 255)
    end
    gfx.rect(x, y, 1, 4, string.format("#%02x%02x%02x", c[1], c[2], c[3]), true)
  end
end
Screen
Screen

gfx.get_pixel gives such a colour back as text, in lowercase, even when it is the exact shade of one of the 16:

Brass
gfx.pixel(1, 1, "#4C99B2")   -- the shade of cyan, written in hex
print(gfx.get_pixel(1, 1))
gfx.pixel(2, 1, "cyan")
print(gfx.get_pixel(2, 1))
Screen
#4c99b2
9

Screens without colours

Not every screen shows the 16 colours:

ScreenText and cell coloursDrawing (gfx)
Modern Computershownshown, "#rrggbb" exactly
Personal Computer, Microcontrollershownshown, a "#rrggbb" as the closest of the 16
Transistor Mainframe (green), Minicomputer (amber)ignored: every character glows in the phosphor colour on a dark screenevery colour except black lights up in the phosphor colour; black (and a "#rrggbb" closest to it) stays dark
Tube Computer (teletype)ignored: dark ink on paperno gfx
LCD Monitor, on any computershownshown, "#rrggbb" exactly
CRT Monitoras on the computer's own screen (the teletype's paper becomes green phosphor)as on the computer's own screen

The computer always remembers the colours a program chose; only the screen decides whether to show them. An LCD Monitor placed against a Transistor Mainframe shows them all. The same palette on the amber screen of a Minicomputer:

Brass
local NAMES = {"white", "orange", "magenta", "light_blue", "yellow", "lime", "pink", "gray",
  "light_gray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
local DARK = {[7] = true, [10] = true, [11] = true, [12] = true, [14] = true, [15] = true}
gfx.clear("black")
gfx.text(4, 6, "THE 16 COLOURS: NUMBER, SPRITE DIGIT, NAME", "white")
for n = 0, 15 do
  local x = 4 + (n % 4) * 76
  local y = 20 + (n // 4) * 38
  gfx.rect(x, y, 72, 34, n, true)
  if n == 15 then gfx.rect(x, y, 72, 34, "gray") end
  local ink = "black"
  if DARK[n] then ink = "white" end
  gfx.text(x + 4, y + 4, tostring(n), ink, 2)
  gfx.text(x + 62, y + 4, string.format("%x", n), ink)
  gfx.text(x + 4, y + 24, NAMES[n + 1], ink)
end
Screen
Screen

On these screens, only black and "not black" count: black text on a lit block disappears, and so do the white labels here, drawn on blocks that are lit too. For a program meant for them, draw in black and one other colour.

Choosing colours

A few habits make screens easier to read, in a dark factory as on a sunny monitor wall:

  • keep the meaning of a colour the same everywhere: lime or green for running and full, yellow or orange for low or busy, red for stopped, empty or overloaded;
  • write dark text (black) on light colours (white, yellow, lime, light_blue, pink...) and white text on dark ones (gray, blue, purple, brown, red, black);
  • do not rely on colour alone: add a word (OK, LOW, STOP), so that the screen still works on a Transistor Mainframe and for colour-blind players.

See Screens and monitors for the layout of a whole screen.