static void test_parse_text (void) { GPtrArray *items; static const gchar *text = "line-background=\"rgba(235,202,210,.4)\"\n" "foreground=\"rgba(100%, 50%, 25%,.4)\"\n" "color: #8d9091;\n" "color: #123;\n" "background-color: hsl(65, 70%, 72%);\n" "text-shadow: 0 1px black;"; printf ("\n"); items = gstyle_color_parse (text); for (gint i = 0; i < items->len; ++i) { gchar *str; const GstyleColor *color; GstyleColorItem *item; guint start, len; item = g_ptr_array_index (items, i); color = gstyle_color_item_get_color (item); start = gstyle_color_item_get_start (item); len = gstyle_color_item_get_len (item); str = gstyle_color_to_string ((GstyleColor *)color, GSTYLE_COLOR_KIND_ORIGINAL); printf ("item(%i,%i): '%s'\n", start, len, str); g_free (str); } g_ptr_array_free (items, TRUE); }
static void update_label_visibility (GstyleColorWidget *self) { const gchar *color_name; g_autofree gchar *fallback_name = NULL; g_assert (GSTYLE_IS_COLOR_WIDGET (self)); if (self->color == NULL) { if (gtk_widget_is_visible (GTK_WIDGET (self->label))) gtk_widget_hide (GTK_WIDGET (self->label)); return; } if (self->is_name_visible) { if (self->filter_func != NULL && GSTYLE_IS_COLOR (self->filtered_color)) color_name = gstyle_color_get_name (self->filtered_color); else color_name = gstyle_color_get_name (self->color); if (color_name != NULL) { gtk_label_set_text (self->label, color_name); if (!gtk_widget_is_visible (GTK_WIDGET (self->label))) gtk_widget_show (GTK_WIDGET (self->label)); return; } } if (self->is_fallback_name_visible) { if (self->filter_func != NULL && GSTYLE_IS_COLOR (self->filtered_color)) fallback_name = gstyle_color_to_string (self->filtered_color, self->fallback_name_kind); else fallback_name = gstyle_color_to_string (self->color, self->fallback_name_kind); gtk_label_set_text (self->label, fallback_name); if (!gtk_widget_is_visible (GTK_WIDGET (self->label))) gtk_widget_show (GTK_WIDGET (self->label)); } else gtk_widget_hide (GTK_WIDGET (self->label)); }
static void gstyle_color_widget_on_drag_data_get (GtkWidget *widget, GdkDragContext *context, GtkSelectionData *data, guint info, guint time) { GstyleColorWidget *self = (GstyleColorWidget *)widget; GdkAtom target = gtk_selection_data_get_target (data); GstyleColor *color; guint16 data_rgba[4]; GdkRGBA rgba; g_assert (GSTYLE_IS_COLOR_WIDGET (self)); g_assert (GDK_IS_DRAG_CONTEXT (context)); if (self->filter_func != NULL && GSTYLE_IS_COLOR (self->filtered_color)) color = self->filtered_color; else color = self->color; if (target == gdk_atom_intern_static_string ("GSTYLE_COLOR_WIDGET")) gtk_selection_data_set (data, target, 8, (void*)&color, sizeof (gpointer)); else if (target == gdk_atom_intern_static_string ("application/x-color")) { gstyle_color_fill_rgba (color, &rgba); data_rgba[0] = (guint16) (rgba.red * 65535); data_rgba[1] = (guint16) (rgba.green * 65535); data_rgba[2] = (guint16) (rgba.blue * 65535); data_rgba[3] = (guint16) (rgba.alpha * 65535); gtk_selection_data_set (data, target, 16, (void*)&data_rgba, 8); } else if (gtk_targets_include_text (&target, 1)) { g_autofree gchar *name = NULL; name = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_ORIGINAL); if (name == NULL) name = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_RGB_HEX6); gtk_selection_data_set_text (data, name, -1); } }
static void test_parse_string (void) { ColorItem *item; GstyleColor *color; GdkRGBA rgba; GstyleColorKind kind; GstyleCielab lab; printf ("\n"); for (item = rgba_table; item->rgb != NULL; item++) { g_autofree gchar *str_hex3 = NULL; g_autofree gchar *str_hex6 = NULL; g_autofree gchar *str_rgba = NULL; g_autofree gchar *str_rgba_percent = NULL; g_autofree gchar *str_hsla = NULL; g_autofree gchar *str_original = NULL; g_autofree gchar *dst_str = NULL; color = gstyle_color_new_from_string (NULL, item->rgb); str_hex3 = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_RGB_HEX3); str_hex6 = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_RGB_HEX6); str_rgba = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_RGBA); str_rgba_percent = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_RGBA_PERCENT); str_hsla = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_HSLA); str_original = gstyle_color_to_string (color, GSTYLE_COLOR_KIND_ORIGINAL); gstyle_color_fill_rgba (color, &rgba); gstyle_color_convert_rgb_to_cielab (&rgba, &lab); kind = gstyle_color_get_kind (color); dst_str = gstyle_color_to_string (color, kind); printf ("dst:'%s'\n", dst_str); printf ("\n----- '%s': rgba: kind:%i\n%s\n%s\n%s\n%s\n%s\n Original: %s\n", item->rgb, kind, str_hex3, str_hex6, str_rgba, str_rgba_percent, str_hsla, str_original); printf ("lab : L=%.3f a=%.3f b=%.3f\n", lab.l, lab.a, lab.b); g_assert (g_ascii_strcasecmp (item->rgb, dst_str) == 0); g_object_unref (color); printf ("\n"); } }
/** * gstyle_palette_save_to_xml: * @self: a #GstylePalette * @file: a #GFile * @error: (nullable): a #GError location or %NULL * * Save a palette to Gnome-Builder .xml palette format. * * Returns: %TRUE if succesfull, %FALSE otherwise. */ gboolean gstyle_palette_save_to_xml (GstylePalette *self, GFile *file, GError **error) { g_autofree gchar *file_path = NULL; const gchar *id; const gchar *name; xmlDocPtr doc; xmlNodePtr root_node; xmlNodePtr palette_node; xmlNodePtr color_node; gint n_colors; gint succes; gchar *header = "Copyright (C) 2016 GNOME Builder Team at irc.gimp.net/#gnome-builder\n" \ "This program is free software: you can redistribute it and/or modify\n" \ "it under the terms of the GNU General Public License as published by\n" \ "the Free Software Foundation, either version 3 of the License, or\n" \ "(at your option) any later version.\n\n" \ "This program is distributed in the hope that it will be useful,\n" \ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \ "GNU General Public License for more details.\n\n" \ "You should have received a copy of the GNU General Public License\n" \ "along with this program. If not, see <http://www.gnu.org/licenses/>\n"; g_return_val_if_fail (GSTYLE_IS_PALETTE (self), FALSE); g_return_val_if_fail (G_IS_FILE (file), FALSE); doc = xmlNewDoc(CHAR_TO_XML ("1.0")); root_node = xmlNewDocComment (doc, CHAR_TO_XML (header)); xmlDocSetRootElement(doc, root_node); palette_node = xmlNewNode (NULL, CHAR_TO_XML ("palette")); xmlAddSibling (root_node, palette_node); id = gstyle_palette_get_id (self); name = gstyle_palette_get_name (self); xmlNewProp (palette_node, CHAR_TO_XML ("id"), CHAR_TO_XML (id)); if (self->gettext_domain) { xmlNewProp (palette_node, CHAR_TO_XML ("_name"), CHAR_TO_XML (name)); xmlNewProp (palette_node, CHAR_TO_XML ("gettext-domain"), CHAR_TO_XML (self->gettext_domain)); } else xmlNewProp (palette_node, CHAR_TO_XML ("name"), CHAR_TO_XML (name)); n_colors = gstyle_palette_get_len (self); for (gint i = 0; i < n_colors; ++i) { const GstyleColor *color; const gchar *color_name; g_autofree gchar *color_string = NULL; color = gstyle_palette_get_color_at_index (self, i); color_name = gstyle_color_get_name ((GstyleColor *)color); if (gstyle_color_get_kind ((GstyleColor *)color) == GSTYLE_COLOR_KIND_PREDEFINED) color_string = gstyle_color_to_string ((GstyleColor *)color, GSTYLE_COLOR_KIND_RGB_HEX6); else color_string = gstyle_color_to_string ((GstyleColor *)color, GSTYLE_COLOR_KIND_ORIGINAL); color_node = xmlNewChild (palette_node, NULL, CHAR_TO_XML ("color"), NULL); xmlNewProp (color_node, CHAR_TO_XML ("name"), CHAR_TO_XML (color_name)); xmlNewProp (color_node, CHAR_TO_XML ("value"), CHAR_TO_XML (color_string)); } file_path = g_file_get_path (file); succes = xmlSaveFormatFileEnc (file_path, doc, "UTF-8", 1); xmlFreeDoc(doc); if (succes == -1) { g_set_error (error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_FILE, _("Unable to save %s\n"), file_path); return FALSE; } else { gstyle_palette_set_changed (self, FALSE); return TRUE; } }