From e7e8e913b51c99e19b7f47ed680463fbbc8beee6 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 27 Sep 2016 19:16:07 -0700 Subject: [PATCH] color-cycle: add color generator helper We need a simple helper to choose colors for visualizers so they do not overlap. This is just a simple cycle using some predefined colors. We should come up with something more unique in the future or possibly do this with CSS and :nth-child(n) once we have access to CSS nodes. --- lib/Makefile.am | 2 + lib/sp-color-cycle.c | 122 +++++++++++++++++++++++++++++++++++++++++++ lib/sp-color-cycle.h | 39 ++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 lib/sp-color-cycle.c create mode 100644 lib/sp-color-cycle.h diff --git a/lib/Makefile.am b/lib/Makefile.am index acd604d6..ac7d1c99 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -159,6 +159,8 @@ libsysprof_ui_@API_VERSION@_la_SOURCES = \ $(uiheaders_DATA) \ sp-callgraph-view.c \ sp-cell-renderer-percent.c \ + sp-color-cycle.c \ + sp-color-cycle.h \ sp-cpu-visualizer-row.c \ sp-empty-state-view.c \ sp-failed-state-view.c \ diff --git a/lib/sp-color-cycle.c b/lib/sp-color-cycle.c new file mode 100644 index 00000000..4c71fa07 --- /dev/null +++ b/lib/sp-color-cycle.c @@ -0,0 +1,122 @@ +/* sp-color-cycle.c + * + * Copyright (C) 2016 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define G_LOG_DOMAIN "sp-color-cycle" + +#include "sp-color-cycle.h" + +G_DEFINE_BOXED_TYPE (SpColorCycle, sp_color_cycle, sp_color_cycle_ref, sp_color_cycle_unref) + +static const gchar *default_colors[] = { + "#73d216", + "#f57900", + "#3465a4", + "#ef2929", + "#75507b", + "#ce5c00", + "#c17d11", + "#cc0000", + "#edd400", + "#555753", + "#4e9a06", + "#204a87", + "#5c3566", + "#a40000", + "#c4a000", + "#8f5902", + "#2e3436", + "#8ae234", + "#729fcf", + "#ad7fa8", + "#fce94f", + "#fcaf3e", + "#e9b96e", + "#888a85", + NULL +}; + +struct _SpColorCycle +{ + volatile gint ref_count; + GdkRGBA *colors; + gsize n_colors; + guint position; +}; + +static void +sp_color_cycle_destroy (SpColorCycle *self) +{ + g_free (self->colors); + g_slice_free (SpColorCycle, self); +} + +SpColorCycle * +sp_color_cycle_new (void) +{ + SpColorCycle *self; + + self = g_slice_new0 (SpColorCycle); + self->ref_count = 1; + self->n_colors = g_strv_length ((gchar **)default_colors); + self->colors = g_new0 (GdkRGBA, self->n_colors); + + for (guint i = 0; default_colors[i]; i++) + { + if G_UNLIKELY (!gdk_rgba_parse (&self->colors[i], default_colors[i])) + g_warning ("Failed to parse color %s into an RGBA", default_colors[i]); + } + + return self; +} + +SpColorCycle * +sp_color_cycle_ref (SpColorCycle *self) +{ + g_return_val_if_fail (self != NULL, NULL); + g_return_val_if_fail (self->ref_count > 0, NULL); + g_atomic_int_inc (&self->ref_count); + return self; +} + +void +sp_color_cycle_unref (SpColorCycle *self) +{ + g_return_if_fail (self != NULL); + g_return_if_fail (self->ref_count > 0); + if (g_atomic_int_dec_and_test (&self->ref_count)) + sp_color_cycle_destroy (self); +} + +void +sp_color_cycle_next (SpColorCycle *self, + GdkRGBA *rgba) +{ + g_return_if_fail (self != NULL); + g_return_if_fail (self->position < self->n_colors); + + *rgba = self->colors[self->position]; + + /* + * TODO: Adjust color HSV/etc + * + * We could simply adjust the brightness/etc after we dispatch + * a color so that we get darker as we go. + */ + + self->position = (self->position + 1) % self->n_colors; +} diff --git a/lib/sp-color-cycle.h b/lib/sp-color-cycle.h new file mode 100644 index 00000000..bbac504f --- /dev/null +++ b/lib/sp-color-cycle.h @@ -0,0 +1,39 @@ +/* sp-color-cycle.h + * + * Copyright (C) 2016 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef SP_COLOR_CYCLE_H +#define SP_COLOR_CYCLE_H + +#include + +G_BEGIN_DECLS + +#define SP_TYPE_COLOR_CYCLE (sp_color_cycle_get_type()) + +typedef struct _SpColorCycle SpColorCycle; + +GType sp_color_cycle_get_type (void); +SpColorCycle *sp_color_cycle_ref (SpColorCycle *self); +void sp_color_cycle_unref (SpColorCycle *self); +SpColorCycle *sp_color_cycle_new (void); +void sp_color_cycle_next (SpColorCycle *self, + GdkRGBA *rgba); + +G_END_DECLS + +#endif /* SP_COLOR_CYCLE_H */