summaryrefslogtreecommitdiffstats
path: root/src/live_effects/parameter/colorpicker.cpp
blob: cf9243e37f6c4253194e4ea4bc4bdfd3953f5134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "colorpicker.h"

#include <gtkmm.h>

#include "color.h"
#include "document-undo.h"
#include "document.h"
#include "inkscape.h"

#include "live_effects/effect.h"
#include "live_effects/parameter/colorpicker.h"

#include "svg/stringstream.h"
#include "svg/svg-color.h"
#include "svg/svg.h"

#include "ui/icon-names.h"
#include "ui/widget/registered-widget.h"

#include <glibmm/i18n.h>

namespace Inkscape {

namespace LivePathEffect {

ColorPickerParam::ColorPickerParam( const Glib::ustring& label, const Glib::ustring& tip,
                      const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
                      Effect* effect, const guint32 default_color )
    : Parameter(label, tip, key, wr, effect),
      value(default_color),
      defvalue(default_color)
{

}

void
ColorPickerParam::param_set_default()
{
    param_setValue(defvalue);
}

static guint32 sp_read_color_alpha(gchar const *str, guint32 def)
{
    guint32 val = 0;
    if (str == nullptr) return def;
    while ((*str <= ' ') && *str) str++;
    if (!*str) return def;

    if (str[0] == '#') {
        gint i;
        for (i = 1; str[i]; i++) {
            int hexval;
            if (str[i] >= '0' && str[i] <= '9')
                hexval = str[i] - '0';
            else if (str[i] >= 'A' && str[i] <= 'F')
                hexval = str[i] - 'A' + 10;
            else if (str[i] >= 'a' && str[i] <= 'f')
                hexval = str[i] - 'a' + 10;
            else
                break;
            val = (val << 4) + hexval;
        }
        if (i != 1 + 8) {
            return def;
        }
    }
    return val;
}

void 
ColorPickerParam::param_update_default(const gchar * default_value)
{
    defvalue = sp_read_color_alpha(default_value, 0x000000ff);
}

bool
ColorPickerParam::param_readSVGValue(const gchar * strvalue)
{
    param_setValue(sp_read_color_alpha(strvalue, 0x000000ff));
    return true;
}

Glib::ustring
ColorPickerParam::param_getSVGValue() const
{
    gchar c[32];
    sprintf(c, "#%08x", value);
    return c;
}

Glib::ustring
ColorPickerParam::param_getDefaultSVGValue() const
{
    gchar c[32];
    sprintf(c, "#%08x", defvalue);
    return c;
}

Gtk::Widget *
ColorPickerParam::param_newWidget()
{
    Gtk::Box *hbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));

    hbox->set_border_width(5);
    hbox->set_homogeneous(false);
    hbox->set_spacing(2);
    Inkscape::UI::Widget::RegisteredColorPicker * colorpickerwdg =
        new Inkscape::UI::Widget::RegisteredColorPicker( param_label,
                                                         param_label,
                                                         param_tooltip,
                                                         param_key,
                                                         param_key + "_opacity_LPE",
                                                        *param_wr,
                                                         param_effect->getRepr(),
                                                         param_effect->getSPDoc() );
    SPDocument *document = param_effect->getSPDoc();
    bool saved = DocumentUndo::getUndoSensitive(document);
    DocumentUndo::setUndoSensitive(document, false);
    colorpickerwdg->setRgba32(value);
    DocumentUndo::setUndoSensitive(document, saved);
    colorpickerwdg->set_undo_parameters(_("Change color button parameter"), INKSCAPE_ICON("dialog-path-effects"));
    hbox->pack_start(*dynamic_cast<Gtk::Widget *> (colorpickerwdg), true, true);
    return dynamic_cast<Gtk::Widget *> (hbox);
}

void
ColorPickerParam::param_setValue(const guint32 newvalue)
{
    value = newvalue;
}


} /* namespace LivePathEffect */

} /* namespace Inkscape */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :