modules: the color wheel selector now displays and select in the active…

… image's space!
This commit is contained in:
Jehan 2023-12-17 14:28:29 +09:00
parent 1066229ba6
commit ebb93a07a5
3 changed files with 77 additions and 105 deletions

View File

@ -48,6 +48,7 @@ struct _ColorselWheel
GimpColorSelector parent_instance;
GtkWidget *hsv;
const Babl *format;
};
struct _ColorselWheelClass
@ -60,6 +61,8 @@ GType colorsel_wheel_get_type (void);
static void colorsel_wheel_set_color (GimpColorSelector *selector,
GeglColor *color);
static void colorsel_wheel_set_format (GimpColorSelector *selector,
const Babl *format);
static void colorsel_wheel_set_config (GimpColorSelector *selector,
GimpColorConfig *config);
static void colorsel_wheel_changed (GimpColorWheel *hsv,
@ -105,6 +108,7 @@ colorsel_wheel_class_init (ColorselWheelClass *klass)
selector_class->icon_name = GIMP_ICON_COLOR_SELECTOR_TRIANGLE;
selector_class->set_color = colorsel_wheel_set_color;
selector_class->set_config = colorsel_wheel_set_config;
selector_class->set_format = colorsel_wheel_set_format;
gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (klass), "ColorselWheel");
}
@ -135,7 +139,7 @@ colorsel_wheel_set_color (GimpColorSelector *selector,
ColorselWheel *wheel = COLORSEL_WHEEL (selector);
gdouble hsv[3];
gegl_color_get_pixel (color, babl_format ("HSV double"), hsv);
gegl_color_get_pixel (color, babl_format_with_space ("HSV double", wheel->format), hsv);
g_signal_handlers_block_by_func (wheel->hsv,
G_CALLBACK (colorsel_wheel_changed),
wheel);
@ -145,6 +149,19 @@ colorsel_wheel_set_color (GimpColorSelector *selector,
wheel);
}
static void
colorsel_wheel_set_format (GimpColorSelector *selector,
const Babl *format)
{
ColorselWheel *wheel = COLORSEL_WHEEL (selector);
if (wheel->format != format)
{
wheel->format = format;
gimp_color_wheel_set_format (GIMP_COLOR_WHEEL (wheel->hsv), format);
}
}
static void
colorsel_wheel_set_config (GimpColorSelector *selector,
GimpColorConfig *config)
@ -163,7 +180,9 @@ colorsel_wheel_changed (GimpColorWheel *wheel,
gdouble hsv[3];
gimp_color_wheel_get_color (wheel, &hsv[0], &hsv[1], &hsv[2]);
gegl_color_set_pixel (color, babl_format ("HSV double"), hsv);
gegl_color_set_pixel (color, babl_format_with_space ("HSV double",
COLORSEL_WHEEL (selector)->format),
hsv);
gimp_color_selector_set_color (selector, color);
g_object_unref (color);
}

View File

