639
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
|
# File 'ext/RMagick/rmfill.cpp', line 639
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;
}
|