Commit 0fa8132c6bbfa894e761e14c5ab57063ed34510e

Thomas de Grivel 2023-11-18T14:55:37

cairo demo with gradients

diff --git a/libc3/window/cairo/c3_window_cairo_demo.c b/libc3/window/cairo/c3_window_cairo_demo.c
index 94ecb7a..ce2cd0e 100644
--- a/libc3/window/cairo/c3_window_cairo_demo.c
+++ b/libc3/window/cairo/c3_window_cairo_demo.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 #include <assert.h>
+#include <math.h>
 #include <stdio.h>
 #include <time.h>
 #include <libc3/c3.h>
@@ -81,33 +82,18 @@ bool c3_window_cairo_demo_render (s_window_cairo *window,
   /* background */
   switch (window->sequence_pos) {
   case 0:
-    if (seq->t < 1.0)
-      cairo_set_source_rgb(cr, 1, 1, 1);
-    else if (seq->t < 2.0)
-      cairo_set_source_rgb(cr, 1, 0, 0);
-    else if (seq->t < 3.0)
-      cairo_set_source_rgb(cr, 1, 1, 0);
-    else if (seq->t < 4.0)
-      cairo_set_source_rgb(cr, 0, 1, 0);
-    else if (seq->t < 5.0)
-      cairo_set_source_rgb(cr, 0, 1, 1);
-    else if (seq->t < 6.0)
-      cairo_set_source_rgb(cr, 0, 0, 1);
-    else if (seq->t < 7.0)
-      cairo_set_source_rgb(cr, 1, 0, 1);
-    else
-      cairo_set_source_rgb(cr, 0, 0, 0);
+    c3_window_cairo_demo_render_bg_rect(window, cr, seq);
     break;
   default:
     cairo_set_source_rgb(cr, 1, 1, 1);
+    cairo_rectangle(cr, 0, 0, window->w, window->h);
+    cairo_fill(cr);
   }
-  cairo_rectangle(cr, 0, 0, window->w, window->h);
-  cairo_fill(cr);
   /* text */
   cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
+  cairo_set_font_size (cr, 20);
   cairo_select_font_face (cr, "CourierNew",
                           CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
-  cairo_set_font_size (cr, 20);
   cairo_text_extents (cr, seq->title, &te);
   cairo_move_to (cr, 20, window->h - te.height - te.y_bearing - 20);
   cairo_show_text (cr, seq->title);
@@ -119,6 +105,38 @@ bool c3_window_cairo_demo_render (s_window_cairo *window,
   return true;
 }
 
+void c3_window_cairo_demo_render_bg_rect (s_window_cairo *window,
+                                          cairo_t *cr,
+                                          s_sequence *seq)
+{
+#define C3_WINDOW_CAIRO_DEMO_BG_RECT_COLOR_MAX 8
+  const s_rgb color[C3_WINDOW_CAIRO_DEMO_BG_RECT_COLOR_MAX] = {
+    {1, 1, 1},
+    {1, 0, 0},
+    {1, 1, 0},
+    {0, 1, 0},
+    {0, 1, 1},
+    {0, 0, 1},
+    {1, 0, 1},
+    {0, 0, 0}
+  };
+  u8 c1;
+  u8 c2;
+  double p;
+  double q;
+  s_rgb rgb;
+  c1 = (u8) trunc(seq->t) % C3_WINDOW_CAIRO_DEMO_BG_RECT_COLOR_MAX;
+  c2 = (u8) trunc(seq->t + 1.0) % C3_WINDOW_CAIRO_DEMO_BG_RECT_COLOR_MAX;
+  p = fmod(seq->t, 1.0);
+  q = 1.0 - p;
+  rgb.r = color[c1].r * q + color[c2].r * p;
+  rgb.g = color[c1].g * q + color[c2].g * p;
+  rgb.b = color[c1].b * q + color[c2].b * p;
+  cairo_set_source_rgb(cr, rgb.r, rgb.g, rgb.b);
+  cairo_rectangle(cr, 0, 0, window->w, window->h);
+  cairo_fill(cr);
+}
+
 bool c3_window_cairo_demo_resize (s_window_cairo *window,
                                   uw w, uw h)
 {
diff --git a/libc3/window/cairo/c3_window_cairo_demo.h b/libc3/window/cairo/c3_window_cairo_demo.h
index a36870b..6b1d25b 100644
--- a/libc3/window/cairo/c3_window_cairo_demo.h
+++ b/libc3/window/cairo/c3_window_cairo_demo.h
@@ -23,6 +23,9 @@ bool c3_window_cairo_demo_button (s_window_cairo *window, u8 button,
 bool c3_window_cairo_demo_key (s_window_cairo *window, uw keysym);
 bool c3_window_cairo_demo_load (s_window_cairo *window);
 bool c3_window_cairo_demo_render (s_window_cairo *window, cairo_t *cr);
+void c3_window_cairo_demo_render_bg_rect (s_window_cairo *window,
+                                          cairo_t *cr,
+                                          s_sequence *seq);
 bool c3_window_cairo_demo_resize (s_window_cairo *window, uw w, uw h);
 
 #endif /* LIBC3_WINDOW_CAIRO_DEMO_H */
diff --git a/libc3/window/cairo/types.h b/libc3/window/cairo/types.h
index d85f85f..11c7629 100644
--- a/libc3/window/cairo/types.h
+++ b/libc3/window/cairo/types.h
@@ -22,6 +22,8 @@
 #include <libc3/types.h>
 #include "../types.h"
 
+typedef struct rgb          s_rgb;
+typedef struct rgba         s_rgba;
 typedef struct window_cairo s_window_cairo;
 
 /* return false to break event loop */
@@ -46,6 +48,19 @@ typedef bool (*f_window_cairo_render) (s_window_cairo *window,
 typedef bool (*f_window_cairo_resize) (s_window_cairo *window,
                                        uw w, uw h);
 
+struct rgb {
+  double r;
+  double g;
+  double b;
+};
+
+struct rgba {
+  double r;
+  double g;
+  double b;
+  double a;
+};
+
 /* Subtype of s_window. See libc3/window/types.h */
 struct window_cairo {
   sw                    x;