@ -85,7 +85,8 @@ typedef struct
guint focus_on_ring : 1;
GimpColorConfig *config;
GimpColorTransform *transform;
const Babl *format;
const Babl *render_fish;
} GimpColorWheelPrivate;
enum
@ -132,9 +133,6 @@ static void gimp_color_wheel_drag_cancel (GtkGesture *gestu
GdkEventSequence *sequence,
gpointer user_data);
static void gimp_color_wheel_create_transform (GimpColorWheel *wheel);
static void gimp_color_wheel_destroy_transform (GimpColorWheel *wheel);
static guint wheel_signals[LAST_SIGNAL];
@ -236,14 +234,11 @@ gimp_color_wheel_init (GimpColorWheel *wheel)
gtk_widget_set_has_window (GTK_WIDGET (wheel), FALSE);
gtk_widget_set_can_focus (GTK_WIDGET (wheel), TRUE);
priv->format = NULL;
priv->ring_fraction = DEFAULT_FRACTION;
priv->size = DEFAULT_SIZE;
priv->ring_width = DEFAULT_RING_WIDTH;
gimp_widget_track_monitor (GTK_WIDGET (wheel),
G_CALLBACK (gimp_color_wheel_destroy_transform),
NULL, NULL);
/* Allow the user to drag the rectangle on the preview */
gesture = gtk_gesture_drag_new (GTK_WIDGET (wheel));
g_signal_connect (gesture, "drag-begin",
@ -875,21 +870,11 @@ paint_ring (GimpColorWheel *wheel,
}
}
if (priv->transform)
p = buf;
for (gint i = 0; i < height; i++)
{
const Babl *format = babl_format ("cairo-RGB24");
guchar *b = (guchar *) buf;
gint i;
for (i = 0; i < height; i++)
{
gimp_color_transform_process_pixels (priv->transform,
format, b,
format, b,
width);
b += stride;
}
babl_process (priv->render_fish, p, p, width);
p += stride / 4;
}
source = cairo_image_surface_create_for_data ((guchar *) buf,
@ -1104,21 +1089,11 @@ paint_triangle (GimpColorWheel *wheel,
}
}
if (priv->transform)
p = buf;
for (gint i = 0; i < height; i++)
{
const Babl *format = babl_format ("cairo-RGB24");
guchar *b = (guchar *) buf;
gint i;
for (i = 0; i < height; i++)
{
gimp_color_transform_process_pixels (priv->transform,
format, b,
format, b,
width);
b += stride;
}
babl_process (priv->render_fish, p, p, width);
p += stride / 4;
}
source = cairo_image_surface_create_for_data ((guchar *) buf,
@ -1199,11 +1174,13 @@ gimp_color_wheel_draw (GtkWidget *widget,
GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget);
GimpColorWheelPrivate *priv = gimp_color_wheel_get_instance_private (wheel);
gboolean draw_focus;
const Babl *render_space;
draw_focus = gtk_widget_has_visible_focus (widget);
if (! priv->transform)
gimp_color_wheel_create_transform (wheel);
render_space = gimp_widget_get_render_space (widget, priv->config);
priv->render_fish = babl_fish (babl_format_with_space ("cairo-RGB24", priv->format),
babl_format_with_space ("cairo-RGB24", render_space)),
paint_ring (wheel, cr);
paint_triangle (wheel, cr, draw_focus);
@ -1299,7 +1276,9 @@ gimp_color_wheel_new (void)
* @v: Value
*
* Sets the current color in an HSV color selector.
* Color component values must be in the [0.0, 1.0] range.
* Color component values must be in the [0.0, 1.0] range and the color space
* of the HSV values will be the one of the Babl format specified in
* `gimp_color_wheel_set_format()`.
*
* Since: 2.10
*/
@ -1312,14 +1291,19 @@ gimp_color_wheel_set_color (GimpColorWheel *wheel,
GimpColorWheelPrivate *priv = gimp_color_wheel_get_instance_private (wheel);
g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel));
g_return_if_fail (h >= 0.0 && h <= 1.0);
g_return_if_fail (s >= 0.0 && s <= 1.0);
g_return_if_fail (v >= 0.0 && v <= 1.0);
priv->h = h;
priv->s = s;
priv->v = v;
priv->h = CLAMP (h, 0.0, 1.0);
priv->s = CLAMP (s, 0.0, 1.0);
priv->v = CLAMP (v, 0.0, 1.0);
/* If the set color is out of gamut, select the clamped color, but do not
* emit any CHANGED state. The color might have been selected through another
* method and the color selector simply updated. We don't want to trigger any
* chain change because of the clamping.
*/
if (h >= 0.0 && h <= 1.0 &&
s >= 0.0 && s <= 1.0 &&
v >= 0.0 && v <= 1.0)
g_signal_emit (wheel, wheel_signals[CHANGED], 0);
gtk_widget_queue_draw (GTK_WIDGET (wheel));
@ -1333,7 +1317,9 @@ gimp_color_wheel_set_color (GimpColorWheel *wheel,
* @v: (out): Return value for the value
*
* Queries the current color in an HSV color selector.
* Returned values will be in the [0.0, 1.0] range.
* Returned values will be in the [0.0, 1.0] range and the color space of the
* HSV values will be the one of the Babl format specified in
* `gimp_color_wheel_set_format()`.
*
* Since: 2.10
*/
@ -1411,24 +1397,28 @@ gimp_color_wheel_set_color_config (GimpColorWheel *wheel,
g_return_if_fail (config == NULL || GIMP_IS_COLOR_CONFIG (config));
if (config != priv->config)
{
if (priv->config)
{
g_signal_handlers_disconnect_by_func (priv->config,
gimp_color_wheel_destroy_transform,
wheel);
gimp_color_wheel_destroy_transform (wheel);
}
g_set_object (&priv->config, config);
if (priv->config)
{
g_signal_connect_swapped (priv->config, "notify",
G_CALLBACK (gimp_color_wheel_destroy_transform),
wheel);
}
/**
* gimp_color_wheel_set_format:
* @wheel: a #GimpColorWheel widget.
* @format: a Babl format.
*
* Sets the format which you want to select colors in.
*
* Since: 3.0
*/
void
gimp_color_wheel_set_format (GimpColorWheel *wheel,
const Babl *format)
{
GimpColorWheelPrivate *priv = gimp_color_wheel_get_instance_private (wheel);
if (priv->format != format)
{
priv->format = format;
gtk_widget_queue_draw (GTK_WIDGET (wheel));
}
}
@ -1532,42 +1522,3 @@ gimp_color_wheel_move (GimpColorWheel *wheel,
gimp_color_wheel_set_color (wheel, hue, sat, val);
}
static void
gimp_color_wheel_create_transform (GimpColorWheel *wheel)
{
GimpColorWheelPrivate *priv = gimp_color_wheel_get_instance_private (wheel);
if (priv->config)
{
static GimpColorProfile *profile = NULL;
const Babl *format = babl_format ("cairo-RGB24");
if (G_UNLIKELY (! profile))
profile = gimp_color_profile_new_rgb_srgb ();
priv->transform = gimp_widget_get_color_transform (GTK_WIDGET (wheel),
priv->config,
profile,
format,
format,
NULL,
GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC,
FALSE);
}
}
static void
gimp_color_wheel_destroy_transform (GimpColorWheel *wheel)
{
GimpColorWheelPrivate *priv = gimp_color_wheel_get_instance_private (wheel);
if (priv->transform)
{
g_object_unref (priv->transform);
priv->transform = NULL;
}
gtk_widget_queue_draw (GTK_WIDGET (wheel));
}

View File

@ -85,6 +85,8 @@ void gimp_color_wheel_set_ring_fraction (GimpColorWheel *wheel,
gdouble fraction);
gdouble gimp_color_wheel_get_ring_fraction (GimpColorWheel *wheel);
void gimp_color_wheel_set_format (GimpColorWheel *wheel,
const Babl *format);
void gimp_color_wheel_set_color_config (GimpColorWheel *wheel,
GimpColorConfig *config);