start cleaning up color parsing

This commit is contained in:
Daniel Micay 2012-11-14 20:48:40 -05:00
parent ed4a3b09c9
commit a3c554e563
2 changed files with 52 additions and 53 deletions

View File

@ -1,4 +1,5 @@
#include <algorithm> #include <algorithm>
#include <array>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <functional> #include <functional>
@ -897,82 +898,80 @@ auto get_config_string(std::bind(get_config<char *>, g_key_file_get_string,
auto get_config_double(std::bind(get_config<double>, g_key_file_get_double, auto get_config_double(std::bind(get_config<double>, g_key_file_get_double,
_1, _2, _3)); _1, _2, _3));
static bool get_config_color(GKeyFile *config, const char *section, const char *key, GdkColor *color) { static maybe<GdkColor> get_config_color(GKeyFile *config, const char *section, const char *key) {
bool success = false;
if (auto s = get_config_string(config, section, key)) { if (auto s = get_config_string(config, section, key)) {
if (gdk_color_parse(*s, color)) { GdkColor color;
success = true; if (gdk_color_parse(*s, &color)) {
} else { g_free(*s);
g_printerr("invalid color string: %s\n", *s); return color;
} }
g_printerr("invalid color string: %s\n", *s);
g_free(*s); g_free(*s);
} }
return success; return {};
} }
static maybe<cairo_pattern_t *> static maybe<cairo_pattern_t *>
get_config_cairo_color(GKeyFile *config, const char *group, const char *key) { get_config_cairo_color(GKeyFile *config, const char *group, const char *key) {
GdkColor color; if (auto color = get_config_color(config, group, key)) {
if (get_config_color(config, group, key, &color)) { return cairo_pattern_create_rgb(color->red / 65535.0,
return cairo_pattern_create_rgb(color.red / 65535.0, color->green / 65535.0,
color.green / 65535.0, color->blue / 65535.0);
color.blue / 65535.0);
} }
return {}; return {};
} }
static void load_theme(VteTerminal *vte, GKeyFile *config) { static void load_theme(VteTerminal *vte, GKeyFile *config) {
const long palette_size = 255; std::array<GdkColor, 255> palette;
GdkColor color, palette[palette_size];
char color_key[] = "color000"; char color_key[] = "color000";
for (unsigned i = 0; i < palette_size; i++) { for (unsigned i = 0; i < palette.size(); i++) {
snprintf(color_key, sizeof color_key, "color%u", i); snprintf(color_key, sizeof color_key, "color%u", i);
if (!get_config_color(config, "colors", color_key, &palette[i])) { if (auto color = get_config_color(config, "colors", color_key)) {
if (i < 16) { palette[i] = *color;
palette[i].blue = (i & 4) ? 0xc000 : 0; } else if (i < 16) {
palette[i].green = (i & 2) ? 0xc000 : 0; palette[i].blue = (i & 4) ? 0xc000 : 0;
palette[i].red = (i & 1) ? 0xc000 : 0; palette[i].green = (i & 2) ? 0xc000 : 0;
if (i > 7) { palette[i].red = (i & 1) ? 0xc000 : 0;
palette[i].blue = (guint16)(palette[i].blue + 0x3fff); if (i > 7) {
palette[i].green = (guint16)(palette[i].green + 0x3fff); palette[i].blue = (guint16)(palette[i].blue + 0x3fff);
palette[i].red = (guint16)(palette[i].red + 0x3fff); palette[i].green = (guint16)(palette[i].green + 0x3fff);
} palette[i].red = (guint16)(palette[i].red + 0x3fff);
} else if (i < 232) {
const unsigned j = i - 16;
const unsigned r = j / 36, g = (j / 6) % 6, b = j % 6;
const unsigned red = (r == 0) ? 0 : r * 40 + 55;
const unsigned green = (g == 0) ? 0 : g * 40 + 55;
const unsigned blue = (b == 0) ? 0 : b * 40 + 55;
palette[i].red = (guint16)(red | red << 8);
palette[i].green = (guint16)(green | green << 8);
palette[i].blue = (guint16)(blue | blue << 8);
} else if (i < 256) {
const unsigned shade = 8 + (i - 232) * 10;
palette[i].red = palette[i].green = palette[i].blue = (guint16)(shade | shade << 8);
} }
} else if (i < 232) {
const unsigned j = i - 16;
const unsigned r = j / 36, g = (j / 6) % 6, b = j % 6;
const unsigned red = (r == 0) ? 0 : r * 40 + 55;
const unsigned green = (g == 0) ? 0 : g * 40 + 55;
const unsigned blue = (b == 0) ? 0 : b * 40 + 55;
palette[i].red = (guint16)(red | red << 8);
palette[i].green = (guint16)(green | green << 8);
palette[i].blue = (guint16)(blue | blue << 8);
} else if (i < 256) {
const unsigned shade = 8 + (i - 232) * 10;
palette[i].red = palette[i].green = palette[i].blue = (guint16)(shade | shade << 8);
} }
} }
vte_terminal_set_colors(vte, nullptr, nullptr, palette, palette_size);
if (get_config_color(config, "colors", "foreground", &color)) { vte_terminal_set_colors(vte, nullptr, nullptr, palette.data(), palette.size());
vte_terminal_set_color_foreground(vte, &color); if (auto color = get_config_color(config, "colors", "foreground")) {
vte_terminal_set_color_foreground(vte, &*color);
} }
if (get_config_color(config, "colors", "foreground_bold", &color)) { if (auto color = get_config_color(config, "colors", "foreground_bold")) {
vte_terminal_set_color_bold(vte, &color); vte_terminal_set_color_bold(vte, &*color);
} }
if (get_config_color(config, "colors", "foreground_dim", &color)) { if (auto color = get_config_color(config, "colors", "foreground_dim")) {
vte_terminal_set_color_dim(vte, &color); vte_terminal_set_color_dim(vte, &*color);
} }
if (get_config_color(config, "colors", "background", &color)) { if (auto color = get_config_color(config, "colors", "background")) {
vte_terminal_set_color_background(vte, &color); vte_terminal_set_color_background(vte, &*color);
vte_terminal_set_background_tint_color(vte, &color); vte_terminal_set_background_tint_color(vte, &*color);
} }
if (get_config_color(config, "colors", "cursor", &color)) { if (auto color = get_config_color(config, "colors", "cursor")) {
vte_terminal_set_color_cursor(vte, &color); vte_terminal_set_color_cursor(vte, &*color);
} }
if (get_config_color(config, "colors", "highlight", &color)) { if (auto color = get_config_color(config, "colors", "highlight")) {
vte_terminal_set_color_highlight(vte, &color); vte_terminal_set_color_highlight(vte, &*color);
} }
if (auto s = get_config_string(config, "hints", "font")) { if (auto s = get_config_string(config, "hints", "font")) {

2
util

@ -1 +1 @@
Subproject commit d7ca0bd7dbe371e86fcd53bbce157d9454ce1574 Subproject commit 2a1badeeec68c0cd22ec111cfd376883100e6f43