1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
5 * Copyright (C) 2007 Sven Neumann <sven@gimp.org>
6 * 2010-2012 Michael Natterer <mitch@gimp.org>
8 * This library is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 3 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see
20 * <https://www.gnu.org/licenses/>.
29 #include "libgimpbase/gimpbase.h"
31 #include "gimpcolortypes.h"
33 #include "gimpcairo.h"
39 * @short_description: Color utility functions for cairo
41 * Utility functions that make cairo easier to use with GIMP color
47 * gimp_cairo_set_source_rgb:
49 * @color: GimpRGB color
51 * Sets the source pattern within @cr to the solid opaque color
52 * described by @color.
54 * This function calls cairo_set_source_rgb() for you.
59 gimp_cairo_set_source_rgb (cairo_t
*cr
,
62 cairo_set_source_rgb (cr
, color
->r
, color
->g
, color
->b
);
66 * gimp_cairo_set_source_rgba:
68 * @color: GimpRGB color
70 * Sets the source pattern within @cr to the solid translucent color
71 * described by @color.
73 * This function calls cairo_set_source_rgba() for you.
78 gimp_cairo_set_source_rgba (cairo_t
*cr
,
81 cairo_set_source_rgba (cr
, color
->r
, color
->g
, color
->b
, color
->a
);
85 * gimp_cairo_checkerboard_create:
88 * @light: light check color or %NULL to use the default light gray
89 * @dark: dark check color or %NULL to use the default dark gray
91 * Create a repeating checkerboard pattern.
93 * Return value: a new Cairo pattern that can be used as a source on @cr.
98 gimp_cairo_checkerboard_create (cairo_t
*cr
,
100 const GimpRGB
*light
,
104 cairo_surface_t
*surface
;
105 cairo_pattern_t
*pattern
;
107 g_return_val_if_fail (cr
!= NULL
, NULL
);
108 g_return_val_if_fail (size
> 0, NULL
);
110 surface
= cairo_surface_create_similar (cairo_get_target (cr
),
113 context
= cairo_create (surface
);
116 gimp_cairo_set_source_rgb (context
, light
);
118 cairo_set_source_rgb (context
,
119 GIMP_CHECK_LIGHT
, GIMP_CHECK_LIGHT
, GIMP_CHECK_LIGHT
);
121 cairo_rectangle (context
, 0, 0, size
, size
);
122 cairo_rectangle (context
, size
, size
, size
, size
);
123 cairo_fill (context
);
126 gimp_cairo_set_source_rgb (context
, dark
);
128 cairo_set_source_rgb (context
,
129 GIMP_CHECK_DARK
, GIMP_CHECK_DARK
, GIMP_CHECK_DARK
);
131 cairo_rectangle (context
, 0, size
, size
, size
);
132 cairo_rectangle (context
, size
, 0, size
, size
);
133 cairo_fill (context
);
135 cairo_destroy (context
);
137 pattern
= cairo_pattern_create_for_surface (surface
);
138 cairo_pattern_set_extend (pattern
, CAIRO_EXTEND_REPEAT
);
140 cairo_surface_destroy (surface
);
146 * gimp_cairo_surface_get_format:
147 * @surface: a Cairo surface
149 * This function returns a #Babl format that corresponds to @surface's
152 * Return value: the #Babl format of @surface.
157 gimp_cairo_surface_get_format (cairo_surface_t
*surface
)
159 g_return_val_if_fail (surface
!= NULL
, NULL
);
160 g_return_val_if_fail (cairo_surface_get_type (surface
) ==
161 CAIRO_SURFACE_TYPE_IMAGE
, NULL
);
163 switch (cairo_image_surface_get_format (surface
))
165 case CAIRO_FORMAT_RGB24
: return babl_format ("cairo-RGB24");
166 case CAIRO_FORMAT_ARGB32
: return babl_format ("cairo-ARGB32");
167 case CAIRO_FORMAT_A8
: return babl_format ("cairo-A8");
173 g_return_val_if_reached (NULL
);
177 * gimp_cairo_surface_create_buffer:
178 * @surface: a Cairo surface
180 * This function returns a #GeglBuffer which wraps @surface's pixels.
181 * It must only be called on image surfaces, calling it on other surface
184 * Return value: a #GeglBuffer
189 gimp_cairo_surface_create_buffer (cairo_surface_t
*surface
)
195 g_return_val_if_fail (surface
!= NULL
, NULL
);
196 g_return_val_if_fail (cairo_surface_get_type (surface
) ==
197 CAIRO_SURFACE_TYPE_IMAGE
, NULL
);
199 format
= gimp_cairo_surface_get_format (surface
);
200 width
= cairo_image_surface_get_width (surface
);
201 height
= cairo_image_surface_get_height (surface
);
204 gegl_buffer_linear_new_from_data (cairo_image_surface_get_data (surface
),
206 GEGL_RECTANGLE (0, 0, width
, height
),
207 cairo_image_surface_get_stride (surface
),
208 (GDestroyNotify
) cairo_surface_destroy
,
209 cairo_surface_reference (surface
));