[Intel-gfx] [PATCH i-g-t 06/16] plot: Add a way to color plots

Damien Lespiau damien.lespiau at intel.com
Mon Jul 6 05:35:34 PDT 2015


It can look pretty and allows to differenciate between several plots
drawn on the same canvas.

Signed-off-by: Damien Lespiau <damien.lespiau at intel.com>
---
 lib/igt_plot.c       | 24 ++++++++++++++++++++++++
 lib/igt_plot.h       |  3 +++
 lib/tests/igt_plot.c |  1 +
 3 files changed, 28 insertions(+)

diff --git a/lib/igt_plot.c b/lib/igt_plot.c
index f7187f6..3f3c2fe 100644
--- a/lib/igt_plot.c
+++ b/lib/igt_plot.c
@@ -231,6 +231,8 @@ static void igt_plot_ctx_init(igt_plot_ctx_t *ctx)
 	memset(ctx, 0, sizeof(*ctx));
 
 	ctx->line_width = 1.5;
+	ctx->r = ctx->g = ctx->b = 0.0;
+	ctx->a = 1.0;
 }
 
 static void igt_plot_ctx_fini(igt_plot_ctx_t *ctx)
@@ -287,6 +289,27 @@ void igt_plot_fini(igt_plot_t *plot)
 }
 
 /**
+ * igt_plot_set_color:
+ * @plot: An #igt_plot_t instance
+ * @r: Red component in the [0, 1] range
+ * @g: Green component in the [0, 1] range
+ * @b: Blue component in the [0, 1] range
+ * @a: Alpha component in the [0, 1] range
+ *
+ * Set the color to use when drawing plots.
+ */
+void igt_plot_set_color(igt_plot_t *plot,
+			double r, double g, double b, double a)
+{
+	igt_plot_ctx_t *ctx = plot->ctx;
+
+	ctx->r = r;
+	ctx->g = g;
+	ctx->b = b;
+	ctx->a = a;
+}
+
+/*
  * igt_plot_set_line_width:
  * @plot: An #igt_plot_t instance
  * @width: The new line width to use
@@ -442,6 +465,7 @@ igt_plot_draw_one(igt_plot_t *plot, igt_plot_ctx_t *ctx, flush_t *flush)
 		cairo_line_to(plot->cr,
 			      fit(x->values[i], area->x1, x_range, x_scale),
 			      fit(y->values[i], area->y2, y_range, -y_scale));
+	cairo_set_source_rgba(plot->cr, ctx->r, ctx->g, ctx->b, ctx->a);
 	cairo_set_line_cap(plot->cr, CAIRO_LINE_CAP_BUTT);
 	cairo_set_line_width(plot->cr, ctx->line_width);
 	cairo_stroke(plot->cr);
diff --git a/lib/igt_plot.h b/lib/igt_plot.h
index 82ad10a..2e00c60 100644
--- a/lib/igt_plot.h
+++ b/lib/igt_plot.h
@@ -76,6 +76,7 @@ typedef struct {
 	igt_vector_t *x, *y;
 	double x_range, y_range;
 	double line_width;
+	double r, g, b, a;
 } igt_plot_ctx_t;
 
 /**
@@ -115,6 +116,8 @@ typedef enum {
 
 void igt_plot_init(igt_plot_t *plot, unsigned int width, unsigned int height);
 void igt_plot_fini(igt_plot_t *plot);
+void igt_plot_set_color(igt_plot_t *plot,
+			double r, double g, double b, double a);
 void igt_plot_set_line_width(igt_plot_t *plot, double width);
 void igt_plot_draw(igt_plot_t *plot, igt_vector_t *x, igt_vector_t *y);
 void igt_plot_write(igt_plot_t *plot, const char *filename);
diff --git a/lib/tests/igt_plot.c b/lib/tests/igt_plot.c
index be0e132..cb7f022 100644
--- a/lib/tests/igt_plot.c
+++ b/lib/tests/igt_plot.c
@@ -82,6 +82,7 @@ static void test_simple_plot(void)
 		y->values[i] = sin(2 * M_PI * x->values[i]);
 
 	igt_plot_init(&plot, 800, 600);
+	igt_plot_set_color(&plot, 0.0, 0.0, 1.0, 1.0);
 	igt_plot_draw(&plot, x, y);
 	igt_plot_write(&plot, "test_simple_plot.png");
 
-- 
2.1.0



More information about the Intel-gfx mailing list