< index
< 2. Console emulator
< 2.2 Drawing on the root console

=====================================
Changing the colors while printing a string
=====================================

If you want to draw a string using different colors for each word, the basic solution is to call a string printing function several times, changing the default colors between each call.
The TCOD library offers a simpler way to do this, allowing you to draw a string using different colors in a single call. For this, you have to insert color control codes in your string.
A color control code is associated with a color set (a foreground color and a background color). If you insert this code in your string, the next characters will use the colors associated with the color control code.
There are 5 predefined color control codes :
For python, remove TCOD_ : libtcod.COLCTRL_1

	TCOD_COLCTRL_1
	TCOD_COLCTRL_2 
	TCOD_COLCTRL_3 
	TCOD_COLCTRL_4 
	TCOD_COLCTRL_5

To associate a color with a code, use setColorControl.
To go back to the console's default colors, insert in your string the color stop control code :

	TCOD_COLCTRL_STOP

Example :
A string with a red over black word, using predefined color control codes :

C++ : TCODConsole::setColorControl(TCOD_COLCTRL_1,TCODColor::red,TCODColor::black);
      TCODConsole::root->printLeft(1,1,TCOD_BKGND_SET, "String with a %cred%c word.",TCOD_COLCTRL_1,TCOD_COLCTRL_STOP);
C   : TCOD_console_set_color_control(TCOD_COLCTRL_1,red,black);
      TCOD_console_print_left(NULL,1,1,TCOD_BKGND_SET, "String with a %cred%c word.",TCOD_COLCTRL_1,TCOD_COLCTRL_STOP);
Py  : libtcod.console_set_color_control(libtcod.COLCTRL_1,litbcod.red,litbcod.black)
      libtcod.console_print_left(0,1,1,libtcod.BKGND_SET, "String with a %cred%c word."%(libtcod.COLCTRL_1,libtcod.COLCTRL_STOP))

You can also use any color without assigning it to a control code, using the generic control codes :

	TCOD_COLCTRL_FORE_RGB
	TCOD_COLCTRL_BACK_RGB

Those controls respectively change the foreground and background color used to print the string characters. In the string, you must insert the r,g,b components of the color (between 1 and 255. The value 0 is forbidden because it represents the end of the string in C/C++) immediately after this code.
Example :
A string with a red word (over default background color), using generic color control codes :

C++ : TCODConsole::root->printLeft(1,1,TCOD_BKGND_SET, "String with a %c%c%c%cred%c word.",TCOD_COLCTRL_FORE_RGB,255,1,1,TCOD_COLCTRL_STOP);
C   : TCOD_console_print_left(NULL,1,1,TCOD_BKGND_SET, "String with a %c%c%c%cred%c word.",TCOD_COLCTRL_FORE_RGB,255,1,1,TCOD_COLCTRL_STOP);
Py  : litbcod.console_print_left(0,1,1,libtcod.BKGND_SET, "String with a %c%c%c%cred%c word."%(libtcod.COLCTRL_FORE_RGB,255,1,1,libtcod.COLCTRL_STOP))

A string with a red over black word, using generic color control codes :

C++ : TCODConsole::root->printLeft(1,1,TCOD_BKGND_SET, "String with a %c%c%c%c%c%c%c%cred%c word.",
            TCOD_COLCTRL_FORE_RGB,255,1,1,TCOD_COLCTRL_BACK_RGB,1,1,1,TCOD_COLCTRL_STOP);
C   : TCOD_console_print_left(NULL,1,1,TCOD_BKGND_SET, "String with a %c%c%c%c%c%c%c%cred%c word.",
            TCOD_COLCTRL_FORE_RGB,255,1,1,TCOD_COLCTRL_BACK_RGB,1,1,1,TCOD_COLCTRL_STOP);
Py  : libtcod.console_print_left(0,1,1,libtcod.BKGND_SET, "String with a %c%c%c%c%c%c%c%cred%c word."%
            (libtcod.COLCTRL_FORE_RGB,255,1,1,libtcod.COLCTRL_BACK_RGB,1,1,1,libtcod.COLCTRL_STOP))

insert a comment