Home / Alt manpages / ppmrainbow(1)

  • ppmrainbow(1)
  • User command
  • linux

Generate a tiled PPM colour strip with ppmrainbow

You will create a PPM image that fades through the colours you choose from left to right, then verify its dimensions and header. The examples use the Netpbm package installed on this machine, version 11.5.2. Allow about ten minutes for a first run. You need a Linux shell, Netpbm, and a directory where you can write the output file.

Checkpoint

The finished file is a readable PPM with the intended width and height, and you know whether its final colour is the first colour repeated or the last colour supplied.

1. Check the installed command

Start by confirming which executable your shell will run. This is an ordinary, unprivileged check. Do not use sudo simply because the command creates an image; write the result in your own working directory.

$ command -v ppmrainbow
/usr/bin/ppmrainbow
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1

The installed manual describes the command as generating a PPM image from a list of colours. Its manual page is dated 12 November 2014, so the version above is the useful boundary for the examples here. If your package reports a different version, check its local manual before relying on details such as defaults.

2. Generate a simple colour strip

ppmrainbow writes the image to standard output. Redirect that output to a new filename. The colour names are passed in their left-to-right order. Netpbm accepts colour specifications understood by its colour parser, including names such as red, green and blue.

$ ppmrainbow -width 600 -height 8 red yellow blue > rainbow.ppm
$ file rainbow.ppm
rainbow.ppm: Netpbm image data, size = 600 x 8, rawbits, pixmap

The explicit dimensions make the result easy to review. The documented defaults are 600 pixels wide and 8 pixels high, so the same image size can be produced by omitting both options. Keeping them in a script is clearer when the output is part of a larger image workflow.

There is no normal progress display because the image data is the command's standard output. If you redirect only standard error, the PPM bytes will still appear in the terminal and may make the shell look broken. Always redirect standard output when creating a file.

3. Choose the ending behaviour

By default, the first colour is added again at the right edge. This is deliberate: it lets you place copies of the strip side by side without a visible jump between the last and first segments. It also means the image ends with the first supplied colour, not the last one.

$ ppmrainbow -width 900 -height 24 red green blue > repeat-first.ppm

Use -norepeat when the right edge must finish with the final colour you name:

$ ppmrainbow -width 900 -height 24 -norepeat red green blue > end-at-blue.ppm
$ file end-at-blue.ppm
end-at-blue.ppm: Netpbm image data, size = 900 x 24, rawbits, pixmap

This distinction matters when the strip is used as a background, mask source or input to another image operation. It is easy to miss because both commands produce a valid PPM. Inspect the result visually or use your normal image tooling when the exact edge colour matters.

4. Use more than two colour stops

Each additional argument becomes another stop in the left-to-right fade. Use a short, explicit list rather than relying on a later tool to repair an unexpected gradient:

$ ppmrainbow -width 1200 -height 32 '#4b0082' blue cyan green yellow orange red > spectrum.ppm
$ test -s spectrum.ppm && echo 'PPM created'
PPM created

Quoting a colour value is a safe habit when it contains shell-significant characters such as #. The arguments are colour specifications, not image filenames. A missing or misspelt specification will make the command fail rather than silently read a file from the current directory.

For a vertical strip, generate the normal horizontal image first and then use a separate Netpbm tool such as pnmrotate or pamflip. Keeping the rotation as a separate step makes it clear which command created the PPM and which command changed its orientation.

5. Protect an existing output

Warning

Shell redirection with > truncates an existing destination before ppmrainbow starts. A failed run can therefore destroy a previous image. Use a temporary name and replace the old file only after validation.

$ ppmrainbow -width 600 -height 8 red yellow blue > rainbow.ppm.new
$ file rainbow.ppm.new
rainbow.ppm.new: Netpbm image data, size = 600 x 8, rawbits, pixmap
$ mv rainbow.ppm.new rainbow.ppm

mv is the state-changing step. If the command fails, leave the original file alone and remove the incomplete temporary file after checking its contents:

$ rm rainbow.ppm.new

That removal is irreversible, so run it only when you have confirmed that the temporary result is not needed. Neither generation nor replacement normally needs elevated privileges. If the destination directory is owned by another account, fix the working location or its permissions through your normal administration process rather than making the image command run as root.

6. Diagnose a failed or surprising result

First check the exit status and preserve the diagnostic text:

$ ppmrainbow -width 0 -height 8 red blue > bad.ppm
ppmrainbow: invalid width and/or height
$ echo $?
1

Width and height must be positive. The output file may already exist as an empty or partial file because the shell opened it before the program rejected the dimensions. This is another reason to use a new temporary destination during testing.

If the file exists but file reports something unexpected, check that standard output was redirected and that the command completed successfully. A PPM normally begins with the magic number P6 on this installation:

$ head -c 2 rainbow.ppm
P6

Do not use a text editor to inspect the whole file. The header is text, but the pixel data is binary. Keep the original source image or project files because ppmrainbow does not provide an undo operation for files you overwrite.

Done means

  • command -v ppmrainbow found the intended Netpbm executable.
  • The colour arguments appear in the desired left-to-right order.
  • Width and height were checked, with -norepeat chosen deliberately when needed.
  • file reports the expected PPM dimensions and head -c 2 reports P6.
  • An existing output was protected by generating a temporary file before replacement.