[gtksourceview] gtksourceview: add GIMP style color formats



commit ff33e68bb576e4ddca2bba35577d9dafc9cbe77b
Author: Christian Hergert <chergert redhat com>
Date:   Mon Sep 2 17:52:36 2019 -0700

    gtksourceview: add GIMP style color formats
    
    This implements an alternate format so that we can have DnD coming from
    the GIMP color picker. With this patch, we can drag from the color picker
    into the GtkSourceView.

 gtksourceview/gtksourceview.c | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)
---
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 60c2701c..eec9ce57 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -4322,30 +4322,52 @@ view_dnd_drop (GtkTextView      *view,
 
        if (info == TARGET_COLOR)
        {
-               guint16 *vals;
+               GdkRGBA rgba;
                gchar string[] = "#000000";
                gint buffer_x;
                gint buffer_y;
                gint length = gtk_selection_data_get_length (selection_data);
+               guint format;
 
                if (length < 0)
                {
                        return;
                }
 
-               if (gtk_selection_data_get_format (selection_data) != 16 || length != 8)
+               format = gtk_selection_data_get_format (selection_data);
+
+               if (format == 8 && length == 4)
                {
-                       g_warning ("Received invalid color data\n");
-                       return;
+                       guint8 *vals;
+
+                       vals = (gpointer) gtk_selection_data_get_data (selection_data);
+
+                       rgba.red = vals[0] / 256.0;
+                       rgba.green = vals[1] / 256.0;
+                       rgba.blue = vals[2] / 256.0;
+                       rgba.alpha = 1.0;
                }
+               else if (format == 16 && length == 8)
+               {
+                       guint16 *vals;
 
-               vals = (gpointer) gtk_selection_data_get_data (selection_data);
+                       vals = (gpointer) gtk_selection_data_get_data (selection_data);
 
-               vals[0] /= 256;
-               vals[1] /= 256;
-               vals[2] /= 256;
+                       rgba.red = vals[0] / 65535.0;
+                       rgba.green = vals[1] / 65535.0;
+                       rgba.blue = vals[2] / 65535.0;
+                       rgba.alpha = 1.0;
+               }
+               else
+               {
+                       g_warning ("Received invalid color data\n");
+                       return;
+               }
 
-               g_snprintf (string, sizeof (string), "#%02X%02X%02X", vals[0], vals[1], vals[2]);
+               g_snprintf (string, sizeof string, "#%02X%02X%02X",
+                           (gint)(rgba.red * 256),
+                           (gint)(rgba.green * 256),
+                           (gint)(rgba.blue * 256));
 
                gtk_text_view_window_to_buffer_coords (view,
                                                       GTK_TEXT_WINDOW_TEXT,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]