|
| 1 | +/* csol |
| 2 | + * Copyright (c) 2017 Niels Sonnich Poulsen (http://nielssp.dk) |
| 3 | + * Licensed under the MIT license. |
| 4 | + * See the LICENSE file or http://opensource.org/licenses/MIT for more information. |
| 5 | + */ |
| 6 | + |
| 7 | +#include "color.h" |
| 8 | + |
| 9 | +#include <string.h> |
| 10 | +#ifdef USE_PDCURSES |
| 11 | +#include <curses.h> |
| 12 | +#else |
| 13 | +#include <ncurses.h> |
| 14 | +#endif |
| 15 | + |
| 16 | +struct color_name { |
| 17 | + char *name; |
| 18 | + int index; |
| 19 | +}; |
| 20 | + |
| 21 | +struct color_name default_colors[] = { |
| 22 | + {"default", -1}, |
| 23 | + {"black", COLOR_BLACK}, |
| 24 | + {"red", COLOR_RED}, |
| 25 | + {"green", COLOR_GREEN}, |
| 26 | + {"yellow", COLOR_YELLOW}, |
| 27 | + {"blue", COLOR_BLUE}, |
| 28 | + {"magenta", COLOR_MAGENTA}, |
| 29 | + {"cyan", COLOR_CYAN}, |
| 30 | + {"white", COLOR_WHITE}, |
| 31 | + {"bright_black", COLOR_BLACK | 8}, |
| 32 | + {"bright_red", COLOR_RED | 8}, |
| 33 | + {"bright_green", COLOR_GREEN | 8}, |
| 34 | + {"bright_yellow", COLOR_YELLOW | 8}, |
| 35 | + {"bright_blue", COLOR_BLUE | 8}, |
| 36 | + {"bright_magenta", COLOR_MAGENTA | 8}, |
| 37 | + {"bright_cyan", COLOR_CYAN | 8}, |
| 38 | + {"bright_white", COLOR_WHITE | 8}, |
| 39 | + {NULL, -1} |
| 40 | +}; |
| 41 | + |
| 42 | +static short find_color(Theme *theme, short index, char *name) { |
| 43 | + if (name) { |
| 44 | + struct color_name *default_color; |
| 45 | + Color *color; |
| 46 | + for (color = theme->colors; color; color = color->next) { |
| 47 | + if (strcmp(color->name, name) == 0) { |
| 48 | + return color->index; |
| 49 | + } |
| 50 | + } |
| 51 | + for (default_color = default_colors; default_color->name; default_color++) { |
| 52 | + if (strcmp(default_color->name, name) == 0) { |
| 53 | + return default_color->index; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return index; |
| 58 | +} |
| 59 | + |
| 60 | +static void find_and_init_color_pair(Theme *theme, short index, ColorPair color_pair) { |
| 61 | + short fg = find_color(theme, color_pair.fg, color_pair.fg_name); |
| 62 | + short bg = find_color(theme, color_pair.bg, color_pair.bg_name); |
| 63 | + if (fg >= COLORS) { |
| 64 | + printf("Unsupported color index: %d\n", fg); |
| 65 | + } |
| 66 | + if (bg >= COLORS) { |
| 67 | + printf("Unsupported color index: %d\n", bg); |
| 68 | + } |
| 69 | + init_pair(index, fg, bg); |
| 70 | +} |
| 71 | + |
| 72 | +void init_theme_colors(Theme *theme) { |
| 73 | + Color *color; |
| 74 | + short index = 15; |
| 75 | + start_color(); |
| 76 | + for (color = theme->colors; color; color = color->next) { |
| 77 | + short color_index = color->name ? ++index : color->index; |
| 78 | + color_content(color_index, &color->old_red, &color->old_green, &color->old_blue); |
| 79 | + if (init_color(color_index, color->red, color->green, color->blue) == 0) { |
| 80 | + color->index = color_index; |
| 81 | + } else { |
| 82 | + printf("Unable to initialize color: %s\n", color->name); |
| 83 | + } |
| 84 | + } |
| 85 | + find_and_init_color_pair(theme, COLOR_PAIR_BACKGROUND, theme->background); |
| 86 | + find_and_init_color_pair(theme, COLOR_PAIR_EMPTY, theme->empty_layout.color); |
| 87 | + find_and_init_color_pair(theme, COLOR_PAIR_BACK, theme->back_layout.color); |
| 88 | + find_and_init_color_pair(theme, COLOR_PAIR_RED, theme->red_layout.color); |
| 89 | + find_and_init_color_pair(theme, COLOR_PAIR_BLACK, theme->black_layout.color); |
| 90 | +} |
| 91 | + |
| 92 | +void ui_list_colors() { |
| 93 | + int i, pair = 0; |
| 94 | + initscr(); |
| 95 | + curs_set(0); |
| 96 | + start_color(); |
| 97 | + printw("Colors: %d Color pairs: %d\n", COLORS, COLOR_PAIRS); |
| 98 | + printw("Standard colors\n"); |
| 99 | + for (i = 0; i < 8; i++) { |
| 100 | + switch (i) { |
| 101 | + case COLOR_BLACK: |
| 102 | + printw("%-8s", "black"); |
| 103 | + break; |
| 104 | + case COLOR_RED: |
| 105 | + printw("%-8s", "red"); |
| 106 | + break; |
| 107 | + case COLOR_GREEN: |
| 108 | + printw("%-8s", "green"); |
| 109 | + break; |
| 110 | + case COLOR_YELLOW: |
| 111 | + printw("%-8s", "yellow"); |
| 112 | + break; |
| 113 | + case COLOR_BLUE: |
| 114 | + printw("%-8s", "blue"); |
| 115 | + break; |
| 116 | + case COLOR_MAGENTA: |
| 117 | + printw("%-8s", "magenta"); |
| 118 | + break; |
| 119 | + case COLOR_CYAN: |
| 120 | + printw("%-8s", "cyan"); |
| 121 | + break; |
| 122 | + case COLOR_WHITE: |
| 123 | + printw("%-8s", "white"); |
| 124 | + break; |
| 125 | + default: |
| 126 | + printw("%-8s", ""); |
| 127 | + } |
| 128 | + } |
| 129 | + printw("\n"); |
| 130 | + for (i = 0; i < 8; i++) { |
| 131 | + init_pair(++pair, i, 0); |
| 132 | + attron(COLOR_PAIR(pair)); |
| 133 | + printw(" %2d ", i); |
| 134 | + init_pair(++pair, 0, i); |
| 135 | + attron(COLOR_PAIR(pair)); |
| 136 | + printw(" %2d ", i); |
| 137 | + attroff(COLOR_PAIR(pair)); |
| 138 | + } |
| 139 | + if (COLORS > 8) { |
| 140 | + printw("\nHigh-intensity colors\n"); |
| 141 | + for (i = 8; i < 16; i++) { |
| 142 | + init_pair(++pair, i, 0); |
| 143 | + attron(COLOR_PAIR(pair)); |
| 144 | + printw(" %2d ", i); |
| 145 | + init_pair(++pair, 0, i); |
| 146 | + attron(COLOR_PAIR(pair)); |
| 147 | + printw(" %2d ", i); |
| 148 | + attroff(COLOR_PAIR(pair)); |
| 149 | + } |
| 150 | + if (COLORS > 16) { |
| 151 | + printw("\n216 colors\n"); |
| 152 | + for (i = 0; i < 6; i++) { |
| 153 | + int j; |
| 154 | + for (j = 0; j < 18; j++) { |
| 155 | + int bg = 16 + i * 36 + j; |
| 156 | + init_pair(++pair, 7, bg); |
| 157 | + attron(COLOR_PAIR(pair)); |
| 158 | + printw(" %3d", bg); |
| 159 | + attroff(COLOR_PAIR(pair)); |
| 160 | + } |
| 161 | + printw("\n"); |
| 162 | + } |
| 163 | + for (i = 0; i < 6; i++) { |
| 164 | + int j; |
| 165 | + for (j = 18; j < 36; j++) { |
| 166 | + int bg = 16 + i * 36 + j; |
| 167 | + init_pair(++pair, 0, bg); |
| 168 | + attron(COLOR_PAIR(pair)); |
| 169 | + printw(" %3d", bg); |
| 170 | + attroff(COLOR_PAIR(pair)); |
| 171 | + } |
| 172 | + printw("\n"); |
| 173 | + } |
| 174 | + printw("Grayscale colors\n"); |
| 175 | + for (i = 0; i < 24; i++) { |
| 176 | + int bg = 232 + i; |
| 177 | + int fg = i >= 12 ? 0 : 7; |
| 178 | + init_pair(++pair, fg, bg); |
| 179 | + attron(COLOR_PAIR(pair)); |
| 180 | + printw("%3d", bg); |
| 181 | + attroff(COLOR_PAIR(pair)); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + getch(); |
| 186 | + endwin(); |
| 187 | + use_default_colors(); |
| 188 | +} |
0 commit comments