]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/graphic-helpers: add a few cairo pattern helpers
authorJohn Lindgren <john@jlindgren.net>
Thu, 29 May 2025 16:31:41 +0000 (12:31 -0400)
committerJohn Lindgren <john@jlindgren.net>
Wed, 18 Jun 2025 19:48:24 +0000 (15:48 -0400)
include/common/graphic-helpers.h
src/common/graphic-helpers.c

index 1b751b2516a3664de2e873d68a0a52d534635b43..3fdde6adec7607d265d563d45b7f413f07858e02 100644 (file)
@@ -12,6 +12,19 @@ struct wlr_fbox;
  */
 void set_cairo_color(cairo_t *cairo, const float *color);
 
+/* Creates a solid color cairo pattern from premultipled RGBA */
+cairo_pattern_t *color_to_pattern(const float *color);
+
+bool is_pattern_opaque(cairo_pattern_t *pattern);
+
+/* Like zfree() but for a cairo_pattern_t */
+#define zfree_pattern(ptr) do { \
+       if (ptr) { \
+               cairo_pattern_destroy(ptr); \
+               (ptr) = NULL; \
+       } \
+} while (0)
+
 /* Draws a border with a specified line width */
 void draw_cairo_border(cairo_t *cairo, struct wlr_fbox fbox, double line_width);
 
index 08817cb50559ec7cbe585ac423e6d2ea4fd9df4d..7a8af89a5ae59c369264cdf244e072c3aaecad5f 100644 (file)
@@ -44,6 +44,54 @@ set_cairo_color(cairo_t *cairo, const float *c)
                c[2] / alpha, alpha);
 }
 
+cairo_pattern_t *
+color_to_pattern(const float *c)
+{
+       float alpha = c[3];
+
+       if (alpha == 0.0f) {
+               return cairo_pattern_create_rgba(0, 0, 0, 0);
+       }
+
+       return cairo_pattern_create_rgba(
+               c[0] / alpha, c[1] / alpha, c[2] / alpha, alpha);
+}
+
+/*
+ * This is used as an optimization in font rendering and errs on the
+ * side of returning false (not opaque) for unknown pattern types.
+ *
+ * The 0.999 alpha threshold was chosen to be greater than 254/255
+ * (about 0.996) while leaving some margin for rounding errors.
+ */
+bool
+is_pattern_opaque(cairo_pattern_t *pattern)
+{
+       double alpha = 0;
+       int stops = 0;
+
+       /* solid color? */
+       if (cairo_pattern_get_rgba(pattern, NULL, NULL, NULL, &alpha)
+                       == CAIRO_STATUS_SUCCESS) {
+               return (alpha >= 0.999);
+       }
+
+       /* gradient? */
+       if (cairo_pattern_get_color_stop_count(pattern, &stops)
+                       == CAIRO_STATUS_SUCCESS) {
+               for (int s = 0; s < stops; s++) {
+                       cairo_pattern_get_color_stop_rgba(pattern, s,
+                               NULL, NULL, NULL, NULL, &alpha);
+                       if (alpha < 0.999) {
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       return false; /* unknown pattern type */
+}
+
 static int
 compare_xcolor_entry(const void *a, const void *b)
 {