If you're printing to stdout, it depends on the terminal you're printing to. You can use ansi escape codes on xterms and other similar terminal emulators.
Here's a bash code snippet that will print all 255 colors supported by xterm, putty and Konsole:
for ((i=0;i<256;i++)); do echo -en "\e[38;5;"$i"m"$i" "; done
You can use these escape codes in any programming language. It's better to rely on a library that will decide which codes to use depending on architecture and the content of the TERM environment variable.