[cairo] Operator for converting translucent graphic to opaque?

Stefan Salewski mail at ssalewski.de
Mon Jun 20 09:46:11 PDT 2011


I have a routine which draws colored translucent graphics to an empty
intermediate surface, which I copy to my destination later. Is it
possible to convert this intermediate surface with colored translucent
graphics to solid, fully opaque graphics with all the same color? (That
may be useful for generating simple shadow effects.)

Here is a C example, which draws a blue translucent line. Operator
CAIRO_OPERATOR_IN converts that line to translucent grey, but I want
opaque black.

#include <cairo.h>

int main (int argc, char *argv[])
{
  cairo_surface_t *surface;
  cairo_t *cr;
  surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 400, 200);
  cr = cairo_create(surface);
  cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
  cairo_set_line_width(cr, 25);
  cairo_move_to(cr, 100.0, 100.0);
  cairo_line_to(cr, 300.0, 100.0);
  cairo_stroke(cr);
  cairo_set_operator(cr, CAIRO_OPERATOR_IN);
  cairo_set_source_rgba(cr, 0, 0, 0, 1);
  cairo_paint(cr);
  cairo_surface_write_to_png(surface, "image.png");
  cairo_destroy(cr);
  cairo_surface_destroy(surface);
  return 0;
}

Thanks,

Stefan Salewski




More information about the cairo mailing list