CAS Community   >   Resources   >  

Img_20180120_181410
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)

https://program-using-gnu-cpp.blogspot.co.uk/2017/07/program-special-characters.html

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

https://program-using-gnu-cpp.blogspot.co.uk/2017/11/program-cycle-through-sequence-of.html

Fruit machine: (red, yellow, amber)

https://program-using-gnu-cpp.blogspot.co.uk/2017/10/program-fruit-machine-using-while-loop.html

Multi-coloured snowstorms:

https://program-using-gnu-cpp.blogspot.co.uk/2017/10/program-rolling-display-of-character.html

More about colour codes on Wikipedia:

https://en.m.wikipedia.org/wiki/ANSI_escape_code

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.
Categories: