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 "gntinternal.h"
29 #define GNT_LOG_DOMAIN "Colors"
31 #include "gntcolors.h"
40 static gboolean hascolors
;
41 static int custom_type
= GNT_COLORS
;
45 } colors
[GNT_TOTAL_COLORS
];
51 for (i
= 0; i
< GNT_TOTAL_COLORS
; i
++)
53 color_content(i
, &colors
[i
].r
,
54 &colors
[i
].g
, &colors
[i
].b
);
59 can_use_custom_color(void)
61 return (gnt_style_get_bool(GNT_STYLE_COLOR
, FALSE
) && can_change_color());
68 for (i
= 0; i
< GNT_TOTAL_COLORS
; i
++)
70 init_color(i
, colors
[i
].r
,
71 colors
[i
].g
, colors
[i
].b
);
75 void gnt_init_colors()
77 static gboolean init
= FALSE
;
85 if (!(hascolors
= has_colors()))
87 defaults
= use_default_colors();
89 if (can_use_custom_color())
93 /* Do some init_color()s */
94 init_color(GNT_COLOR_BLACK
, 0, 0, 0);
95 init_color(GNT_COLOR_RED
, 1000, 0, 0);
96 init_color(GNT_COLOR_GREEN
, 0, 1000, 0);
97 init_color(GNT_COLOR_BLUE
, 250, 250, 700);
98 init_color(GNT_COLOR_WHITE
, 1000, 1000, 1000);
99 init_color(GNT_COLOR_GRAY
, 699, 699, 699);
100 init_color(GNT_COLOR_DARK_GRAY
, 256, 256, 256);
102 /* Now some init_pair()s */
103 init_pair(GNT_COLOR_NORMAL
, GNT_COLOR_BLACK
, GNT_COLOR_WHITE
);
104 init_pair(GNT_COLOR_HIGHLIGHT
, GNT_COLOR_WHITE
, GNT_COLOR_BLUE
);
105 init_pair(GNT_COLOR_SHADOW
, GNT_COLOR_BLACK
, GNT_COLOR_DARK_GRAY
);
107 init_pair(GNT_COLOR_TITLE
, GNT_COLOR_WHITE
, GNT_COLOR_BLUE
);
108 init_pair(GNT_COLOR_TITLE_D
, GNT_COLOR_WHITE
, GNT_COLOR_GRAY
);
110 init_pair(GNT_COLOR_TEXT_NORMAL
, GNT_COLOR_WHITE
, GNT_COLOR_BLUE
);
111 init_pair(GNT_COLOR_HIGHLIGHT_D
, GNT_COLOR_BLACK
, GNT_COLOR_GRAY
);
112 init_pair(GNT_COLOR_DISABLED
, GNT_COLOR_GRAY
, GNT_COLOR_WHITE
);
113 init_pair(GNT_COLOR_URGENT
, GNT_COLOR_WHITE
, GNT_COLOR_RED
);
119 if (defaults
== OK
) {
120 init_pair(GNT_COLOR_NORMAL
, -1, -1);
123 init_pair(GNT_COLOR_NORMAL
, COLOR_BLACK
, COLOR_WHITE
);
126 init_pair(GNT_COLOR_DISABLED
, COLOR_YELLOW
, bg
);
127 init_pair(GNT_COLOR_URGENT
, COLOR_GREEN
, bg
);
129 init_pair(GNT_COLOR_HIGHLIGHT
, COLOR_WHITE
, COLOR_BLUE
);
130 init_pair(GNT_COLOR_SHADOW
, COLOR_BLACK
, COLOR_BLACK
);
131 init_pair(GNT_COLOR_TITLE
, COLOR_WHITE
, COLOR_BLUE
);
132 init_pair(GNT_COLOR_TITLE_D
, COLOR_WHITE
, COLOR_BLACK
);
133 init_pair(GNT_COLOR_TEXT_NORMAL
, COLOR_WHITE
, COLOR_BLUE
);
134 init_pair(GNT_COLOR_HIGHLIGHT_D
, COLOR_CYAN
, COLOR_BLACK
);
141 if (can_use_custom_color())
145 #if GLIB_CHECK_VERSION(2,6,0)
147 gnt_colors_get_color(char *key
)
150 gboolean custom
= can_use_custom_color();
152 key
= g_strstrip(key
);
154 if (strcmp(key
, "black") == 0)
155 color
= custom
? GNT_COLOR_BLACK
: COLOR_BLACK
;
156 else if (strcmp(key
, "red") == 0)
157 color
= custom
? GNT_COLOR_RED
: COLOR_RED
;
158 else if (strcmp(key
, "green") == 0)
159 color
= custom
? GNT_COLOR_GREEN
: COLOR_GREEN
;
160 else if (strcmp(key
, "blue") == 0)
161 color
= custom
? GNT_COLOR_BLUE
: COLOR_BLUE
;
162 else if (strcmp(key
, "white") == 0)
163 color
= custom
? GNT_COLOR_WHITE
: COLOR_WHITE
;
164 else if (strcmp(key
, "gray") == 0)
165 color
= custom
? GNT_COLOR_GRAY
: COLOR_YELLOW
; /* eh? */
166 else if (strcmp(key
, "darkgray") == 0)
167 color
= custom
? GNT_COLOR_DARK_GRAY
: COLOR_BLACK
;
168 else if (strcmp(key
, "magenta") == 0)
169 color
= COLOR_MAGENTA
;
170 else if (strcmp(key
, "cyan") == 0)
172 else if (strcmp(key
, "default") == 0)
175 g_warning("Invalid color name: %s\n", key
);
181 void gnt_colors_parse(GKeyFile
*kfile
)
183 GError
*error
= NULL
;
185 char **keys
= g_key_file_get_keys(kfile
, "colors", &nkeys
, &error
);
189 gnt_warning("%s", error
->message
);
199 gchar
*key
= keys
[nkeys
];
200 char **list
= g_key_file_get_string_list(kfile
, "colors", key
, &len
, NULL
);
203 int r
= atoi(list
[0]);
204 int g
= atoi(list
[1]);
205 int b
= atoi(list
[2]);
208 key
= g_ascii_strdown(key
, -1);
209 color
= gnt_colors_get_color(key
);
211 if (color
== -EINVAL
) {
216 init_color(color
, r
, g
, b
);
224 gnt_color_pairs_parse(kfile
);
227 void gnt_color_pairs_parse(GKeyFile
*kfile
)
229 GError
*error
= NULL
;
231 char **keys
= g_key_file_get_keys(kfile
, "colorpairs", &nkeys
, &error
);
235 gnt_warning("%s", error
->message
);
245 gchar
*key
= keys
[nkeys
];
246 char **list
= g_key_file_get_string_list(kfile
, "colorpairs", key
, &len
, NULL
);
249 GntColorType type
= 0;
250 gchar
*fgc
= g_ascii_strdown(list
[0], -1);
251 gchar
*bgc
= g_ascii_strdown(list
[1], -1);
252 int fg
= gnt_colors_get_color(fgc
);
253 int bg
= gnt_colors_get_color(bgc
);
256 if (fg
== -EINVAL
|| bg
== -EINVAL
) {
261 key
= g_ascii_strdown(key
, -1);
263 if (strcmp(key
, "normal") == 0)
264 type
= GNT_COLOR_NORMAL
;
265 else if (strcmp(key
, "highlight") == 0)
266 type
= GNT_COLOR_HIGHLIGHT
;
267 else if (strcmp(key
, "highlightd") == 0)
268 type
= GNT_COLOR_HIGHLIGHT_D
;
269 else if (strcmp(key
, "shadow") == 0)
270 type
= GNT_COLOR_SHADOW
;
271 else if (strcmp(key
, "title") == 0)
272 type
= GNT_COLOR_TITLE
;
273 else if (strcmp(key
, "titled") == 0)
274 type
= GNT_COLOR_TITLE_D
;
275 else if (strcmp(key
, "text") == 0)
276 type
= GNT_COLOR_TEXT_NORMAL
;
277 else if (strcmp(key
, "disabled") == 0)
278 type
= GNT_COLOR_DISABLED
;
279 else if (strcmp(key
, "urgent") == 0)
280 type
= GNT_COLOR_URGENT
;
288 init_pair(type
, fg
, bg
);
296 #endif /* GKeyFile */
298 int gnt_color_pair(int pair
)
300 return (hascolors
? COLOR_PAIR(pair
) :
301 ((pair
== GNT_COLOR_NORMAL
|| pair
== GNT_COLOR_HIGHLIGHT_D
||
302 pair
== GNT_COLOR_TITLE_D
|| pair
== GNT_COLOR_DISABLED
) ? 0 : A_STANDOUT
));
305 int gnt_color_add_pair(int fg
, int bg
)
307 init_pair(custom_type
, fg
, bg
);
308 return custom_type
++;