static cairo_pattern_t *
transition_pattern (cairo_pattern_t *start,
                    cairo_pattern_t *end,
                    double           progress)
{
  double sx0, sy0, sx1, sy1, sr0, sr1, ex0, ey0, ex1, ey1, er0, er1;
  cairo_pattern_t *result;
  int i, n;

  progress = CLAMP (progress, 0.0, 1.0);

  if (end == NULL)
    return fade_pattern (start, 1.0 - progress);

  g_assert (cairo_pattern_get_type (start) == cairo_pattern_get_type (end));

  switch (cairo_pattern_get_type (start))
    {
    case CAIRO_PATTERN_TYPE_LINEAR:
      cairo_pattern_get_linear_points (start, &sx0, &sy0, &sx1, &sy1);
      cairo_pattern_get_linear_points (end, &ex0, &ey0, &ex1, &ey1);
      result = cairo_pattern_create_linear ((1 - progress) * sx0 + progress * ex0,
                                            (1 - progress) * sx1 + progress * ex1,
                                            (1 - progress) * sy0 + progress * ey0,
                                            (1 - progress) * sy1 + progress * ey1);
      break;
    case CAIRO_PATTERN_TYPE_RADIAL:
      cairo_pattern_get_radial_circles (start, &sx0, &sy0, &sr0, &sx1, &sy1, &sr1);
      cairo_pattern_get_radial_circles (end, &ex0, &ey0, &er0, &ex1, &ey1, &er1);
      result = cairo_pattern_create_radial ((1 - progress) * sx0 + progress * ex0,
                                            (1 - progress) * sy0 + progress * ey0,
                                            (1 - progress) * sr0 + progress * er0,
                                            (1 - progress) * sx1 + progress * ex1,
                                            (1 - progress) * sy1 + progress * ey1,
                                            (1 - progress) * sr1 + progress * er1);
      break;
    default:
      g_return_val_if_reached (NULL);
    }

  cairo_pattern_get_color_stop_count (start, &n);
  for (i = 0; i < n; i++)
    {
      double so, sr, sg, sb, sa, eo, er, eg, eb, ea;

      cairo_pattern_get_color_stop_rgba (start, i, &so, &sr, &sg, &sb, &sa);
      cairo_pattern_get_color_stop_rgba (end, i, &eo, &er, &eg, &eb, &ea);

      cairo_pattern_add_color_stop_rgba (result,
                                         (1 - progress) * so + progress * eo,
                                         (1 - progress) * sr + progress * er,
                                         (1 - progress) * sg + progress * eg,
                                         (1 - progress) * sb + progress * eb,
                                         (1 - progress) * sa + progress * ea);
    }

  return result;
}
static PyObject *
linear_gradient_get_linear_points (PycairoLinearGradient *o)
{
    double x0, y0, x1, y1;
    cairo_pattern_get_linear_points (o->pattern, &x0, &y0, &x1, &y1);
    return Py_BuildValue("(dddd)", x0, y0, x1, y1);
}
static cairo_pattern_t *
fade_pattern (cairo_pattern_t *pattern,
              double           opacity)
{
  double x0, y0, x1, y1, r0, r1;
  cairo_pattern_t *result;
  int i, n;

  switch (cairo_pattern_get_type (pattern))
    {
    case CAIRO_PATTERN_TYPE_LINEAR:
      cairo_pattern_get_linear_points (pattern, &x0, &y0, &x1, &y1);
      result = cairo_pattern_create_linear (x0, y0, x1, y1);
      break;
    case CAIRO_PATTERN_TYPE_RADIAL:
      cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
      result = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
      break;
    default:
      g_return_val_if_reached (NULL);
    }

  cairo_pattern_get_color_stop_count (pattern, &n);
  for (i = 0; i < n; i++)
    {
      double o, r, g, b, a;

      cairo_pattern_get_color_stop_rgba (pattern, i, &o, &r, &g, &b, &a);
      cairo_pattern_add_color_stop_rgba (result, o, r, g, b, a * opacity);
    }

  return result;
}
Exemple #4
0
static VALUE
cr_linear_pattern_get_linear_points (VALUE self)
{
  cairo_status_t status;
  double x0, y0, x1, y1;

  status = cairo_pattern_get_linear_points (_SELF (self), &x0, &y0, &x1, &y1);
  rb_cairo_check_status (status);
  return rb_ary_new3 (4,
                      rb_float_new (x0), rb_float_new (y0),
                      rb_float_new (x1), rb_float_new (y1));
}
Exemple #5
0
static int cairo_pattern_get_linear_points_l( lua_State* L )
{
  double x0 = 0.0;
  double y0 = 0.0;
  double x1 = 0.0;
  double y1 = 0.0;
  lua_cairo_pattern_t* lcp = lua_cairo_pattern_check( L, 1 );

  if ( cairo_pattern_get_linear_points( lcp->pattern, &x0, &y0, &x1, &y1 ) ) {
    luaL_argerror( L, 1, "pattern is not a linear gradient pattern" );
  }

  lua_pushnumber( L, x0 );
  lua_pushnumber( L, y0 );
  lua_pushnumber( L, x1 );
  lua_pushnumber( L, y1 );

  return( 4 );
}
IoObject *IoCairoLinearGradient_getLinearPoints(IoCairoLinearGradient *self, IoObject *locals, IoMessage *m)
{
    double data[4];
    cairo_pattern_get_linear_points(PATTERN(self), &data[0], &data[1], &data[2], &data[3]);
    return IoSeq_newWithDoubles_count_(IOSTATE, data, 4);
}
Exemple #7
0
static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
    cairo_status_t status;
    cairo_pattern_t *pat;

    /* Test pattern_get_rgba */
    {
        double r, g, b, a;
        pat = cairo_pattern_create_rgba (0.2, 0.3, 0.4, 0.5);

        status = cairo_pattern_get_rgba (pat, &r, &g, &b, &a);
        CHECK_SUCCESS;

        if (!CAIRO_TEST_DOUBLE_EQUALS(r,0.2) ||
                !CAIRO_TEST_DOUBLE_EQUALS(g,0.3) ||
                !CAIRO_TEST_DOUBLE_EQUALS(b,0.4) ||
                !CAIRO_TEST_DOUBLE_EQUALS(a,0.5)) {
            cairo_test_log (ctx, "Error: cairo_pattern_get_rgba returned unexepcted results: %g, %g, %g, %g\n",
                            r, g, b, a);
            return CAIRO_TEST_FAILURE;
        }

        cairo_pattern_destroy (pat);
    }

    /* Test pattern_get_surface */
    {
        cairo_surface_t *surf;

        pat = cairo_pattern_create_for_surface (cairo_get_target (cr));

        status = cairo_pattern_get_surface (pat, &surf);
        CHECK_SUCCESS;

        if (surf != cairo_get_target (cr)) {
            cairo_test_log (ctx, "Error: cairo_pattern_get_resurface returned wrong surface\n");
            return CAIRO_TEST_FAILURE;
        }

        cairo_pattern_destroy (pat);
    }

    /* Test get_color_stops & linear_get_points */
    {
        int i;
        double x0, y0, x1, y1;
        double expected_values[15] = { 0.0, 0.2, 0.4, 0.2, 1.0,
                                       0.5, 0.4, 0.5, 0.2, 0.5,
                                       1.0, 0.2, 0.4, 0.5, 0.2
                                     };
        double new_buf[15];

        pat = cairo_pattern_create_linear (1.0, 2.0, 3.0, 4.0);

        for (i = 0; i < 3; i++) {
            cairo_pattern_add_color_stop_rgba (pat,
                                               expected_values[i*5+0],
                                               expected_values[i*5+1],
                                               expected_values[i*5+2],
                                               expected_values[i*5+3],
                                               expected_values[i*5+4]);
        }

        status = cairo_pattern_get_linear_points (pat, &x0, &y0, &x1, &y1);
        CHECK_SUCCESS;

        if (!CAIRO_TEST_DOUBLE_EQUALS(x0,1.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(y0,2.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(x1,3.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(y1,4.0))
            return CAIRO_TEST_FAILURE;

        status = cairo_pattern_get_color_stop_count (pat, &i);
        CHECK_SUCCESS;

        if (i != 3)
            return CAIRO_TEST_FAILURE;

        for (i = 0; i < 3; i++) {
            status = cairo_pattern_get_color_stop_rgba (pat, i,
                     &new_buf[i*5+0],
                     &new_buf[i*5+1],
                     &new_buf[i*5+2],
                     &new_buf[i*5+3],
                     &new_buf[i*5+4]);
            CHECK_SUCCESS;
        }

        status = cairo_pattern_get_color_stop_rgba (pat, 5, NULL, NULL, NULL, NULL, NULL);
        if (status != CAIRO_STATUS_INVALID_INDEX)
            return CAIRO_TEST_FAILURE;

        if (!double_buf_equal (ctx, new_buf, expected_values, sizeof(expected_values)/sizeof(double)) != 0)
            return CAIRO_TEST_FAILURE;

        cairo_pattern_destroy (pat);
    }

    /* Test radial_get_circles */
    {
        double a, b, c, d, e, f;
        pat = cairo_pattern_create_radial (1, 2, 3,
                                           4, 5, 6);

        status = cairo_pattern_get_radial_circles (pat, &a, &b, &c, &d, &e, &f);
        CHECK_SUCCESS;

        if (!CAIRO_TEST_DOUBLE_EQUALS(a,1.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(b,2.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(c,3.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(d,4.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(e,5.0) ||
                !CAIRO_TEST_DOUBLE_EQUALS(f,6.0))
            return CAIRO_TEST_FAILURE;

        cairo_pattern_destroy (pat);
    }

    cairo_set_source_rgb (cr, 0, 1, 0);
    cairo_paint (cr);

    return CAIRO_TEST_SUCCESS;
}