Example: Gradients:
public static int main (string[] args) {
// Create a context:
Cairo.ImageSurface surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, 256, 256);
Cairo.Context context = new Cairo.Context (surface);
// Rectangle:
Cairo.Pattern pat1 = new Cairo.Pattern.linear (0.0, 0.0, 0.0, 256.0);
pat1.add_color_stop_rgba (1, 0, 0, 0, 1);
pat1.add_color_stop_rgba (0, 1, 1, 1, 1);
context.rectangle (0, 0, 256, 256);
context.set_source (pat1);
context.fill ();
// Arc:
Cairo.Pattern pat2 = new Cairo.Pattern.radial (115.2, 102.4, 25.6, 102.4, 102.4, 128.0);
pat2.add_color_stop_rgba (0, 1, 1, 1, 1);
pat2.add_color_stop_rgba (1, 0, 0, 0, 1);
context.set_source (pat2);
context.arc (128.0, 128.0, 76.8, 0, 2 * Math.PI);
context.fill ();
// Save the image:
surface.write_to_png ("img.png");
return 0;
}
valac --pkg cairo gradient.vala
Example: Pattern:
public static int main (string[] args) {
// Create a context:
Cairo.ImageSurface surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, 256, 256);
Cairo.Context context = new Cairo.Context (surface);
// Content:
string image_path = Path.build_filename (Path.get_dirname (args[0]) , "romedalen.png");
Cairo.ImageSurface image = new Cairo.ImageSurface.from_png (image_path);
int w = image.get_width ();
int h = image.get_height ();
Cairo.Pattern pattern = new Cairo.Pattern.for_surface (image);
pattern.set_extend (Cairo.Extend.REPEAT);
context.translate (128.0, 128.0);
context.rotate (Math.PI / 4);
context.scale (1 / Math.sqrt (2), 1 / Math.sqrt (2));
context.translate (-128.0, -128.0);
Cairo.Matrix matrix = Cairo.Matrix.identity ();
matrix.scale (w/256.0 * 5.0, h/256.0 * 5.0);
pattern.set_matrix (matrix);
context.set_source (pattern);
context.rectangle (0, 0, 256.0, 256.0);
context.fill ();
// Save the image:
surface.write_to_png ("img.png");
return 0;
}
valac --pkg cairo image-pattern.vala