-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
349 lines (281 loc) · 10.8 KB
/
main.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include <gtk/gtk.h>
#include <math.h>
#define PAINT_GRADIENT 1
typedef struct color_t {
short unsigned int r, g, b, a;
} color_t;
typedef struct point_t {
double x, y;
} point_t;
static double rad(double deg) {
return deg * (M_PI / 180);
}
color_t aqua = {r: 100, g: 255, b: 255, a: 255};
color_t black = {r: 0, g: 0, b: 0, a: 255};
color_t blue = {r: 0, g: 0, b: 255, a: 255};
color_t gold = {r: 255, g: 215, b: 0, a: 255};
color_t orange = {r: 255, g: 128, b: 0, a: 255};
color_t green = {r: 0, g: 128, b: 0, a: 255};
color_t lime = {r: 0, g: 255, b: 0, a: 255};
color_t magenta = {r: 255, g: 0, b: 255, a: 255};
color_t middle = {r: 100, g: 40, b: 20, a: 255};
color_t pink = {r: 255, g: 20, b: 90, a: 255};
color_t red = {r: 255, g: 0, b: 0, a: 255};
color_t yellow = {r: 255, g: 255, b: 0, a: 255};
static color_t
interpolate (color_t from, color_t to)
{
color_t ret = {
r: from.r + (to.r - from.r) * 0.5,
g: from.g + (to.g - from.g) * 0.5,
b: from.b + (to.b - from.b) * 0.5,
a: from.a + (to.a - from.a) * 0.5,
};
return ret;
}
/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;
static void activate (GApplication *app, gpointer user_data);
static void close_window (void);
static gboolean draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data);
static gboolean configure_event_cb (GtkWidget *widget, GdkEventConfigure *event, gpointer data);
static void clear_surface (void);
static void conic_sector (cairo_pattern_t *pattern, double cx, double cy, double size,
double degStart, double degEnd, color_t from, color_t to);
static void conic_gradient(cairo_t *cr, int width, int height, const color_t *colorStops, size_t nColorStops);
static void create_gouraud_gradient(cairo_pattern_t *pattern);
static void create_coons_gradient(cairo_pattern_t *pattern);
static void create_conic_sector_gradient(cairo_pattern_t *pattern);
static void create_conic_sector_gradient2(cairo_pattern_t *pattern);
int main(int argc, char *argv[])
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gnome.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return 0;
}
static void activate (GApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *drawing_area;
int w = 800, h = 800;
window = gtk_application_window_new (GTK_APPLICATION (app));
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
gtk_window_set_default_size (GTK_WINDOW (window), w, h);
g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_container_add (GTK_CONTAINER (window), frame);
drawing_area = gtk_drawing_area_new ();
/* Set a minimum size. */
gtk_widget_set_size_request (drawing_area, w, h);
gtk_container_add (GTK_CONTAINER (frame), drawing_area);
/* Signals used to handle the backing surface */
g_signal_connect (drawing_area, "draw",
G_CALLBACK (draw_cb), NULL);
g_signal_connect (drawing_area,"configure-event",
G_CALLBACK (configure_event_cb), NULL);
gtk_widget_set_events (drawing_area, gtk_widget_get_events (drawing_area));
gtk_widget_show_all (window);
}
static void
close_window (void)
{
if (surface)
cairo_surface_destroy(surface);
}
static void mesh_pattern_set_corner_color_rgb (cairo_pattern_t *pattern, int id, color_t c)
{
double r, g, b, a;
r = c.r / 255.0f;
g = c.g / 255.0f;
b = c.b / 255.0f;
a = c.a / 255.0f;
// printf("%.2f, %.2f, %.2f, %.2f\n", r, g, b, a);
cairo_mesh_pattern_set_corner_color_rgba (pattern, id, r, g, b, 1.0);
}
static void print_point(point_t p)
{
printf("x: %.4f; y: %.4f\n", p.x, p.y);
}
static void
paint_surface (cairo_t *cr, double w, double h)
{
cairo_move_to (cr, 0, 0);
cairo_line_to (cr, w, 0);
cairo_line_to (cr, w, h);
cairo_line_to (cr, 0, h);
cairo_fill (cr);
}
/* Redraw the screen from the surface. Note that the ::draw
* signal receives a ready-to-be-used cairo_t that is already
* clipped to only draw the exposed areas of the widget
*/
static gboolean
draw_cb (GtkWidget *widget,
cairo_t *cr,
gpointer data)
{
int w = 800, h = 800;
// color_t colorStops[] = { red, yellow, lime, aqua, blue, magenta, red };
// color_t colorStops[] = { red, gold, lime, black };
color_t colorStops[] = { green,black,orange,red};
cairo_pattern_t * pattern = cairo_pattern_create_mesh ();
create_conic_sector_gradient(pattern);
// create_coons_gradient(pattern);
// create_gouraud_gradient(pattern);
cairo_set_source(cr, pattern);
paint_surface (cr, w, h);
cairo_stroke(cr);
return FALSE;
}
static void conic_sector (cairo_pattern_t *pattern, double cx, double cy, double size,
double degStart, double degEnd, color_t from, color_t to)
{
const int degOffset = 90;
double r = sqrt(2) * size / 2;
degStart = rad(degStart - degOffset);
degEnd = rad(degEnd - degOffset);
double f = 4 * tan((degEnd - degStart) / 4) / 3;
point_t p0 = {
x: cx + (r * cos(degStart)),
y: cy + (r * sin(degStart))
};
point_t p1 = {
x: cx + (r * cos(degStart)) - f * (r * sin(degStart)),
y: cy + (r * sin(degStart)) + f * (r * cos(degStart))
};
point_t p2 = {
x: cx + (r * cos(degEnd)) + f * (r * sin(degEnd)),
y: cy + (r * sin(degEnd)) - f * (r * cos(degEnd))
};
point_t p3 = {
x: cx + (r * cos(degEnd)),
y: cy + (r * sin(degEnd))
};
cairo_mesh_pattern_begin_patch (pattern);
cairo_mesh_pattern_move_to (pattern, cx, cy);
cairo_mesh_pattern_line_to (pattern, p0.x, p0.y);
cairo_mesh_pattern_curve_to (pattern, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
cairo_mesh_pattern_line_to (pattern, cx, cy);
mesh_pattern_set_corner_color_rgb (pattern, 0, from);
mesh_pattern_set_corner_color_rgb (pattern, 1, from);
mesh_pattern_set_corner_color_rgb (pattern, 2, to);
mesh_pattern_set_corner_color_rgb (pattern, 3, to);
cairo_mesh_pattern_end_patch (pattern);
}
static void conic_gradient(cairo_t *cr, int width, int height, const color_t *colorStops, size_t nColorStops)
{
cairo_pattern_t * pattern = cairo_pattern_create_mesh ();
color_t from, to;
double cx = width / 2, cy = height / 2;
double size = fmax(width, height);
if (nColorStops == 2) {
from = colorStops[0], to = colorStops[1];
color_t middle = interpolate(from, to);
conic_sector (pattern, cx, cy, size, 0, 180, from, middle);
conic_sector (pattern, cx, cy, size, 180, 360, middle, to);
} else {
double degStride = 360 / (nColorStops - 1);
degStride = 90;
double degStart = 0, degEnd = degStart + degStride;
double r = 1 / 4;
double c = 0.551915024494;
for (size_t i = 0; i < nColorStops; i++) {
from = colorStops[i], to = colorStops[i + 1];
double f = 4 * tan((degEnd - degStart) / 4) / 3;
// printf("f: %.4f\n", f);
double xo = cx + (r * cos(degStart)) + c * (r * sin(degStart));
double yo = cy + (r * sin(degStart)) - c * (r * cos(degStart));
conic_sector (pattern, xo, yo, size, degStart, degEnd, from, from);
degStart = degEnd, degEnd = degStart + degStride;
}
}
cairo_set_source(cr, pattern);
}
static void
create_gouraud_gradient(cairo_pattern_t *pattern)
{
cairo_mesh_pattern_begin_patch (pattern);
cairo_mesh_pattern_move_to (pattern, 0, 0);
cairo_mesh_pattern_line_to (pattern, 200, 200);
cairo_mesh_pattern_line_to (pattern, 0, 200);
cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0); // red
cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 0, 1, 0); // green
cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 0, 0, 1); // blue
cairo_mesh_pattern_end_patch (pattern);
}
static void
create_coons_gradient(cairo_pattern_t *pattern)
{
cairo_mesh_pattern_begin_patch (pattern);
cairo_mesh_pattern_move_to (pattern, 45, 12);
cairo_mesh_pattern_curve_to(pattern, 69, 24, 173, -15, 115, 50);
cairo_mesh_pattern_curve_to(pattern, 127, 66, 174, 47, 148, 104);
cairo_mesh_pattern_curve_to(pattern, 65, 58, 70, 69, 18, 103);
cairo_mesh_pattern_curve_to(pattern, 42, 43, 63, 45, 45, 12);
cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0); // red
cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 0, 1, 0); // green
cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 0, 0, 1); // blue
cairo_mesh_pattern_set_corner_color_rgb (pattern, 3, 1, 1, 0); // yellow
cairo_mesh_pattern_end_patch (pattern);
}
static void
create_conic_sector_gradient(cairo_pattern_t *pattern)
{
cairo_mesh_pattern_begin_patch(pattern);
cairo_mesh_pattern_move_to(pattern, 0, 200);
cairo_mesh_pattern_line_to(pattern, 0, 0);
cairo_mesh_pattern_curve_to(pattern, 110.4, 0, 200, 110.4, 200, 200);
cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0); // red
cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 0, 1, 0); // green
cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 0, 0, 1); // blue
cairo_mesh_pattern_set_corner_color_rgb (pattern, 3, 1, 1, 0); // yellow
cairo_mesh_pattern_end_patch(pattern);
}
static void
create_conic_sector_gradient2(cairo_pattern_t *pattern)
{
cairo_mesh_pattern_begin_patch(pattern);
cairo_mesh_pattern_move_to(pattern, 0, 200);
cairo_mesh_pattern_line_to(pattern, 0, 0);
cairo_mesh_pattern_curve_to(pattern, 110.4, 0, 200, 110.4, 200, 200);
cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0); // red
cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 1, 0, 0); // red
cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 1, 1, 0); // yellow
cairo_mesh_pattern_set_corner_color_rgb (pattern, 3, 1, 1, 0); // yellow
cairo_mesh_pattern_end_patch(pattern);
}
/* Create a new surface of the appropriate size to store our scribbles */
static gboolean
configure_event_cb (GtkWidget *widget,
GdkEventConfigure *event,
gpointer data)
{
if (surface)
cairo_surface_destroy (surface);
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
/* Initialize the surface to white */
clear_surface ();
/* We've handled the configure event, no need for further processing. */
return TRUE;
}
static void
clear_surface (void)
{
cairo_t *cr;
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr);
cairo_destroy (cr);
}