''' Provides simple color changing for Win32 console line printing output. Based on WConio (http://newcenturycomputers.net/projects/wconio.html). Functions: fg(fgcolor, bgcolor=None) Set foreground color bg(fgcolor, bgcolor=None) Set background color normal() Set to gray fg and black bg clrscr() Clear the screen Colors available: black gray blue lblue green lgreen cyan lcyan red lred magenta lmagenta brown yellow white lwhite --------------------------------------------------------------------------- Copyright (C) 2009 Don Peterson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ''' import WConio as w # Colors black = w.BLACK blue = w.BLUE green = w.GREEN cyan = w.CYAN red = w.RED magenta = w.MAGENTA brown = w.BROWN white = w.LIGHTGRAY gray = w.DARKGRAY lblue = w.LIGHTBLUE lgreen = w.LIGHTGREEN lcyan = w.LIGHTCYAN lred = w.LIGHTRED lmagenta = w.LIGHTMAGENTA yellow = w.YELLOW lwhite = w.WHITE clrscr = w.clrscr def SetColors(fgcolor, bgcolor=None, reversed=False): # fgcolor can be a tuple of colors to allow easier binding if type(fgcolor) in (tuple, list): fgcolor, bgcolor = fgcolor if reversed: fgcolor, bgcolor = bgcolor, fgcolor if fgcolor != None: w.textcolor(fgcolor) if bgcolor != None: w.textbackground(bgcolor) def fg(fgcolor=white, bgcolor=None): SetColors(fgcolor, bgcolor) def bg(bgcolor=black, fgcolor=None): SetColors(fgcolor, bgcolor, reversed=True) def normal(): SetColors(white, black) if __name__ == "__main__": import sys out = sys.stdout.write # Display the color combinations names = { black : "black", blue : "blue", green : "green", cyan : "cyan", red : "red", magenta : "magenta", brown : "brown", gray : "gray", white : "white", lblue : "lblue", lgreen : "lgreen", lcyan : "lcyan", lred : "lred", lmagenta : "lmagenta", yellow : "yellow", lwhite : "lwhite", } low = [black, blue, green, cyan, red, magenta, brown, white] high = [gray, lblue, lgreen, lcyan, lred, lmagenta, yellow, lwhite] # Print title fg(yellow) msg = ("%s Text Colors" % __file__).center(79) out(msg + "\n") back = "Background: " msg = "Black Blue Green Cyan Red Magenta Brown White\n\n" fg(lcyan) out(back + msg) def Table(bgcolors): for fgcolor in low + high: normal() out("%-15s" % names[fgcolor]) for bgcolor in bgcolors: fg(fgcolor, bgcolor) out(" words ") normal() out(" ") normal() out("\n") Table(low) msg = "Gray lBlue lGreen lCyan lRed lMagenta Yellow lWhite\n\n" fg(lcyan) out("\n" + back + msg) Table(high) msg = '''Functions provided: fg(fgcolor, bgcolor=None) Set foreground color bg(bgcolor, fgcolor=None) Set background color normal() Set to white fg and black bg clrscr() Clear the screen ''' out("\n" + msg)