Class: Magick::GradientFill

Inherits:
Object
  • Object
show all
Defined in:
ext/RMagick/rmmain.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x1, y1, x2, y2, start_color, stop_color) ⇒ Object

Extern: GradientFill#initialize(start_color, stop_color) Purpose: store the vector points and the start and stop colors



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/RMagick/rmfill.c', line 82

VALUE
GradientFill_initialize(
    VALUE self,
    VALUE x1,
    VALUE y1,
    VALUE x2,
    VALUE y2,
    VALUE start_color,
    VALUE stop_color)
{
    rm_GradientFill *fill;

    Data_Get_Struct(self, rm_GradientFill, fill);

    fill->x1 = NUM2DBL(x1);
    fill->y1 = NUM2DBL(y1);
    fill->x2 = NUM2DBL(x2);
    fill->y2 = NUM2DBL(y2);
    Color_to_PixelPacket(&fill->start_color, start_color);
    Color_to_PixelPacket(&fill->stop_color, stop_color);

    return self;
}

Class Method Details

.new(x1, y1, x2, y2, start_color, stop_color) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/RMagick/rmfill.c', line 38

VALUE
GradientFill_new(
    VALUE class,
    VALUE x1,
    VALUE y1,
    VALUE x2,
    VALUE y2,
    VALUE start_color,
    VALUE stop_color)
{
    rm_GradientFill *fill;
    volatile VALUE new_fill;
    VALUE argv[6];

    argv[0] = x1;
    argv[1] = y1;
    argv[2] = x2;
    argv[3] = y2;
    argv[4] = start_color;
    argv[5] = stop_color;

    new_fill = Data_Make_Struct(class
                              , rm_GradientFill
                              , NULL
                              , free_Fill
                              , fill);

    rb_obj_call_init((VALUE)new_fill, 6, argv);
    return new_fill;
}

Instance Method Details

#fill(image_obj) ⇒ Object

Extern: GradientFill_fill(image_obj) Purpose: the GradientFill#fill method - call GradientFill with the

start and stop colors specified when this fill object
was created.


460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'ext/RMagick/rmfill.c', line 460

VALUE
GradientFill_fill(VALUE self, VALUE image_obj)
{
    rm_GradientFill *fill;
    Image *image;
    PixelPacket start_color, stop_color;
    double x1, y1, x2, y2;          // points on the line

    Data_Get_Struct(self, rm_GradientFill, fill);
    Data_Get_Struct(image_obj, Image, image);

    x1 = fill->x1;
    y1 = fill->y1;
    x2 = fill->x2;
    y2 = fill->y2;
    start_color = fill->start_color;
    stop_color  = fill->stop_color;

    if (fabs(x2-x1) < 0.5)       // vertical?
    {
        // If the x1,y1 and x2,y2 points are essentially the same
        if (fabs(y2-y1) < 0.5)
        {
            point_fill(image, x1, y1, &start_color, &stop_color);
        }

        // A vertical line is a special case. (Yes, really do pass x1
        // as both the 2nd and 4th arguments!)
        else
        {
            vertical_fill(image, x1, &start_color, &stop_color);
        }
    }

    // A horizontal line is a special case.
    else if (fabs(y2-y1) < 0.5)
    {
        // Pass y1 as both the 3rd and 5th arguments!
        horizontal_fill(image, y1, &start_color, &stop_color);
    }

    // This is the general case - a diagonal line. If the line is more horizontal
    // than vertical, use the top and bottom of the image as the ends of the
    // gradient, otherwise use the sides of the image.
    else
    {
        double m = ((double)(y2 - y1))/((double)(x2 - x1));
        double diagonal = ((double)image->rows)/image->columns;
        if (fabs(m) <= diagonal)
        {
            v_diagonal_fill(image, x1, y1, x2, y2, &start_color, &stop_color);
        }
        else
        {
            h_diagonal_fill(image, x1, y1, x2, y2, &start_color, &stop_color);
        }
    }

    return self;
}