CAS Community   >   Resources   >  

Using colours with text output on Mac, Pi and Ubuntu

How to output text to screen using standard ANSI colour codes

Phil Gardner

Created by Phil Gardner
last edited Jan 21 2018 by Phil Gardner

When displaying text on a screen in Linux/Unix, can send escape codes that will change the foreground or background colours.

Create constant identifiers in your programs for these sequences of escape characters and you have an easy and readable way to create colour output.

Can define your names directly in your program code, or alternatively create a reusable file e.g. “colours.h” to include from other source-code files.

Owls: (green and yellow)

Hello example: (red, green, blue, cyan)

Fruit machine: (red, yellow, amber)

Multi-coloured snowstorms:

More about colour codes on Wikipedia:

Feedback and Comments

Available when logged in (join via the front page, for free):
  • View 0 comments on this resource.
  • View resource history, links to related resources.
  • Leave feedback for the author(s), or help by editing the resource.

© 2021 BCS, The Chartered Institute for IT Registered charity: No. 292786
Using the websiteDisclaimer of liabilityCookies policyPrivacy notice