FreeBASIC  0.91.0
io_color.c
Go to the documentation of this file.
1 /* console COLOR statement */
2 
3 #include "../fb.h"
4 #include "fb_private_console.h"
5 
6 int fb_ConsoleColor( int fc, int bc, int flags )
7 {
8  const char map[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
9  int old_fg = __fb_con.fg_color;
10  int old_bg = __fb_con.bg_color;
11  int force = FALSE;
12 
13  if (!__fb_con.inited)
14  return old_fg | (old_bg << 16);
15 
16  if (!(flags & FB_COLOR_FG_DEFAULT))
17  __fb_con.fg_color = (fc & 0xF);
18  if (!(flags & FB_COLOR_BG_DEFAULT))
19  __fb_con.bg_color = (bc & 0xF);
20 
21  if ((__fb_con.inited == INIT_CONSOLE) || (__fb_con.term_type != TERM_XTERM)) {
22  /* console and any terminal but xterm do not support extended color attributes and only allow 16+8 colors */
23  if (__fb_con.fg_color != old_fg) {
24  if ((__fb_con.fg_color ^ old_fg) & 0x8) {
25  /* bright mode changed: reset attributes and force setting both back and fore colors */
27  if (__fb_con.fg_color & 0x8)
29  force = TRUE;
30  }
31  fb_hTermOut(SEQ_FG_COLOR, 0, map[__fb_con.fg_color & 0x7]);
32  }
33  if ((__fb_con.bg_color != old_bg) || (force))
34  fb_hTermOut(SEQ_BG_COLOR, 0, map[__fb_con.bg_color & 0x7]);
35  } else {
36  /* generic xterm supports 16+16 colors */
37  if (__fb_con.fg_color != old_fg)
38  fb_hTermOut(SEQ_SET_COLOR_EX, map[__fb_con.fg_color & 0x7] + (__fb_con.fg_color & 0x8 ? 90 : 30), 0);
39  if (__fb_con.bg_color != old_bg)
40  fb_hTermOut(SEQ_SET_COLOR_EX, map[__fb_con.bg_color & 0x7] + (__fb_con.bg_color & 0x8 ? 100 : 40), 0);
41  }
42 
43  return old_fg | (old_bg << 16);
44 }
45 
47 {
48  return __fb_con.inited ? (__fb_con.fg_color | (__fb_con.bg_color << 4)) : 0x7;
49 }