Re: composites circle
Uli Schlachter <[email protected]>
| Newsgroups | gmane.comp.lib.cairo |
|---|---|
| Message-ID | <[email protected]> |
Am 13.01.2016 um 10:52 schrieb Alex Vazquez: > Hi!! > I am trying replicate the next effect with cairo > http://postimg.org/image/ieohdu29t/ > I have tested with different operator but i can't replicate the effect. > Is it possible to effect with cairo ? > Code > http://pasted.co/3c081a75 Some random things with operators is attached. However, perhaps it makes more sense to try to do this with cairo_mask() / cairo_mask_surface()... Uli -- Bruce Schneier can read and understand Perl programs. -- cairo mailing list [email protected] http://lists.cairographics.org/mailman/listinfo/cairo
t.c
(text/x-csrc, 2.6 KB)
#include <cairo.h>
#include <math.h>
#define WIDTH 75
#define HEIGHT 170
static void circle(cairo_t *cr, double xc, double yc, double radius)
{
cairo_arc(cr, xc, yc, radius, 0, 2 * M_PI);
}
static void circle_negative(cairo_t *cr, double xc, double yc, double radius)
{
cairo_arc_negative(cr, xc, yc, radius, 0, -2 * M_PI);
}
int main()
{
cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);
cairo_t *cr = cairo_create(s);
/* Background */
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
/* Version 1, draw red, then white, then transparent red ontop */
circle(cr, 25, 25, 25);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_fill(cr);
circle(cr, 50, 25, 25);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_fill(cr);
circle(cr, 25, 25, 25);
cairo_set_source_rgba(cr, 1, 0, 0, 0.25);
cairo_fill(cr);
/* Version 2, intersect some clips to draw each part separately */
cairo_save(cr);
cairo_translate(cr, 0, 60);
/* Draw the red part */
cairo_save(cr);
circle(cr, 25, 25, 25);
cairo_clip_preserve(cr);
circle_negative(cr, 50, 25, 25);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_fill(cr);
cairo_restore(cr);
/* Draw the white part */
cairo_save(cr);
circle(cr, 50, 25, 25);
cairo_clip_preserve(cr);
circle_negative(cr, 25, 25, 25);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_fill(cr);
cairo_restore(cr);
/* Draw the redish part */
circle(cr, 25, 25, 25);
cairo_clip(cr);
circle(cr, 50, 25, 25);
cairo_clip(cr);
cairo_set_source_rgb(cr, 1, 0.5, 0.5);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
cairo_restore(cr);
/* Version 3, fun with operators (the overlap is made semi-transparent
* and the red is then drawn below) */
cairo_save(cr);
cairo_translate(cr, 0, 120);
/* Draw white circle */
circle(cr, 50, 25, 25);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_fill(cr);
/* Clip to red circle plus overlap */
circle(cr, 25, 25, 25);
cairo_clip_preserve(cr);
/* Make red circle fully transparent */
circle_negative(cr, 50, 25, 25);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_fill(cr);
/* Make overlap partly transparent (only alpha batters here) */
cairo_set_source_rgba(cr, 1, 1, 1, 0.50);
cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OUT);
circle(cr, 50, 25, 25);
cairo_fill(cr);
/* Now draw the red circle below what we got so far */
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OVER);
cairo_paint(cr);
cairo_restore(cr);
cairo_destroy(cr);
cairo_surface_write_to_png(s, "out.png");
cairo_surface_destroy(s);
return 0;
}