Code

Filters. Experimental duochrome version 2 (new structure, presence parameters and...
[inkscape.git] / src / extension / internal / filter / color.h
1 #ifndef __INKSCAPE_EXTENSION_INTERNAL_FILTER_COLOR_H__
2 #define __INKSCAPE_EXTENSION_INTERNAL_FILTER_COLOR_H__
3 /* Change the 'COLOR' above to be your file name */
5 /*
6  * Copyright (C) 2010 Authors:
7  *   Ivan Louette (filters)
8  *   Nicolas Dufour (UI) <nicoduf@yahoo.fr>
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
12 /* ^^^ Change the copyright to be you and your e-mail address ^^^ */
14 #include "filter.h"
16 #include "extension/internal/clear-n_.h"
17 #include "extension/system.h"
18 #include "extension/extension.h"
20 namespace Inkscape {
21 namespace Extension {
22 namespace Internal {
23 namespace Filter {
25 class Duochrome : public Inkscape::Extension::Internal::Filter::Filter {
26 protected:
27         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
29 public:
30         Duochrome ( ) : Filter() { };
31         virtual ~Duochrome ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
33         static void init (void) {
34                 Inkscape::Extension::build_from_mem(
35                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
36                                 "<name>" N_("Duochrome, custom -EXP-") "</name>\n"
37                                 "<id>org.inkscape.effect.filter.Duochrome</id>\n"
38 // Using color widgets in tabs makes Inkscape crash...
39 //                  "<param name=\"tab\" type=\"notebook\">\n"
40 //                    "<page name=\"Color1\" _gui-text=\"Color 1\">\n"
41                         "<param name=\"fluo\" gui-text=\"" N_("Fluorescence") "\" type=\"boolean\">false</param>\n"
42                         "<param name=\"swapcolors\" gui-text=\"" N_("Swap colors") "\" type=\"boolean\">false</param>\n"
43                         "<_param name=\"header1\" type=\"groupheader\">Color 1</_param>\n"
44                                         "<param name=\"color1\" gui-text=\"" N_("Color 1") "\" type=\"color\">1364325887</param>\n"
45 //                    "</page>\n"
46 //                    "<page name=\"Color2\" _gui-text=\"Color 2\">\n"
47                         "<_param name=\"header2\" type=\"groupheader\">Color 2</_param>\n"
48                                         "<param name=\"color2\" gui-text=\"" N_("Color 2") "\" type=\"color\">-65281</param>\n"
49 //                    "</page>\n"
50 //                "</param>\n"
51                                 "<effect>\n"
52                                         "<object-type>all</object-type>\n"
53                                         "<effects-menu>\n"
54                                                 "<submenu name=\"" N_("Filters") "\">\n"
55                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
56                               "</submenu>\n"
57                                         "</effects-menu>\n"
58                                         "<menu-tip>" N_("Change colors to a two colors palette") "</menu-tip>\n"
59                                 "</effect>\n"
60                         "</inkscape-extension>\n", new Duochrome());
61         };
63 };
65 gchar const *
66 Duochrome::get_filter_text (Inkscape::Extension::Extension * ext)
67 {
68         if (_filter != NULL) g_free((void *)_filter);
70     std::ostringstream a1;
71     std::ostringstream r1;
72     std::ostringstream g1;
73     std::ostringstream b1;
74     std::ostringstream a2;
75     std::ostringstream r2;
76     std::ostringstream g2;
77     std::ostringstream b2;
78     std::ostringstream fluo;
79     std::ostringstream swapc;
81     guint32 color1 = ext->get_param_color("color1");
82     guint32 color2 = ext->get_param_color("color2");
83     bool fluorescence = ext->get_param_bool("fluo");
84     bool swapcolors = ext->get_param_bool("swapcolors");
86     a1 << (color1 & 0xff) / 255.0F;
87     r1 << ((color1 >> 24) & 0xff);
88     g1 << ((color1 >> 16) & 0xff);
89     b1 << ((color1 >>  8) & 0xff);
90     a2 << (color2 & 0xff) / 255.0F;
91     r2 << ((color2 >> 24) & 0xff);
92     g2 << ((color2 >> 16) & 0xff);
93     b2 << ((color2 >>  8) & 0xff);
94     if (fluorescence)
95         fluo << "";
96     else
97         fluo << " in=\"blend\"";
98     if (swapcolors)
99         swapc << "in";
100     else
101         swapc << "out";
103         _filter = g_strdup_printf(
104                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Duochrome, custom -EXP-\">\n"
105             "<feColorMatrix type=\"luminanceToAlpha\" result=\"fbSourceGraphic\" />\n"
106             "<feFlood in=\"fbSourceGraphic\" flood-opacity=\"%s\" flood-color=\"rgb(%s,%s,%s)\" result=\"composite1\" />\n"
107             "<feComposite in=\"composite1\" in2=\"fbSourceGraphic\" operator=\"%s\" result=\"result2\" />\n"
108             "<feComposite in2=\"SourceGraphic\" k2=\"1\" result=\"fbSourceGraphic\" operator=\"arithmetic\" />\n"
109             "<feFlood in=\"fbSourceGraphic\" flood-opacity=\"%s\" flood-color=\"rgb(%s,%s,%s)\" result=\"result2\" />\n"
110             "<feMerge>\n"
111                 "<feMergeNode in=\"result2\" />\n"
112                 "<feMergeNode in=\"fbSourceGraphic\" />\n"
113             "</feMerge>\n"
114             "<feBlend in2=\"fbSourceGraphic\" mode=\"normal\" blend=\"normal\" result=\"blend\" />\n"
115             "<feColorMatrix type=\"matrix\" values=\"2 -1 0 0 0 0 2 -1 0 0 -1 0 2 0 0 0 0 0 1 0 \" />\n"
116             "<feComposite %s in2=\"SourceGraphic\" operator=\"in\" result=\"fbSourceGraphic\" />\n"
117         "</filter>\n", a1.str().c_str(), r1.str().c_str(), g1.str().c_str(), b1.str().c_str(), swapc.str().c_str(), a2.str().c_str(), r2.str().c_str(), g2.str().c_str(), b2.str().c_str(), fluo.str().c_str());
119         return _filter;
120 };
123 class Duochrome2 : public Inkscape::Extension::Internal::Filter::Filter {
124 protected:
125         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
127 public:
128         Duochrome2 ( ) : Filter() { };
129         virtual ~Duochrome2 ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
131         static void init (void) {
132                 Inkscape::Extension::build_from_mem(
133                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
134                                 "<name>" N_("Duochrome2, custom -EXP-") "</name>\n"
135                                 "<id>org.inkscape.effect.filter.Duochrome2</id>\n"
136                         "<param name=\"fluo\" gui-text=\"" N_("Fluorescence level:") "\" type=\"float\" min=\"0\" max=\"2\">0</param>\n"
137                         "<param name=\"pres1\" gui-text=\"" N_("Presence 1:") "\" type=\"float\" min=\"0\" max=\"2\">1</param>\n"
138                         "<param name=\"pres2\" gui-text=\"" N_("Presence 2:") "\" type=\"float\" min=\"0\" max=\"2\">1</param>\n"
139                         "<param name=\"swapcolors\" gui-text=\"" N_("Swap colors") "\" type=\"boolean\">false</param>\n"
140                         "<_param name=\"header1\" type=\"groupheader\">Color 1</_param>\n"
141                                         "<param name=\"color1\" gui-text=\"" N_("Color 1") "\" type=\"color\">1364325887</param>\n"
142                         "<_param name=\"header2\" type=\"groupheader\">Color 2</_param>\n"
143                                         "<param name=\"color2\" gui-text=\"" N_("Color 2") "\" type=\"color\">-65281</param>\n"
144                                 "<effect>\n"
145                                         "<object-type>all</object-type>\n"
146                                         "<effects-menu>\n"
147                                                 "<submenu name=\"" N_("Filters") "\">\n"
148                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
149                               "</submenu>\n"
150                                         "</effects-menu>\n"
151                                         "<menu-tip>" N_("Convert luminance values to a duochrome palette") "</menu-tip>\n"
152                                 "</effect>\n"
153                         "</inkscape-extension>\n", new Duochrome2());
154         };
156 };
158 gchar const *
159 Duochrome2::get_filter_text (Inkscape::Extension::Extension * ext)
161         if (_filter != NULL) g_free((void *)_filter);
163     std::ostringstream a1;
164     std::ostringstream r1;
165     std::ostringstream g1;
166     std::ostringstream b1;
167     std::ostringstream a2;
168     std::ostringstream r2;
169     std::ostringstream g2;
170     std::ostringstream b2;
171     std::ostringstream fluo;
172     std::ostringstream pres1;
173     std::ostringstream pres2;
174     std::ostringstream swap1;
175     std::ostringstream swap2;
177     guint32 color1 = ext->get_param_color("color1");
178     guint32 color2 = ext->get_param_color("color2");
179     float fluorescence = ext->get_param_float("fluo");
180     float presence1 = ext->get_param_float("pres1");
181     float presence2 = ext->get_param_float("pres2");
182     bool swapcolors = ext->get_param_bool("swapcolors");
184     a1 << (color1 & 0xff) / 255.0F;
185     r1 << ((color1 >> 24) & 0xff);
186     g1 << ((color1 >> 16) & 0xff);
187     b1 << ((color1 >>  8) & 0xff);
188     a2 << (color2 & 0xff) / 255.0F;
189     r2 << ((color2 >> 24) & 0xff);
190     g2 << ((color2 >> 16) & 0xff);
191     b2 << ((color2 >>  8) & 0xff);
192     fluo << fluorescence;
193     pres1 << presence1;
194     pres2 << presence2;
196     if (swapcolors) {
197         swap1 << "in";
198         swap2 << "out";
199     } else {
200         swap2 << "in";
201         swap1 << "out";
202     }
204         _filter = g_strdup_printf(
205                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Duochrome2, custom -EXP-\">\n"
206             "<feColorMatrix type=\"luminanceToAlpha\" result=\"colormatrix1\" />\n"
207             "<feFlood flood-opacity=\"%s\" flood-color=\"rgb(%s,%s,%s)\" result=\"flood1\" />\n"
208             "<feComposite in2=\"colormatrix1\" operator=\"%s\" result=\"composite1\" />\n"
209             "<feFlood in=\"colormatrix1\" flood-opacity=\"%s\" flood-color=\"rgb(%s,%s,%s)\" result=\"flood2\" />\n"
210             "<feComposite in2=\"colormatrix1\" result=\"composite2\" operator=\"%s\" />\n"
211             "<feComposite in=\"composite2\" in2=\"composite1\" k2=\"%s\"  k3=\"%s\" operator=\"arithmetic\" result=\"composite3\" />\n"
212             "<feColorMatrix in=\"composite3\" type=\"matrix\" values=\"2 -1 0 0 0 0 2 -1 0 0 -1 0 2 0 0 0 0 0 1 0 \" result=\"colormatrix2\" />\n"
213             "<feComposite in=\"colormatrix2\" in2=\"composite3\" operator=\"arithmetic\" k2=\"%s\" result=\"composite4\" />\n"
214             "<feBlend in=\"composite4\" in2=\"composite3\" blend=\"normal\" mode=\"normal\" result=\"blend\" />\n"
215             "<feComposite in2=\"SourceGraphic\" operator=\"in\" />\n"
216         "</filter>\n", a1.str().c_str(), r1.str().c_str(), g1.str().c_str(), b1.str().c_str(), swap1.str().c_str(), a2.str().c_str(), r2.str().c_str(), g2.str().c_str(), b2.str().c_str(), swap2.str().c_str(), pres2.str().c_str(), pres1.str().c_str(), fluo.str().c_str());
218         return _filter;
219 };
220 }; /* namespace Filter */
221 }; /* namespace Internal */
222 }; /* namespace Extension */
223 }; /* namespace Inkscape */
225 /* Change the 'COLOR' below to be your file name */
226 #endif /* __INKSCAPE_EXTENSION_INTERNAL_FILTER_COLOR_H__ */