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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#include "config.h"
#include <ncurses.h>
#include "gntcolors.h"
#include "gntstyle.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
static struct
{
short r, g, b;
} colors[GNT_TOTAL_COLORS];
static void
backup_colors()
{
short i;
for (i = 0; i < GNT_TOTAL_COLORS; i++)
{
color_content(i, &colors[i].r,
&colors[i].g, &colors[i].b);
}
}
static gboolean
can_use_custom_color()
{
return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color());
}
static void
restore_colors()
{
short i;
for (i = 0; i < GNT_TOTAL_COLORS; i++)
{
init_color(i, colors[i].r,
colors[i].g, colors[i].b);
}
}
void gnt_init_colors()
{
static gboolean init = FALSE;
int defaults;
if (init)
return;
init = TRUE;
start_color();
defaults = use_default_colors();
if (can_use_custom_color())
{
backup_colors();
/* Do some init_color()s */
init_color(GNT_COLOR_BLACK, 0, 0, 0);
init_color(GNT_COLOR_RED, 1000, 0, 0);
init_color(GNT_COLOR_GREEN, 0, 1000, 0);
init_color(GNT_COLOR_BLUE, 250, 250, 700);
init_color(GNT_COLOR_WHITE, 1000, 1000, 1000);
init_color(GNT_COLOR_GRAY, 699, 699, 699);
init_color(GNT_COLOR_DARK_GRAY, 256, 256, 256);
/* Now some init_pair()s */
init_pair(GNT_COLOR_NORMAL, GNT_COLOR_BLACK, GNT_COLOR_WHITE);
init_pair(GNT_COLOR_HIGHLIGHT, GNT_COLOR_WHITE, GNT_COLOR_BLUE);
init_pair(GNT_COLOR_SHADOW, GNT_COLOR_BLACK, GNT_COLOR_DARK_GRAY);
init_pair(GNT_COLOR_TITLE, GNT_COLOR_WHITE, GNT_COLOR_BLUE);
init_pair(GNT_COLOR_TITLE_D, GNT_COLOR_WHITE, GNT_COLOR_GRAY);
init_pair(GNT_COLOR_TEXT_NORMAL, GNT_COLOR_WHITE, GNT_COLOR_BLUE);
init_pair(GNT_COLOR_HIGHLIGHT_D, GNT_COLOR_BLACK, GNT_COLOR_GRAY);
init_pair(GNT_COLOR_DISABLED, GNT_COLOR_GRAY, GNT_COLOR_WHITE);
init_pair(GNT_COLOR_URGENT, GNT_COLOR_WHITE, GNT_COLOR_RED);
}
else
{
int bg;
if (defaults == OK) {
init_pair(GNT_COLOR_NORMAL, -1, -1);
bg = -1;
} else {
init_pair(GNT_COLOR_NORMAL, COLOR_BLACK, COLOR_WHITE);
bg = COLOR_WHITE;
}
init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg);
init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg);
init_pair(GNT_COLOR_HIGHLIGHT, COLOR_WHITE, COLOR_BLUE);
init_pair(GNT_COLOR_SHADOW, COLOR_BLACK, COLOR_BLACK);
init_pair(GNT_COLOR_TITLE, COLOR_WHITE, COLOR_BLUE);
init_pair(GNT_COLOR_TITLE_D, COLOR_WHITE, COLOR_BLACK);
init_pair(GNT_COLOR_TEXT_NORMAL, COLOR_WHITE, COLOR_BLUE);
init_pair(GNT_COLOR_HIGHLIGHT_D, COLOR_CYAN, COLOR_BLACK);
}
}
void
gnt_uninit_colors()
{
if (can_use_custom_color())
restore_colors();
}
static int
get_color(char *key)
{
int color;
gboolean custom = can_use_custom_color();
key = g_strstrip(key);
if (strcmp(key, "black") == 0)
color = custom ? GNT_COLOR_BLACK : COLOR_BLACK;
else if (strcmp(key, "red") == 0)
color = custom ? GNT_COLOR_RED : COLOR_RED;
else if (strcmp(key, "green") == 0)
color = custom ? GNT_COLOR_GREEN : COLOR_GREEN;
else if (strcmp(key, "blue") == 0)
color = custom ? GNT_COLOR_BLUE : COLOR_BLUE;
else if (strcmp(key, "white") == 0)
color = custom ? GNT_COLOR_WHITE : COLOR_WHITE;
else if (strcmp(key, "gray") == 0)
color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */
else if (strcmp(key, "darkgray") == 0)
color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK;
else if (strcmp(key, "magenta") == 0)
color = COLOR_MAGENTA;
else if (strcmp(key, "cyan") == 0)
color = COLOR_CYAN;
else
color = -1;
return color;
}
#if GLIB_CHECK_VERSION(2,6,0)
void gnt_colors_parse(GKeyFile *kfile)
{
GError *error = NULL;
gsize nkeys;
char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error);
if (error)
{
g_printerr("GntColors: %s\n", error->message);
g_error_free(error);
error = NULL;
}
else if (nkeys)
{
gnt_init_colors();
while (nkeys--)
{
gsize len;
char *key = keys[nkeys];
char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL);
if (len == 3)
{
int r = atoi(list[0]);
int g = atoi(list[1]);
int b = atoi(list[2]);
int color = -1;
g_ascii_strdown(key, -1);
color = get_color(key);
if (color == -1)
continue;
init_color(color, r, g, b);
}
g_strfreev(list);
}
g_strfreev(keys);
}
gnt_color_pairs_parse(kfile);
}
void gnt_color_pairs_parse(GKeyFile *kfile)
{
GError *error = NULL;
gsize nkeys;
char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error);
if (error)
{
g_printerr("GntColors: %s\n", error->message);
g_error_free(error);
return;
}
else if (nkeys)
gnt_init_colors();
while (nkeys--)
{
gsize len;
char *key = keys[nkeys];
char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL);
if (len == 2)
{
GntColorType type = 0;
int fg = get_color(g_ascii_strdown(list[0], -1));
int bg = get_color(g_ascii_strdown(list[1], -1));
if (fg == -1 || bg == -1)
continue;
g_ascii_strdown(key, -1);
if (strcmp(key, "normal") == 0)
type = GNT_COLOR_NORMAL;
else if (strcmp(key, "highlight") == 0)
type = GNT_COLOR_HIGHLIGHT;
else if (strcmp(key, "highlightd") == 0)
type = GNT_COLOR_HIGHLIGHT_D;
else if (strcmp(key, "shadow") == 0)
type = GNT_COLOR_SHADOW;
else if (strcmp(key, "title") == 0)
type = GNT_COLOR_TITLE;
else if (strcmp(key, "titled") == 0)
type = GNT_COLOR_TITLE_D;
else if (strcmp(key, "text") == 0)
type = GNT_COLOR_TEXT_NORMAL;
else if (strcmp(key, "disabled") == 0)
type = GNT_COLOR_DISABLED;
else if (strcmp(key, "urgent") == 0)
type = GNT_COLOR_URGENT;
else
continue;
init_pair(type, fg, bg);
}
g_strfreev(list);
}
g_strfreev(keys);
}
#endif /* GKeyFile */
|