Gradient fill color Algorithm

Source: Internet
Author: User

Method 1:
Bilinear interpolation method

1. Calculate the zoom ratio first. Bl = 2/W... 2 is the source image width (because there are only two colors)... W is the enlarged width.
2. Start the loop, from 1 to w the target width. If the loop variable is I..., use n = I * BL.
3. extract the fractional part of N and store it in Variable P .. then synthesize the color. The target color r = R1 * (1-p) + R2 * p g = G1 * (1-p) + G2 * p B = b1 * (1-p) + B2 * p .... r1/G1/B1 is the starting color of red/green/blue, R2/G2/B2 is the ending color of red/green/blue.

 

Color g_color [3] = {255, 0, 0,255}, {0,255, 0,255}, {0, 0,255,255 }};
U8 g_perc [2] = {30, 70 };
Gradient_color g_gc = {g_color, g_perc, 3 };

Void fill_gradient_color (s32 X1, s32 Y1, s32 X2, s32 Y2)
{
S32 I;
S32 done = 0;
S32 W;
S32 P;
S32 fill_width;
S32 x_start = 0, x_end;
S32 X;
Float BL;
Float n, m;
Float TR, TG, TB;
Color start_color, end_color, C;
 
Gradient_color * GC = & g_gc;

Fill_width = x2-x1;
 
For (I = 0; (I <(GC-> n-1 )&&! Done); I ++)
{
Start_color = GC-> C [I];
End_color = GC-> C [I + 1];
P = GC-> P [I];
X_end = x_start + pixtel_percent (fill_width, P );
If (x_end> fill_width-1)
{
X_end = fill_width-1;
Done = 1;
}

W = x_end-x_start;
BL = 1.0/(float) W;

C. R = start_color.r;
C. G = start_color.g;
C. B = start_color. B;
C. Alpha = start_color.alpha;
For (x = 1; x <= W; X ++)
{
S32 TMP;
N = (float) x * BL;
TMP = (s32) N;
M = N-(float) TMP;
Gui_draw_vertical_line (Y1, Y2, X1 + x_start + X, C );

Tr = start_color.r * (1.0-m) + end_color.r * m;
Tg = start_color.g * (1.0-m) + end_color.g * m;
TB = start_color. B * (1.0-m) + end_color. B * m;
C. R = (s8) TR;
C. G = (s8) TG;
C. B = (s8) TB;
}
X_start = x_end-1;
}
}

 

 

Method 2:
Divisor is the gradient width, IR, Ig, IB is the incremental value of different color components, IR = (R2-R1)/divisor, Ig = (G2-G1)/divisor, IB = (B2-B1)/divisor, R1/G1/B1 is the starting color of red/green/blue, R2/G2/B2 is the ending color of red/green/blue. then, starting from the initial color value, each painting increases the color value components by IR, Ig, and IB.

For (I = 0; (I <(GC-> n-1 )&&! Done); I ++)
{
Start_color = GC-> C [I];
End_color = GC-> C [I + 1];
P = GC-> P [I];
X_end = x_start + pixtel_percent (fill_width, P );
If (x_end> fill_width-1)
{
X_end = fill_width-1;
Done = 1;
}
Divisor = (float) (x_end-x_start) + 1;
If (divisor = 0)
{
Divisor = 1;
}
IR =-(start_color.r-end_color.r)/divisor;
IG =-(start_color.g-end_color.g)/divisor;
IB =-(start_color. B-end_color. B)/divisor;
Tr = C. R = start_color.r;
Tg = c. g = start_color.g;
TB = C. B = start_color. B;
C. Alpha = start_color.alpha;
For (x = x_start; x <= x_end; X ++)
{
Gui_draw_vertical_line (Y1, Y2, x + X1, C );
Tr + = IR;
Tg + = ig;
TB + = Ib;
C. R = (s8) TR;
C. G = (s8) TG;
C. B = (s8) TB;
}
X_start = x_end + 1;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.