Forum Archive

My solution for the color issue

jemanuel

It's pretty straightforward.

try: #For Pythonista:
    import console
    console.set_font('Menlo-Regular', 7)
    def printc_ios_helper_func(input_string, r, g, b):
        console.set_color(r, g, b)
        print(input_string)
    def printc_ios_func(input_string, color_code):
        if color_code == 'g':
            printc_ios_helper_func(input_string, 46,204,64)
        elif color_code == 'r':
            printc_ios_helper_func(input_string, 255,65,54)
        elif color_code == 'c':
            printc_ios_helper_func(input_string, 127,219,255)
        elif color_code == 'b':
            printc_ios_helper_func(input_string, 0,116,217)
        elif color_code == 'y':
            printc_ios_helper_func(input_string, 255,220,0)
        elif color_code == 'm':
            printc_ios_helper_func(input_string, 240,18,190)
        elif color_code == 'k':
            printc_ios_helper_func(input_string, 25,25,25)
    def printrc_ios_func(input_string):
        color_name_list = [(46,204,64),(255,65,54),(127,219,255),(0,116,217),(240,18,190),(25,25,25),(random.randint(25,250),random.randint(25,250),random.randint(25,250))]
        random_color = random.choice(color_name_list)
        printc_ios_helper_func(input_string, random_color[0], random_color[1], random_color[2])
    printc = printc_ios_func
    printrc = printrc_ios_func
except:
    from colorama import Fore
    def printrc(input_string):
        color_name_list = ['YELLOW','GREEN','CYAN', 'BLUE', 'RED', 'MAGENTA', 'LIGHTBLACK_EX', 'LIGHTGREEN_EX', 'LIGHTBLUE_EX', 'LIGHTCYAN_EX', 'LIGHTMAGENTA_EX', 'LIGHTRED_EX', 'LIGHTYELLOW_EX']
        print(str(eval('Fore.'+random.choice(color_name_list))) + str(input_string))
        print(Fore.RESET)
    def printc(input_string, color_code):
        if color_code == 'g':
            print(Fore.LIGHTGREEN_EX + input_string)
        elif color_code == 'r':
            print(Fore.LIGHTRED_EX + input_string)
        elif color_code == 'c':
            print(Fore.LIGHTCYAN_EX + input_string)
        elif color_code == 'b':
            print(Fore.LIGHTBLUE_EX + input_string)
        elif color_code == 'y':
            print(Fore.LIGHTYELLOW_EX + input_string)
        elif color_code == 'm':
            print(Fore.LIGHTMAGENTA_EX + input_string)
        elif color_code == 'k':
            print(Fore.LIGHTBLACK_EX + input_string)
        print(Fore.RESET)
ccc

Very cool!!

dict lookup can make you code smaller, faster, and easier to understand and modify...

        if color_code == 'g':
            printc_ios_helper_func(input_string, 46,204,64)
        elif color_code == 'r':
            printc_ios_helper_func(input_string, 255,65,54)
        elif color_code == 'c':
            printc_ios_helper_func(input_string, 127,219,255)
        elif color_code == 'b':
            printc_ios_helper_func(input_string, 0,116,217)
        elif color_code == 'y':
            printc_ios_helper_func(input_string, 255,220,0)
        elif color_code == 'm':
            printc_ios_helper_func(input_string, 240,18,190)
        elif color_code == 'k':
            printc_ios_helper_func(input_string, 25,25,25)

-->

        color_dict = {'g': (46, 204, 64),
                      'r': (255, 65, 54),
                      'c': (127, 219, 255),
                      'b': (0, 116, 217),
                      'y': (255, 220, 0),
                      'm': (240, 18, 190),
                      'k': (25, 25, 25)}
        printc_ios_helper_func(input_string, color_dict[color_code])

Also, to avoid bare exceptions change except: to except ImportError:.