libgimpwidgets: Allow colors to be dropped on ColorAreas

Post color space invasion, this function needed to read in
GeglColors directly rather than converting from GimpRGB.
This commit is contained in:
Alx Sa 2024-03-10 16:21:39 +00:00
parent aaba366f0c
commit 5ce128a9fa
1 changed files with 70 additions and 12 deletions

View File

@ -973,27 +973,85 @@ gimp_color_area_drag_data_received (GtkWidget *widget,
guint time)
{
GimpColorArea *area = GIMP_COLOR_AREA (widget);
const guint16 *vals;
GeglColor *color;
GimpRGB rgb;
const guchar *data;
gint data_length;
const Babl *format;
const gchar *encoding;
gint encoding_length;
const guchar *pixel;
gint pixel_length;
const guint8 *profile_data = NULL;
int profile_length = 0;
if (gtk_selection_data_get_length (selection_data) != 8 ||
gtk_selection_data_get_format (selection_data) != 16)
if (selection_data == NULL)
{
g_warning ("%s: received invalid color data", G_STRFUNC);
return;
}
vals = (const guint16 *) gtk_selection_data_get_data (selection_data);
data = gtk_selection_data_get_data (selection_data);
data_length = gtk_selection_data_get_length (selection_data);
encoding = (const gchar *) data;
encoding_length = strlen (encoding) + 1;
if (! babl_format_exists ((const char *) data))
{
g_critical ("%s: received invalid color format: \"%s\"!", G_STRFUNC, encoding);
return;
}
format = babl_format (encoding);
pixel_length = babl_format_get_bytes_per_pixel (format);
if (data_length < encoding_length + pixel_length)
{
g_critical ("%s: received invalid color data of %d bytes "
"(expected: %d bytes or more)!",
G_STRFUNC, data_length, encoding_length + pixel_length);
return;
}
pixel = data + encoding_length;
profile_length = data_length - encoding_length - pixel_length;
if (profile_length > 0)
{
GimpColorProfile *profile;
GError *error = NULL;
profile_data = pixel + pixel_length;
profile = gimp_color_profile_new_from_icc_profile (profile_data, profile_length, &error);
if (profile)
{
const Babl *space;
space = gimp_color_profile_get_space (profile,
GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC,
&error);
if (space)
{
format = babl_format_with_space (encoding, space);
}
else
{
g_warning ("%s: failed to create Babl space for profile: %s",
G_STRFUNC, error->message);
g_clear_error (&error);
}
g_object_unref (profile);
}
else
{
g_warning ("%s: received invalid profile data of %d bytes: %s",
G_STRFUNC, profile_length, error->message);
g_clear_error (&error);
}
}
gimp_rgba_set (&rgb,
(gdouble) vals[0] / 0xffff,
(gdouble) vals[1] / 0xffff,
(gdouble) vals[2] / 0xffff,
(gdouble) vals[3] / 0xffff);
color = gegl_color_new (NULL);
gegl_color_set_pixel (color, babl_format ("R'G'B' double"), &rgb);
gegl_color_set_pixel (color, format, pixel);
gimp_color_area_set_color (area, color);
g_object_unref (color);