Class: Magick::GradientFill

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

Instance Method Summary collapse

Constructor Details

#initialize(x1, y1, x2, y2, start_color, stop_color) ⇒ Magick::GradientFill

Initialize GradientFill object.

Parameters:

  • x1 (Float)

    x position of first point

  • y1 (Float)

    y position of first point

  • x2 (Float)

    x position of second point

  • y2 (Float)

    y position of second point

  • start_color (Magick::Pixel, String)

    the start color

  • stop_color (Magick::Pixel, String)

    the stop color



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/RMagick/rmfill.cpp', line 108

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

    TypedData_Get_Struct(self, rm_GradientFill, &rm_gradient_fill_data_type, fill);

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

    return self;
}

Instance Method Details

#fill(image_obj) ⇒ Magick::GradientFill

Call GradientFill with the start and stop colors specified when this fill object was created.

Parameters:

Returns:



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'ext/RMagick/rmfill.cpp', line 640

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

    TypedData_Get_Struct(self, rm_GradientFill, &rm_gradient_fill_data_type, fill);
    image = rm_check_destroyed(rm_cur_image(image_obj));

    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.
        else
        {
            vertical_fill(image, x1, &start_color, &stop_color);
        }
    }

    // A horizontal line is a special case.
    else if (fabs(y2-y1) < 0.5)
    {
        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;
}