1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <curses.h>
#include <stdio.h> // vs(n)printf(), va_list, etc.
#include <ctype.h> // toupper(), isupper()
const short COLOR_BRIGHT = 8;
const short BACK_COLOR = COLOR_WHITE + COLOR_BRIGHT;
const short FORE_COLOR = COLOR_GREEN;
const int MAX_STRING = 512;
const char SET_BACK = '&';
const char SET_FORE = '^';
inline chtype COLORPAIR( short x, short y ) {
return COLOR_PAIR( x * COLORS + y );
}
void wsconout( WINDOW* win, char* strIn ) {
short fore = FORE_COLOR;
short back = BACK_COLOR;
for( char* str = strIn; /*empty*/; ++str )
{
switch( *str ) {
case '\0': // end-of-string, return
goto ResetDefaultsAndReturn;
default: // Output all unrecognized chars literally
waddch( win, *str );
continue;
case SET_BACK:
case SET_FORE:
{
char mode = *str++;
short newColor;
switch( toupper( *str )) {
case '\0': // end-of-string, output lone mode char and return
waddch( win, mode );
goto ResetDefaultsAndReturn;
default: // Print something we don't recognize as is
waddch( win, mode );
// Just print one mode char for two in a row
if( *str != mode ) waddch( win, *str );
continue;
case 'D': // Reset default colors (both fore and back)
back = BACK_COLOR;
fore = FORE_COLOR;
wattrset( win, COLORPAIR( fore, back ));
continue;
case 'K': newColor = COLOR_BLACK; break;
case 'W': newColor = COLOR_WHITE; break;
case 'R': newColor = COLOR_RED; break;
case 'G': newColor = COLOR_GREEN; break;
case 'B': newColor = COLOR_BLUE; break;
case 'C': newColor = COLOR_CYAN; break;
case 'Y': newColor = COLOR_YELLOW; break;
case 'P': newColor = COLOR_MAGENTA; break;
}
newColor += isupper( *str ) ? COLOR_BRIGHT : 0; // Set brightness
if( mode == SET_BACK ) back = newColor;
else fore = newColor;
wattrset( win, COLORPAIR( fore, back ));
}
continue;
}
}
ResetDefaultsAndReturn:
wattrset( win, COLORPAIR( FORE_COLOR, BACK_COLOR ));
}
void wvconout( WINDOW* win, char* fmt, va_list args ) {
char str[ MAX_STRING ];
_vsnprintf( str, MAX_STRING, fmt, args ); // protects from overrun
// vsprintf( str, fmt, args ); // doesn't protect from overrun
wsconout( win, str );
}
void wconout( WINDOW* win, char* fmt, ... ) {
va_list args;
va_start( args, fmt );
wvconout( win, fmt, args );
va_end( args );
}
void conout( char *fmt, ... ) {
va_list args;
va_start( args, fmt );
wvconout( stdscr, fmt, args );
va_end( args );
}
void init_color_pairs() {
short f, b;
for( f = 0; f < COLORS; ++f )
for( b = 0; b < COLORS; ++b )
init_pair( f * COLORS + b, f, b );
}
int startwin() {
initscr();
start_color();
init_color_pairs();
bkgd( COLORPAIR( FORE_COLOR, BACK_COLOR ));
}
int main( int argc, char *argv[] ) {
startwin();
conout( "Default color\n" );
conout( "^G&KBright Green On Bright Black\n" );
conout( "^g&kDark Green on Dark Black\n" );
conout( "Back to default color\n" );
conout( "^C&bCyan on blue^D back to defaults\n" );
conout( "^cW^Ch^ya^Yt'^gs ^G^^^bu^rp^p^^^R?\n" );
conout( "Back to default fore && back\n" );
getch();
endwin();
}
|