2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27 #include "gntcolors.h"
35 static gboolean hascolors
;
36 static int custom_type
= GNT_COLORS
;
40 } colors
[GNT_TOTAL_COLORS
];
46 for (i
= 0; i
< GNT_TOTAL_COLORS
; i
++)
48 color_content(i
, &colors
[i
].r
,
49 &colors
[i
].g
, &colors
[i
].b
);
54 can_use_custom_color(void)
56 return (gnt_style_get_bool(GNT_STYLE_COLOR
, FALSE
) && can_change_color());
63 for (i
= 0; i
< GNT_TOTAL_COLORS
; i
++)
65 init_color(i
, colors
[i
].r
,
66 colors
[i
].g
, colors
[i
].b
);
70 void gnt_init_colors()
72 static gboolean init
= FALSE
;
80 if (!(hascolors
= has_colors()))
82 defaults
= use_default_colors();
84 if (can_use_custom_color())
88 /* Do some init_color()s */
89 init_color(GNT_COLOR_BLACK
, 0, 0, 0);
90 init_color(GNT_COLOR_RED
, 1000, 0, 0);
91 init_color(GNT_COLOR_GREEN
, 0, 1000, 0);
92 init_color(GNT_COLOR_BLUE
, 250, 250, 700);
93 init_color(GNT_COLOR_WHITE
, 1000, 1000, 1000);
94 init_color(GNT_COLOR_GRAY
, 699, 699, 699);
95 init_color(GNT_COLOR_DARK_GRAY
, 256, 256, 256);
97 /* Now some init_pair()s */
98 init_pair(GNT_COLOR_NORMAL
, GNT_COLOR_BLACK
, GNT_COLOR_WHITE
);
99 init_pair(GNT_COLOR_HIGHLIGHT
, GNT_COLOR_WHITE
, GNT_COLOR_BLUE
);
100 init_pair(GNT_COLOR_SHADOW
, GNT_COLOR_BLACK
, GNT_COLOR_DARK_GRAY
);
102 init_pair(GNT_COLOR_TITLE
, GNT_COLOR_WHITE
, GNT_COLOR_BLUE
);
103 init_pair(GNT_COLOR_TITLE_D
, GNT_COLOR_WHITE
, GNT_COLOR_GRAY
);
105 init_pair(GNT_COLOR_TEXT_NORMAL
, GNT_COLOR_WHITE
, GNT_COLOR_BLUE
);
106 init_pair(GNT_COLOR_HIGHLIGHT_D
, GNT_COLOR_BLACK
, GNT_COLOR_GRAY
);
107 init_pair(GNT_COLOR_DISABLED
, GNT_COLOR_GRAY
, GNT_COLOR_WHITE
);
108 init_pair(GNT_COLOR_URGENT
, GNT_COLOR_WHITE
, GNT_COLOR_RED
);
114 if (defaults
== OK
) {
115 init_pair(GNT_COLOR_NORMAL
, -1, -1);
118 init_pair(GNT_COLOR_NORMAL
, COLOR_BLACK
, COLOR_WHITE
);
121 init_pair(GNT_COLOR_DISABLED
, COLOR_YELLOW
, bg
);
122 init_pair(GNT_COLOR_URGENT
, COLOR_GREEN
, bg
);
124 init_pair(GNT_COLOR_HIGHLIGHT
, COLOR_WHITE
, COLOR_BLUE
);
125 init_pair(GNT_COLOR_SHADOW
, COLOR_BLACK
, COLOR_BLACK
);
126 init_pair(GNT_COLOR_TITLE
, COLOR_WHITE
, COLOR_BLUE
);
127 init_pair(GNT_COLOR_TITLE_D
, COLOR_WHITE
, COLOR_BLACK
);
128 init_pair(GNT_COLOR_TEXT_NORMAL
, COLOR_WHITE
, COLOR_BLUE
);
129 init_pair(GNT_COLOR_HIGHLIGHT_D
, COLOR_CYAN
, COLOR_BLACK
);
136 if (can_use_custom_color())
140 #if GLIB_CHECK_VERSION(2,6,0)
142 gnt_colors_get_color(char *key
)
145 gboolean custom
= can_use_custom_color();
147 key
= g_strstrip(key
);
149 if (strcmp(key
, "black") == 0)
150 color
= custom
? GNT_COLOR_BLACK
: COLOR_BLACK
;
151 else if (strcmp(key
, "red") == 0)
152 color
= custom
? GNT_COLOR_RED
: COLOR_RED
;
153 else if (strcmp(key
, "green") == 0)
154 color
= custom
? GNT_COLOR_GREEN
: COLOR_GREEN
;
155 else if (strcmp(key
, "blue") == 0)
156 color
= custom
? GNT_COLOR_BLUE
: COLOR_BLUE
;
157 else if (strcmp(key
, "white") == 0)
158 color
= custom
? GNT_COLOR_WHITE
: COLOR_WHITE
;
159 else if (strcmp(key
, "gray") == 0)
160 color
= custom
? GNT_COLOR_GRAY
: COLOR_YELLOW
; /* eh? */
161 else if (strcmp(key
, "darkgray") == 0)
162 color
= custom
? GNT_COLOR_DARK_GRAY
: COLOR_BLACK
;
163 else if (strcmp(key
, "magenta") == 0)
164 color
= COLOR_MAGENTA
;
165 else if (strcmp(key
, "cyan") == 0)
167 else if (strcmp(key
, "default") == 0)
170 g_warning("Invalid color name: %s\n", key
);
176 void gnt_colors_parse(GKeyFile
*kfile
)
178 GError
*error
= NULL
;
180 char **keys
= g_key_file_get_keys(kfile
, "colors", &nkeys
, &error
);
184 g_printerr("GntColors: %s\n", error
->message
);
194 gchar
*key
= keys
[nkeys
];
195 char **list
= g_key_file_get_string_list(kfile
, "colors", key
, &len
, NULL
);
198 int r
= atoi(list
[0]);
199 int g
= atoi(list
[1]);
200 int b
= atoi(list
[2]);
203 key
= g_ascii_strdown(key
, -1);
204 color
= gnt_colors_get_color(key
);
209 init_color(color
, r
, g
, b
);
217 gnt_color_pairs_parse(kfile
);
220 void gnt_color_pairs_parse(GKeyFile
*kfile
)
222 GError
*error
= NULL
;
224 char **keys
= g_key_file_get_keys(kfile
, "colorpairs", &nkeys
, &error
);
228 g_printerr("GntColors: %s\n", error
->message
);
238 gchar
*key
= keys
[nkeys
];
239 char **list
= g_key_file_get_string_list(kfile
, "colorpairs", key
, &len
, NULL
);
242 GntColorType type
= 0;
243 gchar
*fgc
= g_ascii_strdown(list
[0], -1);
244 gchar
*bgc
= g_ascii_strdown(list
[1], -1);
245 int fg
= gnt_colors_get_color(fgc
);
246 int bg
= gnt_colors_get_color(bgc
);
249 if (fg
== -1 || bg
== -1)
252 key
= g_ascii_strdown(key
, -1);
254 if (strcmp(key
, "normal") == 0)
255 type
= GNT_COLOR_NORMAL
;
256 else if (strcmp(key
, "highlight") == 0)
257 type
= GNT_COLOR_HIGHLIGHT
;
258 else if (strcmp(key
, "highlightd") == 0)
259 type
= GNT_COLOR_HIGHLIGHT_D
;
260 else if (strcmp(key
, "shadow") == 0)
261 type
= GNT_COLOR_SHADOW
;
262 else if (strcmp(key
, "title") == 0)
263 type
= GNT_COLOR_TITLE
;
264 else if (strcmp(key
, "titled") == 0)
265 type
= GNT_COLOR_TITLE_D
;
266 else if (strcmp(key
, "text") == 0)
267 type
= GNT_COLOR_TEXT_NORMAL
;
268 else if (strcmp(key
, "disabled") == 0)
269 type
= GNT_COLOR_DISABLED
;
270 else if (strcmp(key
, "urgent") == 0)
271 type
= GNT_COLOR_URGENT
;
278 init_pair(type
, fg
, bg
);
286 #endif /* GKeyFile */
288 int gnt_color_pair(int pair
)
290 return (hascolors
? COLOR_PAIR(pair
) :
291 ((pair
== GNT_COLOR_NORMAL
|| pair
== GNT_COLOR_HIGHLIGHT_D
||
292 pair
== GNT_COLOR_TITLE_D
|| pair
== GNT_COLOR_DISABLED
) ? 0 : A_STANDOUT
));
295 int gnt_color_add_pair(int fg
, int bg
)
297 init_pair(custom_type
, fg
, bg
);
298 return custom_type
++;