Code

Cleanup on color changers
[inkscape.git] / src / widgets / sp-color-icc-selector.cpp
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <math.h>
5 #include <gtk/gtkbutton.h>
6 #include <gtk/gtkcombobox.h>
7 #include <gtk/gtksignal.h>
8 #include <gtk/gtklabel.h>
9 #include <gtk/gtktable.h>
10 #include <gtk/gtkspinbutton.h>
11 #include <glibmm/i18n.h>
12 #include "../dialogs/dialog-events.h"
13 #include "sp-color-icc-selector.h"
14 #include "sp-color-scales.h"
15 #include "svg/svg-icc-color.h"
16 #include "document.h"
17 #include "inkscape.h"
18 #include "profile-manager.h"
20 #define noDEBUG_LCMS
22 #if ENABLE_LCMS
23 #include "color-profile-fns.h"
24 #include "color-profile.h"
25 //#define DEBUG_LCMS
26 #ifdef DEBUG_LCMS
27 #include "prefs-utils.h"
28 #include <gtk/gtkmessagedialog.h>
29 #endif // DEBUG_LCMS
30 #endif // ENABLE_LCMS
33 #ifdef DEBUG_LCMS
34 extern guint update_in_progress;
35 #define DEBUG_MESSAGE(key, ...) \
36 {\
37     gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
38     gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
39     gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
40     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
41     if ( dump )\
42     {\
43         g_message( __VA_ARGS__ );\
44 \
45     }\
46     if ( dumpD )\
47     {\
48         GtkWidget *dialog = gtk_message_dialog_new(NULL,\
49                                                    GTK_DIALOG_DESTROY_WITH_PARENT, \
50                                                    GTK_MESSAGE_INFO,    \
51                                                    GTK_BUTTONS_OK,      \
52                                                    __VA_ARGS__          \
53                                                    );\
54         g_signal_connect_swapped(dialog, "response",\
55                                  G_CALLBACK(gtk_widget_destroy),        \
56                                  dialog);                               \
57         gtk_widget_show_all( dialog );\
58     }\
59 }
60 #endif // DEBUG_LCMS
64 G_BEGIN_DECLS
66 static void sp_color_icc_selector_class_init (SPColorICCSelectorClass *klass);
67 static void sp_color_icc_selector_init (SPColorICCSelector *cs);
68 static void sp_color_icc_selector_destroy (GtkObject *object);
70 static void sp_color_icc_selector_show_all (GtkWidget *widget);
71 static void sp_color_icc_selector_hide_all (GtkWidget *widget);
74 G_END_DECLS
76 static SPColorSelectorClass *parent_class;
78 #define XPAD 4
79 #define YPAD 1
81 GType
82 sp_color_icc_selector_get_type (void)
83 {
84     static GType type = 0;
85     if (!type) {
86         static const GTypeInfo info = {
87             sizeof (SPColorICCSelectorClass),
88             NULL, /* base_init */
89             NULL, /* base_finalize */
90             (GClassInitFunc) sp_color_icc_selector_class_init,
91             NULL, /* class_finalize */
92             NULL, /* class_data */
93             sizeof (SPColorICCSelector),
94             0,    /* n_preallocs */
95             (GInstanceInitFunc) sp_color_icc_selector_init,
96             0,    /* value_table */
97         };
99         type = g_type_register_static (SP_TYPE_COLOR_SELECTOR,
100                                        "SPColorICCSelector",
101                                        &info,
102                                        static_cast< GTypeFlags > (0) );
103     }
104     return type;
107 static void
108 sp_color_icc_selector_class_init (SPColorICCSelectorClass *klass)
110     static const gchar* nameset[] = {N_("CMS"), 0};
111     GtkObjectClass *object_class;
112     GtkWidgetClass *widget_class;
113     SPColorSelectorClass *selector_class;
115     object_class = (GtkObjectClass *) klass;
116     widget_class = (GtkWidgetClass *) klass;
117     selector_class = SP_COLOR_SELECTOR_CLASS (klass);
119     parent_class = SP_COLOR_SELECTOR_CLASS (g_type_class_peek_parent (klass));
121     selector_class->name = nameset;
122     selector_class->submode_count = 1;
124     object_class->destroy = sp_color_icc_selector_destroy;
126     widget_class->show_all = sp_color_icc_selector_show_all;
127     widget_class->hide_all = sp_color_icc_selector_hide_all;
131 ColorICCSelector::ColorICCSelector( SPColorSelector* csel )
132     : ColorSelector( csel ),
133       _updating( FALSE ),
134       _dragging( FALSE ),
135       _fixupNeeded(0),
136       _fooCount(0),
137       _fooScales(0),
138       _fooAdj(0),
139       _fooSlider(0),
140       _fooBtn(0),
141       _fooLabel(0),
142       _fooMap(0),
143       _adj(0),
144       _sbtn(0),
145       _label(0),
146       _tt(0)
147 #if ENABLE_LCMS
148     ,
149       _profileName(""),
150       _prof(),
151       _profChannelCount(0),
152       _profChangedID(0)
153 #endif // ENABLE_LCMS
157 ColorICCSelector::~ColorICCSelector()
159     _adj = 0;
160     _sbtn = 0;
161     _label = 0;
164 void sp_color_icc_selector_init (SPColorICCSelector *cs)
166     SP_COLOR_SELECTOR(cs)->base = new ColorICCSelector( SP_COLOR_SELECTOR(cs) );
168     if ( SP_COLOR_SELECTOR(cs)->base )
169     {
170         SP_COLOR_SELECTOR(cs)->base->init();
171     }
175 /*
176 icSigRgbData
177 icSigCmykData
178 icSigCmyData
179 */
180 #define SPACE_ID_RGB 0
181 #define SPACE_ID_CMY 1
182 #define SPACE_ID_CMYK 2
185 #if ENABLE_LCMS
186 static icUInt16Number* getScratch() {
187     // bytes per pixel * input channels * width
188     static icUInt16Number* scritch = static_cast<icUInt16Number*>(g_new(icUInt16Number, 4 * 1024));
190     return scritch;
193 struct MapMap {
194     DWORD space;
195     DWORD inForm;
196 };
198 void getThings( DWORD space, gchar const**& namers, gchar const**& tippies, guint const*& scalies ) {
199     MapMap possible[] = {
200         {icSigXYZData,   TYPE_XYZ_16},
201         {icSigLabData,   TYPE_Lab_16},
202         //icSigLuvData
203         {icSigYCbCrData, TYPE_YCbCr_16},
204         {icSigYxyData,   TYPE_Yxy_16},
205         {icSigRgbData,   TYPE_RGB_16},
206         {icSigGrayData,  TYPE_GRAY_16},
207         {icSigHsvData,   TYPE_HSV_16},
208         {icSigHlsData,   TYPE_HLS_16},
209         {icSigCmykData,  TYPE_CMYK_16},
210         {icSigCmyData,   TYPE_CMY_16},
211     };
213     static gchar const *names[][6] = {
214         {"_X", "_Y", "_Z", "", "", ""},
215         {"_L", "_a", "_b", "", "", ""},
216         //
217         {"_Y", "C_b", "C_r", "", "", ""},
218         {"_Y", "_x", "y", "", "", ""},
219         {_("_R"), _("_G"), _("_B"), "", "", ""},
220         {_("_G"), "", "", "", "", ""},
221         {_("_H"), _("_S"), "_V", "", "", ""},
222         {_("_H"), _("_L"), _("_S"), "", "", ""},
223         {_("_C"), _("_M"), _("_Y"), _("_K"), "", ""},
224         {_("_C"), _("_M"), _("_Y"), "", "", ""},
225     };
227     static gchar const *tips[][6] = {
228         {"X", "Y", "Z", "", "", ""},
229         {"L", "a", "b", "", "", ""},
230         //
231         {"Y", "Cb", "Cr", "", "", ""},
232         {"Y", "x", "y", "", "", ""},
233         {_("Red"), _("Green"), _("Blue"), "", "", ""},
234         {_("Gray"), "", "", "", "", ""},
235         {_("Hue"), _("Saturation"), "Value", "", "", ""},
236         {_("Hue"), _("Lightness"), _("Saturation"), "", "", ""},
237         {_("Cyan"), _("Magenta"), _("Yellow"), _("Black"), "", ""},
238         {_("Cyan"), _("Magenta"), _("Yellow"), "", "", ""},
239     };
241     static guint scales[][6] = {
242         {2, 1, 2, 1, 1, 1},
243         {100, 256, 256, 1, 1, 1},
244         //
245         {1, 1, 1, 1, 1, 1},
246         {1, 1, 1, 1, 1, 1},
247         {1, 1, 1, 1, 1, 1},
248         {1, 1, 1, 1, 1, 1},
249         {360, 1, 1, 1, 1, 1},
250         {360, 1, 1, 1, 1, 1},
251         {1, 1, 1, 1, 1, 1},
252         {1, 1, 1, 1, 1, 1},
253     };
255     int index = 0;
256     for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
257         if ( possible[i].space == space ) {
258             index = i;
259             break;
260         }
261     }
263     namers = names[index];
264     tippies = tips[index];
265     scalies = scales[index];
267 #endif // ENABLE_LCMS
270 void ColorICCSelector::init()
272     GtkWidget *t;
273     gint row = 0;
275     _updating = FALSE;
276     _dragging = FALSE;
278     _tt = gtk_tooltips_new();
280     t = gtk_table_new (5, 3, FALSE);
281     gtk_widget_show (t);
282     gtk_box_pack_start (GTK_BOX (_csel), t, TRUE, TRUE, 0);
284 #if ENABLE_LCMS
285     //guint partCount = _cmsChannelsOf( icSigRgbData );
286     gchar const** names = 0;
287     gchar const** tips = 0;
288     getThings( icSigRgbData, names, tips, _fooScales );
289 #endif // ENABLE_LCMS
291     /* Create components */
292     row = 0;
295     _fixupBtn = gtk_button_new_with_label(_("Fix"));
296     g_signal_connect( G_OBJECT(_fixupBtn), "clicked", G_CALLBACK(_fixupHit), (gpointer)this );
297     gtk_widget_set_sensitive( _fixupBtn, FALSE );
298     gtk_tooltips_set_tip( _tt, _fixupBtn, _("Fix RGB fallback to match icc-color() value."), NULL );
299     //gtk_misc_set_alignment( GTK_MISC (_fixupBtn), 1.0, 0.5 );
300     gtk_widget_show( _fixupBtn );
301     gtk_table_attach( GTK_TABLE (t), _fixupBtn, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD );
304     _profileSel = gtk_combo_box_new_text();
305     gtk_combo_box_append_text( GTK_COMBO_BOX(_profileSel), "<none>" );
306     gtk_widget_show( _profileSel );
307     gtk_combo_box_set_active( GTK_COMBO_BOX(_profileSel), 0 );
308     gtk_table_attach( GTK_TABLE(t), _profileSel, 1, 2, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD );
310 #if ENABLE_LCMS
311     _profChangedID = g_signal_connect( G_OBJECT(_profileSel), "changed", G_CALLBACK(_profileSelected), (gpointer)this );
312     gtk_widget_set_sensitive( _profileSel, false ); // temporary
313 #else
314     gtk_widget_set_sensitive( _profileSel, false );
315 #endif // ENABLE_LCMS
318     row++;
320     _fooCount = 4;
321     _fooAdj = new GtkAdjustment*[_fooCount];
322     _fooSlider = new GtkWidget*[_fooCount];
323     _fooBtn = new GtkWidget*[_fooCount];
324     _fooLabel = new GtkWidget*[_fooCount];
325     _fooMap = new guchar*[_fooCount];
327     for ( guint i = 0; i < _fooCount; i++ ) {
328         /* Label */
329 #if ENABLE_LCMS
330         _fooLabel[i] = gtk_label_new_with_mnemonic( names[i] );
331 #else
332         _fooLabel[i] = gtk_label_new_with_mnemonic( "." );
333 #endif // ENABLE_LCMS
334         gtk_misc_set_alignment( GTK_MISC (_fooLabel[i]), 1.0, 0.5 );
335         gtk_widget_show( _fooLabel[i] );
336         gtk_table_attach( GTK_TABLE (t), _fooLabel[i], 0, 1, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD );
338         /* Adjustment */
339         gdouble step = static_cast<gdouble>(_fooScales[i]) / 100.0;
340         gdouble page = static_cast<gdouble>(_fooScales[i]) / 10.0;
341         gint digits = (step > 0.9) ? 0 : 2;
342         _fooAdj[i] = GTK_ADJUSTMENT( gtk_adjustment_new( 0.0, 0.0, _fooScales[i],  step, page, page ) );
344         /* Slider */
345         _fooSlider[i] = sp_color_slider_new( _fooAdj[i] );
346 #if ENABLE_LCMS
347         gtk_tooltips_set_tip( _tt, _fooSlider[i], tips[i], NULL );
348 #else
349         gtk_tooltips_set_tip( _tt, _fooSlider[i], ".", NULL );
350 #endif // ENABLE_LCMS
351         gtk_widget_show( _fooSlider[i] );
352         gtk_table_attach( GTK_TABLE (t), _fooSlider[i], 1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)GTK_FILL, XPAD, YPAD );
354         _fooBtn[i] = gtk_spin_button_new( _fooAdj[i], step, digits );
355 #if ENABLE_LCMS
356         gtk_tooltips_set_tip( _tt, _fooBtn[i], tips[i], NULL );
357 #else
358         gtk_tooltips_set_tip( _tt, _fooBtn[i], ".", NULL );
359 #endif // ENABLE_LCMS
360         sp_dialog_defocus_on_enter( _fooBtn[i] );
361         gtk_label_set_mnemonic_widget( GTK_LABEL(_fooLabel[i]), _fooBtn[i] );
362         gtk_widget_show( _fooBtn[i] );
363         gtk_table_attach( GTK_TABLE (t), _fooBtn[i], 2, 3, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, XPAD, YPAD );
365         _fooMap[i] = g_new( guchar, 4 * 1024 );
366         memset( _fooMap[i], 0x0ff, 1024 * 4 );
369         /* Signals */
370         gtk_signal_connect( GTK_OBJECT( _fooAdj[i] ), "value_changed", GTK_SIGNAL_FUNC( _adjustmentChanged ), _csel );
372         gtk_signal_connect( GTK_OBJECT( _fooSlider[i] ), "grabbed", GTK_SIGNAL_FUNC( _sliderGrabbed ), _csel );
373         gtk_signal_connect( GTK_OBJECT( _fooSlider[i] ), "released", GTK_SIGNAL_FUNC( _sliderReleased ), _csel );
374         gtk_signal_connect( GTK_OBJECT( _fooSlider[i] ), "changed", GTK_SIGNAL_FUNC( _sliderChanged ), _csel );
376         row++;
377     }
379     /* Label */
380     _label = gtk_label_new_with_mnemonic (_("_A"));
381     gtk_misc_set_alignment (GTK_MISC (_label), 1.0, 0.5);
382     gtk_widget_show (_label);
383     gtk_table_attach (GTK_TABLE (t), _label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD);
385     /* Adjustment */
386     _adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 10.0, 10.0);
388     /* Slider */
389     _slider = sp_color_slider_new (_adj);
390     gtk_tooltips_set_tip (_tt, _slider, _("Alpha (opacity)"), NULL);
391     gtk_widget_show (_slider);
392     gtk_table_attach (GTK_TABLE (t), _slider, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)GTK_FILL, XPAD, YPAD);
394     sp_color_slider_set_colors( SP_COLOR_SLIDER( _slider ),
395                                 SP_RGBA32_F_COMPOSE( 1.0, 1.0, 1.0, 0.0 ),
396                                 SP_RGBA32_F_COMPOSE( 1.0, 1.0, 1.0, 0.5 ),
397                                 SP_RGBA32_F_COMPOSE( 1.0, 1.0, 1.0, 1.0 ) );
400     /* Spinbutton */
401     _sbtn = gtk_spin_button_new (GTK_ADJUSTMENT (_adj), 1.0, 0);
402     gtk_tooltips_set_tip (_tt, _sbtn, _("Alpha (opacity)"), NULL);
403     sp_dialog_defocus_on_enter (_sbtn);
404     gtk_label_set_mnemonic_widget (GTK_LABEL(_label), _sbtn);
405     gtk_widget_show (_sbtn);
406     gtk_table_attach (GTK_TABLE (t), _sbtn, 2, 3, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, XPAD, YPAD);
408     /* Signals */
409     gtk_signal_connect (GTK_OBJECT (_adj), "value_changed",
410                         GTK_SIGNAL_FUNC (_adjustmentChanged), _csel);
412     gtk_signal_connect (GTK_OBJECT (_slider), "grabbed",
413                         GTK_SIGNAL_FUNC (_sliderGrabbed), _csel);
414     gtk_signal_connect (GTK_OBJECT (_slider), "released",
415                         GTK_SIGNAL_FUNC (_sliderReleased), _csel);
416     gtk_signal_connect (GTK_OBJECT (_slider), "changed",
417                         GTK_SIGNAL_FUNC (_sliderChanged), _csel);
420 static void
421 sp_color_icc_selector_destroy (GtkObject *object)
423     if (((GtkObjectClass *) (parent_class))->destroy)
424         (* ((GtkObjectClass *) (parent_class))->destroy) (object);
427 static void
428 sp_color_icc_selector_show_all (GtkWidget *widget)
430     gtk_widget_show (widget);
433 static void
434 sp_color_icc_selector_hide_all (GtkWidget *widget)
436     gtk_widget_hide (widget);
439 GtkWidget *
440 sp_color_icc_selector_new (void)
442     SPColorICCSelector *csel;
444     csel = (SPColorICCSelector*)gtk_type_new (SP_TYPE_COLOR_ICC_SELECTOR);
446     return GTK_WIDGET (csel);
450 void ColorICCSelector::_fixupHit( GtkWidget* src, gpointer data )
452     (void)src;
453     ColorICCSelector* self = reinterpret_cast<ColorICCSelector*>(data);
454     gtk_widget_set_sensitive( self->_fixupBtn, FALSE );
455     self->_adjustmentChanged( self->_fooAdj[0], SP_COLOR_ICC_SELECTOR(self->_csel) );
458 #if ENABLE_LCMS
459 void ColorICCSelector::_profileSelected( GtkWidget* src, gpointer data )
461     (void)src;
462     ColorICCSelector* self = reinterpret_cast<ColorICCSelector*>(data);
463     gint activeIndex = gtk_combo_box_get_active( GTK_COMBO_BOX(self->_profileSel) );
464     gchar* name = (activeIndex != 0) ? gtk_combo_box_get_active_text( GTK_COMBO_BOX(self->_profileSel) ) : 0;
465     //self->_switchToProfile( name );
466     if ( name ) {
467         g_free( name );
468     }
470 #endif // ENABLE_LCMS
472 void ColorICCSelector::_switchToProfile( gchar const* name )
474     bool dirty = false;
475     SPColor tmp( _color );
477     if ( name ) {
478         if ( tmp.icc && tmp.icc->colorProfile == name ) {
479             g_message("Already at name [%s]", name );
480         } else {
481             g_message("Need to switch to profile [%s]", name );
482             if ( tmp.icc ) {
483                 tmp.icc->colors.clear();
484             } else {
485                 tmp.icc = new SVGICCColor();
486             }
487             tmp.icc->colorProfile = name;
488             dirty = true;
489         }
490     } else {
491         g_message("NUKE THE ICC");
492         if ( tmp.icc ) {
493             delete tmp.icc;
494             tmp.icc = 0;
495             dirty = true;
496         } else {
497             g_message("No icc to nuke");
498         }
499     }
501     if ( dirty ) {
502         _setProfile( tmp.icc );
503         // Set the color now.
505     }
508 void ColorICCSelector::_profilesChanged( std::string const & name )
510     GtkComboBox* combo = GTK_COMBO_BOX(_profileSel);
512     g_signal_handler_block( G_OBJECT(_profileSel), _profChangedID );
514     GtkTreeModel* model = gtk_combo_box_get_model( combo );
515     GtkTreeIter iter;
516     while ( gtk_tree_model_get_iter_first( model, &iter ) ) {
517         gtk_combo_box_remove_text( combo, 0 );
518     }
520     gtk_combo_box_append_text( combo, "<none>");
522     gtk_combo_box_set_active( combo, 0 );
524     int index = 1;
525     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
526     while ( current ) {
527         SPObject* obj = SP_OBJECT(current->data);
528         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
529         gtk_combo_box_append_text( combo, prof->name );
530         if ( name == prof->name ) {
531             gtk_combo_box_set_active( combo, index );
532         }
534         index++;
535         current = g_slist_next(current);
536     }
538     g_signal_handler_unblock( G_OBJECT(_profileSel), _profChangedID );
541 /* Helpers for setting color value */
543 void ColorICCSelector::_colorChanged()
545     _updating = TRUE;
546 //     sp_color_icc_set_color( SP_COLOR_ICC( _icc ), &color );
548 #ifdef DEBUG_LCMS
549     g_message( "/^^^^^^^^^  %p::_colorChanged(%08x:%s)", this,
550                color.toRGBA32(alpha), ( (color.icc) ? color.icc->colorProfile.c_str(): "<null>" )
551                );
552 #endif // DEBUG_LCMS
554 #ifdef DEBUG_LCMS
555     g_message("FLIPPIES!!!!     %p   '%s'", color.icc, (color.icc?color.icc->colorProfile.c_str():"<null>"));
556 #endif // DEBUG_LCMS
558     _profilesChanged( (_color.icc) ? _color.icc->colorProfile : std::string("") );
559     ColorScales::setScaled( _adj, _alpha );
561 #if ENABLE_LCMS
562     _setProfile( _color.icc );
563     _fixupNeeded = 0;
564     gtk_widget_set_sensitive( _fixupBtn, FALSE );
566     if ( _prof && _prof->getTransfToSRGB8() ) {
567         icUInt16Number tmp[4];
568         for ( guint i = 0; i < _profChannelCount; i++ ) {
569             gdouble val = 0.0;
570             if ( _color.icc->colors.size() > i ) {
571                 if ( _fooScales[i] == 256 ) {
572                     val = (_color.icc->colors[i] + 128.0) / static_cast<gdouble>(_fooScales[i]);
573                 } else {
574                     val = _color.icc->colors[i] / static_cast<gdouble>(_fooScales[i]);
575                 }
576             }
577             tmp[i] = val * 0x0ffff;
578         }
579         guchar post[4] = {0,0,0,0};
580         cmsDoTransform( _prof->getTransfToSRGB8(), tmp, post, 1 );
581         guint32 other = SP_RGBA32_U_COMPOSE(post[0], post[1], post[2], 255 );
582         if ( other != _color.toRGBA32(255) ) {
583             _fixupNeeded = other;
584             gtk_widget_set_sensitive( _fixupBtn, TRUE );
585             //g_message("Color needs to change 0x%06x to 0x%06x", color.toRGBA32(255) >> 8, other >> 8 );
586         }
587     }
588 #else
589     (void)color;
590 #endif // ENABLE_LCMS
591     _updateSliders( -1 );
594     _updating = FALSE;
595 #ifdef DEBUG_LCMS
596     g_message( "\\_________  %p::_colorChanged()", this );
597 #endif // DEBUG_LCMS
600 #if ENABLE_LCMS
601 void ColorICCSelector::_setProfile( SVGICCColor* profile )
603 #ifdef DEBUG_LCMS
604     g_message( "/^^^^^^^^^  %p::_setProfile(%s)", this,
605                ( (profile) ? profile->colorProfile.c_str() : "<null>")
606                );
607 #endif // DEBUG_LCMS
608     bool profChanged = false;
609     if ( _prof && (!profile || (_profileName != profile->colorProfile) ) ) {
610         // Need to clear out the prior one
611         profChanged = true;
612         _profileName.clear();
613         _prof = 0;
614         _profChannelCount = 0;
615     } else if ( profile && !_prof ) {
616         profChanged = true;
617     }
619     for ( guint i = 0; i < _fooCount; i++ ) {
620         gtk_widget_hide( _fooLabel[i] );
621         gtk_widget_hide( _fooSlider[i] );
622         gtk_widget_hide( _fooBtn[i] );
623     }
625     if ( profile ) {
626         _prof = SP_ACTIVE_DOCUMENT->profileManager->find(profile->colorProfile.c_str());
627         if ( _prof && _prof->getProfileClass() != icSigNamedColorClass ) {
628             _profChannelCount = _cmsChannelsOf( _prof->getColorSpace() );
630             gchar const** names = 0;
631             gchar const** tips = 0;
632             getThings( _prof->getColorSpace(), names, tips, _fooScales );
634             if ( profChanged ) {
635                 for ( guint i = 0; i < _profChannelCount; i++ ) {
636                     gtk_label_set_text_with_mnemonic( GTK_LABEL(_fooLabel[i]), names[i]);
638                     gtk_tooltips_set_tip( _tt, _fooSlider[i], tips[i], NULL );
639                     gtk_tooltips_set_tip( _tt, _fooBtn[i], tips[i], NULL );
641                     sp_color_slider_set_colors( SP_COLOR_SLIDER(_fooSlider[i]),
642                                                 SPColor(0.0, 0.0, 0.0).toRGBA32(0xff),
643                                                 SPColor(0.5, 0.5, 0.5).toRGBA32(0xff),
644                                                 SPColor(1.0, 1.0, 1.0).toRGBA32(0xff) );
645 /*
646                     _fooAdj[i] = GTK_ADJUSTMENT( gtk_adjustment_new( val, 0.0, _fooScales[i],  step, page, page ) );
647                     gtk_signal_connect( GTK_OBJECT( _fooAdj[i] ), "value_changed", GTK_SIGNAL_FUNC( _adjustmentChanged ), _csel );
649                     sp_color_slider_set_adjustment( SP_COLOR_SLIDER(_fooSlider[i]), _fooAdj[i] );
650                     gtk_spin_button_set_adjustment( GTK_SPIN_BUTTON(_fooBtn[i]), _fooAdj[i] );
651                     gtk_spin_button_set_digits( GTK_SPIN_BUTTON(_fooBtn[i]), digits );
652 */
653                     gtk_widget_show( _fooLabel[i] );
654                     gtk_widget_show( _fooSlider[i] );
655                     gtk_widget_show( _fooBtn[i] );
656                     //gtk_adjustment_set_value( _fooAdj[i], 0.0 );
657                     //gtk_adjustment_set_value( _fooAdj[i], val );
658                 }
659                 for ( guint i = _profChannelCount; i < _fooCount; i++ ) {
660                     gtk_widget_hide( _fooLabel[i] );
661                     gtk_widget_hide( _fooSlider[i] );
662                     gtk_widget_hide( _fooBtn[i] );
663                 }
664             }
665         } else {
666             // Give up for now on named colors
667             _prof = 0;
668         }
669     }
671 #ifdef DEBUG_LCMS
672     g_message( "\\_________  %p::_setProfile()", this );
673 #endif // DEBUG_LCMS
675 #endif // ENABLE_LCMS
677 void ColorICCSelector::_updateSliders( gint ignore )
679 #ifdef ENABLE_LCMS
680     if ( _color.icc )
681     {
682         for ( guint i = 0; i < _profChannelCount; i++ ) {
683             gdouble val = 0.0;
684             if ( _color.icc->colors.size() > i ) {
685                 if ( _fooScales[i] == 256 ) {
686                     val = (_color.icc->colors[i] + 128.0) / static_cast<gdouble>(_fooScales[i]);
687                 } else {
688                     val = _color.icc->colors[i] / static_cast<gdouble>(_fooScales[i]);
689                 }
690             }
691             gtk_adjustment_set_value( _fooAdj[i], val );
692         }
694         if ( _prof->getTransfToSRGB8() ) {
695             for ( guint i = 0; i < _profChannelCount; i++ ) {
696                 if ( static_cast<gint>(i) != ignore ) {
697                     icUInt16Number* scratch = getScratch();
698                     icUInt16Number filler[4] = {0, 0, 0, 0};
699                     for ( guint j = 0; j < _profChannelCount; j++ ) {
700                         filler[j] = 0x0ffff * ColorScales::getScaled( _fooAdj[j] );
701                     }
703                     icUInt16Number* p = scratch;
704                     for ( guint x = 0; x < 1024; x++ ) {
705                         for ( guint j = 0; j < _profChannelCount; j++ ) {
706                             if ( j == i ) {
707                                 *p++ = x * 0x0ffff / 1024;
708                             } else {
709                                 *p++ = filler[j];
710                             }
711                         }
712                     }
714                     cmsDoTransform( _prof->getTransfToSRGB8(), scratch, _fooMap[i], 1024 );
715                     sp_color_slider_set_map( SP_COLOR_SLIDER(_fooSlider[i]), _fooMap[i] );
716                 }
717             }
718         }
719     }
720 #else
721     (void)ignore;
722 #endif // ENABLE_LCMS
724     guint32 start = _color.toRGBA32( 0x00 );
725     guint32 mid = _color.toRGBA32( 0x7f );
726     guint32 end = _color.toRGBA32( 0xff );
728     sp_color_slider_set_colors( SP_COLOR_SLIDER(_slider), start, mid, end );
733 void ColorICCSelector::_adjustmentChanged( GtkAdjustment *adjustment, SPColorICCSelector *cs )
735 // // TODO check this. It looks questionable:
736 //     // if a value is entered between 0 and 1 exclusive, normalize it to (int) 0..255  or 0..100
737 //     if (adjustment->value > 0.0 && adjustment->value < 1.0) {
738 //         gtk_adjustment_set_value( adjustment, floor ((adjustment->value) * adjustment->upper + 0.5) );
739 //     }
741 #ifdef DEBUG_LCMS
742     g_message( "/^^^^^^^^^  %p::_adjustmentChanged()", cs );
743 #endif // DEBUG_LCMS
745      ColorICCSelector* iccSelector = (ColorICCSelector*)(SP_COLOR_SELECTOR(cs)->base);
746      if (iccSelector->_updating) {
747          return;
748      }
750      iccSelector->_updating = TRUE;
752      gint match = -1;
754      SPColor newColor( iccSelector->_color );
755      gfloat scaled = ColorScales::getScaled( iccSelector->_adj );
756      if ( iccSelector->_adj == adjustment ) {
757 #ifdef DEBUG_LCMS
758          g_message("ALPHA");
759 #endif // DEBUG_LCMS
760      } else {
761 #if ENABLE_LCMS
762          for ( guint i = 0; i < iccSelector->_fooCount; i++ ) {
763              if ( iccSelector->_fooAdj[i] == adjustment ) {
764                  match = i;
765                  break;
766              }
767          }
768          if ( match >= 0 ) {
769 #ifdef DEBUG_LCMS
770              g_message(" channel %d", match );
771 #endif // DEBUG_LCMS
772          }
775          icUInt16Number tmp[4];
776          for ( guint i = 0; i < 4; i++ ) {
777              tmp[i] = ColorScales::getScaled( iccSelector->_fooAdj[i] ) * 0x0ffff;
778          }
779          guchar post[4] = {0,0,0,0};
781          cmsDoTransform( iccSelector->_prof->getTransfToSRGB8(), tmp, post, 1 );
783          SPColor other( SP_RGBA32_U_COMPOSE(post[0], post[1], post[2], 255) );
784          other.icc = new SVGICCColor();
785          if ( iccSelector->_color.icc ) {
786              other.icc->colorProfile = iccSelector->_color.icc->colorProfile;
787          }
789          guint32 prior = iccSelector->_color.toRGBA32(255);
790          guint32 newer = other.toRGBA32(255);
792          if ( prior != newer ) {
793 #ifdef DEBUG_LCMS
794              g_message("Transformed color from 0x%08x to 0x%08x", prior, newer );
795              g_message("      ~~~~ FLIP");
796 #endif // DEBUG_LCMS
797              newColor = other;
798              newColor.icc->colors.clear();
799              for ( guint i = 0; i < iccSelector->_profChannelCount; i++ ) {
800                  gdouble val = ColorScales::getScaled( iccSelector->_fooAdj[i] );
801                  if ( iccSelector->_fooScales ) {
802                      val *= iccSelector->_fooScales[i];
803                      if ( iccSelector->_fooScales[i] == 256 ) {
804                          val -= 128;
805                      }
806                  }
807                  newColor.icc->colors.push_back( val );
808              }
809          }
810 #endif // ENABLE_LCMS
811      }
812      iccSelector->_updateInternals( newColor, scaled, iccSelector->_dragging );
813      iccSelector->_updateSliders( match );
815      iccSelector->_updating = FALSE;
816 #ifdef DEBUG_LCMS
817      g_message( "\\_________  %p::_adjustmentChanged()", cs );
818 #endif // DEBUG_LCMS
821 void ColorICCSelector::_sliderGrabbed( SPColorSlider *slider, SPColorICCSelector *cs )
823     (void)slider;
824     (void)cs;
825 //    ColorICCSelector* iccSelector = (ColorICCSelector*)(SP_COLOR_SELECTOR(cs)->base);
826 //     if (!iccSelector->_dragging) {
827 //         iccSelector->_dragging = TRUE;
828 //         iccSelector->_grabbed();
829 //         iccSelector->_updateInternals( iccSelector->_color, ColorScales::getScaled( iccSelector->_adj ), iccSelector->_dragging );
830 //     }
833 void ColorICCSelector::_sliderReleased( SPColorSlider *slider, SPColorICCSelector *cs )
835     (void)slider;
836     (void)cs;
837 //     ColorICCSelector* iccSelector = (ColorICCSelector*)(SP_COLOR_SELECTOR(cs)->base);
838 //     if (iccSelector->_dragging) {
839 //         iccSelector->_dragging = FALSE;
840 //         iccSelector->_released();
841 //         iccSelector->_updateInternals( iccSelector->_color, ColorScales::getScaled( iccSelector->_adj ), iccSelector->_dragging );
842 //     }
845 void ColorICCSelector::_sliderChanged( SPColorSlider *slider, SPColorICCSelector *cs )
847     (void)slider;
848     (void)cs;
849 #ifdef DEBUG_LCMS
850     g_message("Changed  %p and %p", slider, cs );
851 #endif // DEBUG_LCMS
852 //     ColorICCSelector* iccSelector = (ColorICCSelector*)(SP_COLOR_SELECTOR(cs)->base);
854 //     iccSelector->_updateInternals( iccSelector->_color, ColorScales::getScaled( iccSelector->_adj ), iccSelector->_dragging );
858 /*
859   Local Variables:
860   mode:c++
861   c-file-style:"stroustrup"
862   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
863   indent-tabs-mode:nil
864   fill-column:99
865   End:
866 */
867 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :