short vdk_screen_get_color_pair(vdk_screen_t *screen,short fg,short bg) { short color_pair; short fg_color; short bg_color; short i; if(screen == NULL) return -1; if(fg == COLOR_WHITE && bg == COLOR_BLACK) return 0; // use fast color indexing when possible if(screen->state & VDK_SCREEN_FAST_COLOR) { color_pair = (bg * COLORS) + (COLORS - fg -1); return color_pair; } // safe color indexing (slower) for(i = 1;i < COLOR_PAIRS;i++) { pair_content(i,&fg_color,&bg_color); if(fg_color == fg && bg_color == bg) break; } return i; }
/* Checks whether color pair number pair has specified foreground (fg) and * background (bg) colors. */ static int color_pair_matches(int pair, int fg, int bg) { short pair_fg, pair_bg; pair_content(pair, &pair_fg, &pair_bg); return (pair_fg == fg && pair_bg == bg); }
int main(void) { short pair,f,b; initscr(); start_color(); /* Create color pairs */ init_pair(1,COLOR_WHITE,COLOR_BLUE); init_pair(2,COLOR_BLACK,COLOR_RED); init_pair(3,COLOR_YELLOW,COLOR_RED); init_pair(4,COLOR_BLUE,COLOR_GREEN); init_pair(5,COLOR_CYAN,COLOR_MAGENTA); /* display pair colors */ for(pair=1;pair<=5;pair++) { attrset(COLOR_PAIR(pair)); pair_content(pair,&f,&b); printw("Pair %d: %s foreground, %s background.\n",\ pair,ncolor(f),ncolor(b)); } refresh(); getch(); endwin(); return 0; }
/* * If there is an established color pair with the given fg and bg * colors, return it. Else allocate a new pair with these colors * and return that. */ static int curses_colors_find(int fg, int bg) { int pair_no; short fge, bge; for (pair_no = 0; pair_no <= allocated_colors && pair_no < COLOR_PAIRS; pair_no++) { pair_content(pair_no, &fge, &bge); if (fg == fge && bg == bge) return(pair_no); } /* * No pair was found, allocate a new one. */ if (allocated_colors < (COLOR_PAIRS-1)) { allocated_colors++; init_pair(allocated_colors, fg, bg); return(allocated_colors); } /* * No space to allocate a new one, return error. */ return(-1); }
int CursesRenderer::getPair(Colour fg, Colour bg) { int pair = -1; for (int i = 0; i < COLOR_PAIRS; i++) { short c1, c2; pair_content(i, &c1, &c2); if (c1 == fg && c2 == bg) { pair = i; break; } } if (pair == -1) { for (unsigned int i = 1; i < myColourMap.size(); i++) if (!myColourMap[i]) { pair = i; init_pair(pair, fg, bg); myColourMap[i] = true; break; } } if (pair == -1) pair = 0; return pair; }
inline gshort viper_color_pair(gshort fg,gshort bg) { gshort color_pair; gshort fg_color,bg_color; gint i; extern guint32 viper_global_flags; if(fg==COLOR_WHITE && bg==COLOR_BLACK) return 0; /* use fast color indexing when possible. */ if(viper_global_flags & VIPER_FASTCOLOR) { color_pair=(bg*COLORS)+(COLORS-fg-1); return color_pair; } /* safe color indexing (slower) */ for(i=1; i<COLOR_PAIRS; i++) { pair_content(i,&fg_color,&bg_color); if(fg_color==fg && bg_color==bg) break; } return i; }
char *attr_to_string( int code ) { static char result[136]; unsigned int i; int pair = ( code & 65280 ) >> 8; int bold = 0; if ( bold ) code &= -2097153; result[0] = 0; i = 0; for ( ; i <= 6; i++ ) { if ( Mono_Attrs[ i ].code & code ) { if ( result[0] ) { memcpy( &result[ strlen( result ) ] ); } strcat( result, Mono_Attrs[ i ].name ); } // i++; } if ( pair && pair_content( pair, &f, &b ) != -1 ) { char *fg = lookup_color( f ); char *bg = lookup_color( b ); if ( result[0] ) strcat( result, "+" ); sprintf( &result[ strlen( result ) ], "%s/%s", fg, bg ); } return result; }
static short color_pair_get(short fg, short bg) { static bool has_default_colors; static short *color2palette, default_fg, default_bg; static short color_pairs_max, color_pair_current; if (!color2palette) { pair_content(0, &default_fg, &default_bg); if (default_fg == -1) default_fg = COLOR_WHITE; if (default_bg == -1) default_bg = COLOR_BLACK; has_default_colors = (use_default_colors() == OK); color_pairs_max = MIN(COLOR_PAIRS, MAX_COLOR_PAIRS); if (COLORS) color2palette = calloc((COLORS + 2) * (COLORS + 2), sizeof(short)); } if (fg >= COLORS) fg = default_fg; if (bg >= COLORS) bg = default_bg; if (!has_default_colors) { if (fg == -1) fg = default_fg; if (bg == -1) bg = default_bg; } if (!color2palette || (fg == -1 && bg == -1)) return 0; unsigned int index = color_pair_hash(fg, bg); if (color2palette[index] == 0) { short oldfg, oldbg; if (++color_pair_current >= color_pairs_max) color_pair_current = 1; pair_content(color_pair_current, &oldfg, &oldbg); unsigned int old_index = color_pair_hash(oldfg, oldbg); if (init_pair(color_pair_current, fg, bg) == OK) { color2palette[old_index] = 0; color2palette[index] = color_pair_current; } } return color2palette[index]; }
short colour_pair::find_slot_with_colour(short fg, short bg) { short ft, bt; for(short i=0; i < short(slot_counts.size()); ++i) { pair_content(i+1, &ft, &bt); if(ft==fg && bg==bt) return i; } return -1; //colour not found }
static chtype merge_colors(chtype foreground, chtype background) { chtype result = foreground; if ((foreground & A_COLOR) != (background & A_COLOR)) { short fg_f, bg_f; short fg_b, bg_b; short fg_pair = (short) PAIR_NUMBER(foreground); short bg_pair = (short) PAIR_NUMBER(background); if (pair_content(fg_pair, &fg_f, &bg_f) != ERR && pair_content(bg_pair, &fg_b, &bg_b) != ERR) { result &= ~A_COLOR; result |= dlg_color_pair(fg_f, bg_b); } } return result; }
static void COB_NOINLINE cob_screen_init (void) { char *s; if (!cob_screen_initialized) { s = getenv ("COB_SCREEN_EXCEPTIONS"); if (s) { if (*s == 'Y' || *s == 'y') { cob_extended_status = 1; s = getenv ("COB_SCREEN_ESC"); if (s) { if (*s == 'Y' || *s == 'y') { cob_use_esc = 1; } } } } /* Get default insert mode, if 'Y' set to on */ s = getenv ("COB_INSERT_MODE"); if (s) { if (*s == 'Y' || *s == 'y') { insert_mode = 1; } } fflush (stdout); fflush (stderr); if (!initscr ()) { cob_runtime_error ("Failed to initialize curses"); cob_stop_run (1); } cbreak (); keypad (stdscr, 1); nl (); noecho (); if (has_colors ()) { start_color (); pair_content ((short)0, &fore_color, &back_color); if (COLOR_PAIRS) { #ifdef HAVE_LIBPDCURSES size_t i; /* pdcurses sets ALL pairs to default fg/bg */ /* IMHO a bug. */ for (i = 1; i < (size_t)COLOR_PAIRS; ++i) { init_pair ((short)i, 0, 0); } #endif cob_has_color = 1; } } attrset (A_NORMAL); getmaxyx (stdscr, cob_max_y, cob_max_x); cob_screen_initialized = 1; } }
EIF_INTEGER c_ecurses_pair_content (EIF_INTEGER n, EIF_INTEGER *f, EIF_INTEGER *g) { EIF_INTEGER _result; short f1, g1; f1 = (short) *((short *)f); g1 = (short) *((short *)g); _result = pair_content (n, &f1, &g1); *f = (EIF_INTEGER) f1; *g = (EIF_INTEGER) g1; return _result; };
void CNucleotide::create_colors() { short bg = 0; pair_content(0, 0, &bg); init_pair(ADE, COLOR_BLUE, bg); init_pair(THY, COLOR_YELLOW, bg); init_pair(GUA, COLOR_GREEN, bg); init_pair(CYT, COLOR_RED, bg); init_pair(URA, COLOR_MAGENTA, bg); }
/* Return the foreground and background color numbers of a given PAIR */ SCM gucu_pair_content (SCM s_pair) { int ret; short c_fore, c_back; ret = pair_content (scm_to_short (s_pair), &c_fore, &c_back); if (ret == OK) { return scm_list_2 (scm_from_short (c_fore), scm_from_short (c_back)); } else return SCM_BOOL_F; }
int vdk_screen_set_colors(vdk_screen_t *screen,short color_pair) { if(screen == NULL) return -1; if(screen->state & VDK_SCREEN_PAUSED) return -1; if(!(screen->state & VDK_SCREEN_COLORIZED)) return -1; if(color_pair < 0) return -1; wbkgdset(stdscr,COLOR_PAIR(color_pair)); wcolor_set(stdscr,color_pair,NULL); pair_content(color_pair,&screen->fg,&screen->bg); return 0; }
static void viper_kmio_show_mouse(MEVENT *mouse_event) { extern VIPER *viper; extern WINDOW *SCREEN_WINDOW; WINDOW *screen_window; static chtype color; gshort fg,bg; screen_window=SCREEN_WINDOW; if(viper->console_mouse==NULL) { viper->console_mouse=newwin(1,1,0,0); color=mvwinch(screen_window,0,0); pair_content(PAIR_NUMBER(color & A_COLOR),&fg,&bg); if(bg==COLOR_RED || bg==COLOR_YELLOW || bg==COLOR_MAGENTA) color=VIPER_COLORS(COLOR_CYAN,COLOR_CYAN); if(bg==COLOR_CYAN || bg==COLOR_BLUE) color=VIPER_COLORS(COLOR_YELLOW,COLOR_YELLOW); } if(mouse_event!=NULL) { color=mvwinch(screen_window,mouse_event->y,mouse_event->x); pair_content(PAIR_NUMBER(color & A_COLOR),&fg,&bg); if(bg==COLOR_RED || bg==COLOR_YELLOW || bg==COLOR_MAGENTA) color=VIPER_COLORS(COLOR_CYAN,COLOR_CYAN); else color=VIPER_COLORS(COLOR_YELLOW,COLOR_YELLOW); mvwin(viper->console_mouse,mouse_event->y,mouse_event->x); } mvwaddch(viper->console_mouse,0,0,' ' | color); return; }
/*** Return the foreground and background colors associated with a color pair id. @function pair_content @int pair color pair id to act on @treturn int foreground color of *pair* @treturn int background color of *pair* @see can_change_color(3x) @see init_pair */ static int Ppair_content(lua_State *L) { short pair = checkint(L, 1); short f; short b; int ret = pair_content(pair, &f, &b); if (ret == ERR) return 0; lua_pushinteger(L, f); lua_pushinteger(L, b); return 2; }
int main(void) { chtype ch,a; char bgchar; int bgcolor,x; short fore,back; char colors[8][8] = { "Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White" }; char attribs[15][11] = { "Standout", "Underline", "Reverse", "Blink", "Dim", "Bold", "AltChar", "Invis", "Protect", "Horizontal", "Left", "Low", "Right", "Top", "Vertical" }; a = 0x10000; initscr(); start_color(); init_pair(1,COLOR_WHITE,COLOR_BLUE); bkgd(COLOR_PAIR(1) | A_BOLD); ch = getbkgd(stdscr); bgchar = (ch & A_CHARTEXT); /* Read character */ bgcolor = (ch & A_COLOR) >> 8; /* Read color pair */ pair_content(bgcolor,&fore,&back); /* Read colors */ printw("Background chtype is 0x%04x\n",ch); printw("Background character is 0x%02x or '%c'\n",\ bgchar,bgchar); printw("Background color pair is %d\n",bgcolor); printw("\tForeground color is %s\n",colors[fore]); printw("\tBackground color is %s\n",colors[back]); addstr("Other attributes found:\n"); for(x=0; x<15; x++) { if(a & ch) printw("%s\n",attribs[x]); a <<= 1; } refresh(); getch(); endwin(); return 0; }
gint viper_form_driver(FORM *form,gint request,guint32 flags, chtype active,chtype normal,gshort cursor_color) { WINDOW *window; chtype eraser; chtype temp_ch; gint x,y; gint retval; gshort fg,bg; if(form==NULL) return ERR; if(form_sub(form)!=form_win(form)) window=form_sub(form); else window=form_win(form); getyx(window,y,x); eraser=field_back(current_field(form)); mvwchgat(window,y,x,1,(eraser & A_ATTRIBUTES), PAIR_NUMBER(eraser & A_COLOR),NULL); retval=form_driver(form,request); if(flags & FORM_COLORIZE) viper_form_colorize(form,active,normal,active,normal); if(flags & FORM_CURSOR_NONE) return retval; temp_ch=termattrs(); if((flags & FORM_CURSOR_ULINE) && !(temp_ch & A_UNDERLINE)) return ERR; getyx(window,y,x); temp_ch=field_fore(current_field(form)); if(flags & FORM_CURSOR_ULINE) mvwchgat(window,y,x,1,(temp_ch & A_ATTRIBUTES) | A_UNDERLINE, PAIR_NUMBER(temp_ch & A_COLOR),NULL); else { pair_content(PAIR_NUMBER(temp_ch & A_COLOR),&fg,&bg); if(cursor_color!=-1) { bg=cursor_color; mvwchgat(window,y,x,1,A_NORMAL,viper_color_pair(fg,bg),NULL); } else mvwchgat(window,y,x,1,A_REVERSE,viper_color_pair(fg,bg),NULL); } return E_OK; }
short find_color_pair(short fg,short bg) { short fg_color,bg_color; int i; if(has_colors()==FALSE) return -1; for(i=1;i<COLOR_PAIRS;i++) { pair_content(i,&fg_color,&bg_color); if(fg_color==fg && bg_color==bg) break; } if(i==COLOR_PAIRS) return -1; return i; }
/* This function prints the cash the player has with optional prefix as well as screen coordinates. Please note that offsetx is the offset from the right of the screen, y is the offset from the top as always. */ void printfunds(int y, int offsetx, const char* prefix) { char moneystr[50]; char prefixbuffer[50]; if(prefix==NULL) { strncpy(prefixbuffer,"",50); } else { strncpy(prefixbuffer,prefix,50); } sprintf(moneystr,"$%d",ledger.get_funds()); //Save screen coordinates for later. int begy,begx; getyx(stdscr,begy,begx); //Save color and brightness information for later. short colorpair; short front, back; char dim; attr_t attrs; attr_get(&attrs,&colorpair,NULL); if((attrs & WA_DIM)==0) dim=0; else dim=1; pair_content(colorpair,&front,&back); //Move, set color, and write. move(y,80-strlen(moneystr)-strlen(prefixbuffer)-offsetx); addstr(prefixbuffer); set_color(COLOR_GREEN,COLOR_BLACK,1); addstr(moneystr); //Recover old settings move(begy,begx); set_color(front,back,dim); }
// color_content() seems to give you the default ncurses value (one of 0, 680 // or 1000), *not* the actual value being used by the terminal... :/ This // function is not likely useful until we can get the latter (we don't want // generally to restore the (hideous) ncurses defaults). int preserve_colors(void){ int ret = OK,q; if(colorpairs_allowed >= 0 || colors_allowed >= 0){ return ERR; } colors_allowed = COLORS; colorpairs_allowed = COLOR_PAIRS; if(colors_allowed > COLOR_CEILING || colorpairs_allowed > COLORPAIR_CEILING){ return ERR; } for(q = 0 ; q < colorpairs_allowed ; ++q){ ret |= pair_content(q,ofg + q,obg + q); } for(q = 0 ; q < colors_allowed ; ++q){ ret |= color_content(q,oor + q,oog + q,oob + q); } return ret; }
/* * Return a string defining the color theme in the form: * foreground color 'on' background color * in order to dump this data in the configuration file. * Color numbers follow the ncurses library definitions. * If ncurses library was compiled with --enable-ext-funcs, * then default color is -1. */ static char *config_color_theme_name(void) { #define MAXCOLORS 8 #define NBCOLORS 2 #define DEFAULTCOLOR 255 #define DEFAULTCOLOR_EXT -1 char *theme; int i; short color[NBCOLORS]; const char *color_name[NBCOLORS]; const char *default_color = "default"; const char *name[MAXCOLORS] = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" }; if (!colorize) { return mem_strdup("none"); } pair_content(COLR_CUSTOM, &color[0], &color[1]); for (i = 0; i < NBCOLORS; i++) { if ((color[i] == DEFAULTCOLOR) || (color[i] == DEFAULTCOLOR_EXT)) { color_name[i] = default_color; } else if (color[i] >= 0 && color[i] <= MAXCOLORS) { color_name[i] = name[color[i]]; } else { EXIT(_("unknown color")); /* NOTREACHED */ } } asprintf(&theme, "%s on %s", color_name[0], color_name[1]); return theme; }
/* Reset used list */ void owl_fmtext_reset_colorpairs(void) { if (owl_global_get_hascolors(&g)) { short i, j; owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g); cpmgr->next = 8; /* The test is <= because we allocated COLORS+1 entries. */ for(i = 0; i <= COLORS; i++) { for(j = 0; j <= COLORS; j++) { cpmgr->pairs[i][j] = -1; } } for(i = 0; i < 8; i++) { short fg, bg; if (i >= COLORS) continue; pair_content(i, &fg, &bg); cpmgr->pairs[fg+1][bg+1] = i; } } }
static void console_savecolors(struct NcConsole *self) { short int i; short int r,g,b; short int fg, bg; struct color_reader cr; int use_cr; use_cr = self->flags & NC_SUPPORTSCOLORREADER && !color_reader_init(&cr); int maxc = MIN(self->maxcolors, CONSOLE_MAXSAVECOLORS); for (i = MIN_COLOR; i < maxc; i++) { if(use_cr) color_reader_get_color(&cr, i, &self->org_colors[i]); else { color_content(i, &r, &g, &b); self->org_colors[i] = RGB(console_fromthousand(r), console_fromthousand(g), console_fromthousand(b)); } } if(use_cr) color_reader_close(&cr); for (i = MIN_PAIR; i < maxc+MIN_PAIR; i++) { pair_content(i, &fg, &bg); self->org_fgcolors[i-MIN_PAIR] = fg; self->org_bgcolors[i-MIN_PAIR] = bg; } }
int color_str(int y, int x, short fg_color, short bg_color, const char * str) { short i; // Search all the pair of colors // to match with the given one. // Then apply the specific pair. // Naive way for (i = 1;i < COLOR_PAIRS;i++) { short f, b; pair_content(i, &f, &b); if (f == fg_color && b == bg_color) break; } attron(COLOR_PAIR(i)); mvaddstr(y,x,str); attroff(COLOR_PAIR(i)); return 0; }
/*** Color Pair manager ***/ void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr) { /* This could be a bitarray if we wanted to save memory. */ short i, j; cpmgr->next = 8; /* The test is <= because we allocate COLORS+1 entries. */ cpmgr->pairs = owl_malloc((COLORS+1) * sizeof(short*)); for(i = 0; i <= COLORS; i++) { cpmgr->pairs[i] = owl_malloc((COLORS+1) * sizeof(short)); for(j = 0; j <= COLORS; j++) { cpmgr->pairs[i][j] = -1; } } if (owl_global_get_hascolors(&g)) { for(i = 0; i < 8; i++) { short fg, bg; if (i >= COLORS) continue; pair_content(i, &fg, &bg); cpmgr->pairs[fg+1][bg+1] = i; } } }
short ColorManager::checkColorPair(short wanted_fore, int wanted_back){ int i; short fore, back; if ( (wanted_fore > 16) || (wanted_back > 8) || (wanted_fore < 0) || (wanted_back < 0) ){ return 0; } for (i = 0; i < m_colorPaircount; i++) { pair_content((short) i, &fore, &back); if ( (fore == wanted_fore) && (back == wanted_back) ) return (short) i; } init_pair((short) m_colorPaircount, wanted_fore, (short) wanted_back); m_colorPaircount++; return (short) (m_colorPaircount - 1); }
short find_add_pair(const short fg, const short bg) /* {{{ */ { /* find a color pair with specified content or create a new one */ short tmpfg, tmpbg, pair, free_pair = -1; int ret; /* look for an existing pair */ for (pair=1; pair<COLOR_PAIRS; pair++) { if (pairs_used[pair]) { ret = pair_content(pair, &tmpfg, &tmpbg); if (ret == ERR) continue; if (tmpfg == fg && tmpbg == bg) return pair; } else if (free_pair==-1) free_pair = pair; } /* return a new pair */ return add_color_pair(free_pair, fg, bg); } /* }}} */
int cursesAttribute(unsigned char dosAttribute) { /* this doesn't support non-default background. * if we want that, we need to define 64 color * pairs and go from there. */ int ch = 0; short a, b; pair_content(dosAttribute, &a, &b); if(!a && !b) ch = COLOR_PAIR(COLOR_WHITE) | A_BOLD; else ch = COLOR_PAIR(dosAttribute); if (dosAttribute > 8) { ch |= A_BOLD; } else if ((dosAttribute & (7 << 4)) > 8) { ch |= A_BOLD; } if (!(dosAttribute & FOREGROUND_INTENSITY)) ch |= A_DIM; if (dosAttribute & BACKGROUND_INTENSITY) ch |= A_REVERSE; return ch; }