#include #include #define ASCII_COLOR_BRIGHT 0x8 #define ASCII_COLOR_MASK 0x7 #define populate_colors(_obj_, _color_, _attr_) \ short _color_ = colors_to_pair((char)(_obj_)->fg & ASCII_COLOR_MASK, (char)(_obj_)->bg & ASCII_COLOR_MASK); \ unsigned int _attr_ = ((char)(_obj_)->fg & ASCII_COLOR_BRIGHT) ? A_BOLD : 0; struct color_scheme { short fg, bg; }; short colors_to_pair(short foreground, short background) { short i = 0; if (!has_colors()) { return -1; } /* go through all the color pairs */ for (i = 0; i < COLOR_PAIRS-1; i++) { short f = 0; short b = 0; /* get the pair colors */ pair_content(i, &f, &b); /* check if it matches what we need ? */ if ((f == foreground) && (b == background)) { // fprintf(stderr, "found match at fg = %d, bg = %d\n", f, b); return i; } } // fprintf(stderr, "no match found!\n"); /* return the default pair for the terminal */ return 0; } static void change_chtype_color(chtype *text, int len, struct color_scheme *color_scheme) { int i = 0; populate_colors(color_scheme, color, attr); for (i = 0; irows; line++ ) { change_chtype_color(label->info[line], label->infoLen[line], color_scheme); } /* remove previous color and bold from the box atrribute */ setCDKLabelBoxAttribute(label, ~A_COLOR); setCDKLabelBoxAttribute(label, ~A_BOLD); /* set the new color to the box atrributes */ setCDKLabelBoxAttribute(label, COLOR_PAIR(color) | attr); /* set background attribute */ setCDKLabelBackgroundAttrib(label,COLOR_PAIR(color)); if (force_draw) { /* force the object drawing */ drawCDKLabel(label, ObjOf(label)->box); } } #define COLORS_COUNT 8 static CDKLABEL *labels[64][2]; void main() { CDKSCREEN *cdkscreen; WINDOW *screen; short f, b, i; int ypos = 0, xpos = 0; /* Initialize the Cdk screen. */ screen = initscr(); cdkscreen = initCDKScreen (screen); initCDKColor(); for (i = 0; i < COLOR_PAIRS; i++) { char text[36] = { 0 }, *ptr; struct color_scheme color_scheme; pair_content(i, &color_scheme.fg, &color_scheme.bg); snprintf(text, sizeof(text), "line values: i = %d, fg = %d, bg = %d", i, color_scheme.fg, color_scheme.bg); ptr = strdup(text); labels[i][0] = newCDKLabel (cdkscreen, xpos, ypos, &ptr, 1, 0, 0); labels[i][1] = newCDKLabel (cdkscreen, xpos + 37, ypos, &ptr, 1, 0, 0); change_label_color(labels[i][0], &color_scheme, 1); color_scheme.fg |= ASCII_COLOR_BRIGHT; change_label_color(labels[i][1], &color_scheme, 1); if ((COLOR_PAIRS/2 < i) && !xpos) { xpos = 74; ypos = -1; } ypos += 1; } waitCDKLabel (labels[i - 1][0], ' '); /* Clean up */ for (i = 0; i < COLOR_PAIRS-1; i++) { destroyCDKLabel (labels[i][0]); destroyCDKLabel (labels[i][1]); } destroyCDKScreen (cdkscreen); endCDK(); exit (0); }