# File: coloring.py # Author: Dr. Gibson # Date: 12/1/2017 # Section: N/A # E-mail: k.gibson@umbc.edu # Description: # This file contains a function that allows the user to print # out a message in a different text and background color. ############################################################################## # colorMsg() prints out a message in color # Input: msg; a string, the message to be printed # text; a string, the color to make the text # back; a string, the color to make the background # Output: None (prints message to screen in desired colors) def colorMsg(msg, text, back): CODE = "\033[" # ANSI code that starts all color escape sequences RESET = CODE + "0m" # ANSI code that resets the colors to the default START = CODE + "1;" # start of ANSI code for changing colors BG_OFF = 10 # offset for background colors COLORS = {'black': '30', 'red': '31', 'green': '32', 'yellow': '33', \ 'blue': '34', 'magenta': '35', 'cyan': '36', 'white': '37'} # set text and background color ANSI codes codeT = COLORS[text] codeBG = ";" + str( int(COLORS[back]) + BG_OFF) + "m" # print message in desired colors print(START + codeT + codeBG + msg + RESET) def main(): colorMsg("Hello world!", "red", "black") # dummy values to show functionality for printing variable values row = 7 col = 5 msg1 = "\tCurrently at position [" + str(row) + ", " + str(col) + "]" colorMsg(msg1, "magenta", "white") colorMsg("This semester has been ruff", "yellow", "red") main()