Linear gradient fill mode
This commit is contained in:
parent
0dfe96f291
commit
76cb1c5446
4 changed files with 81 additions and 16 deletions
44
src/fill.c
44
src/fill.c
|
|
@ -188,16 +188,54 @@ void simulate_flood_fill(SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Ui
|
|||
}
|
||||
|
||||
|
||||
void draw_linear_gradient(SDL_Surface * canvas, int x_left, int y_top, int x_right, int y_bottom,
|
||||
void draw_linear_gradient(SDL_Surface * canvas, SDL_Surface * last,
|
||||
int x_left, int y_top, int x_right, int y_bottom,
|
||||
int x1, int y1, int x2, int y2, Uint32 draw_color, Uint8 * touched
|
||||
) {
|
||||
Uint32 old_colr, new_colr;
|
||||
int xx, yy;
|
||||
float xd, yd;
|
||||
Uint8 draw_r, draw_g, draw_b, old_r, old_g, old_b, new_r, new_g, new_b;
|
||||
float A, B, C, C1, C2, ratio;
|
||||
|
||||
/* Get our target color */
|
||||
SDL_GetRGB(draw_color, canvas->format, &draw_r, &draw_g, &draw_b);
|
||||
|
||||
A = (x2 - x1);
|
||||
B = (y2 - y1);
|
||||
C1 = (A * x1) + (B * y1);
|
||||
C2 = (A * x2) + (B * y2);
|
||||
/* FIXME: C2 should be larger than C1? */
|
||||
|
||||
for (yy = y_top; yy <= y_bottom; yy++) {
|
||||
for (xx = x_left; xx <= x_right; xx++) {
|
||||
if (touched[(yy * canvas->w) + xx]) {
|
||||
/* FIXME */
|
||||
putpixels[canvas->format->BytesPerPixel] (canvas, xx, yy, draw_color);
|
||||
/* Get the old color, and blend it (with a distance-based ratio) with the target color */
|
||||
old_colr = getpixels[last->format->BytesPerPixel] (last, xx, yy);
|
||||
SDL_GetRGB(old_colr, last->format, &old_r, &old_g, &old_b);
|
||||
|
||||
/* (h/t David Z on StackOverflow for how to quickly compute this:
|
||||
https://stackoverflow.com/questions/521493/creating-a-linear-gradient-in-2d-array) */
|
||||
C = (A * xx) + (B * yy);
|
||||
|
||||
if (C < C1) {
|
||||
/* At/beyond the click spot (opposite direction of mouse); solid color */
|
||||
putpixels[canvas->format->BytesPerPixel] (canvas, xx, yy, draw_color);
|
||||
} else if (C >= C2) {
|
||||
/* At/beyond the mouse; completely faded out */
|
||||
putpixels[canvas->format->BytesPerPixel] (canvas, xx, yy, old_colr);
|
||||
} else {
|
||||
/* The actual gradient... */
|
||||
|
||||
ratio = (C - C1) / (C2 - C1);
|
||||
|
||||
new_r = (Uint8) (((float) old_r) * ratio + ((float) draw_r * (1.00 - ratio)));
|
||||
new_g = (Uint8) (((float) old_g) * ratio + ((float) draw_g * (1.00 - ratio)));
|
||||
new_b = (Uint8) (((float) old_b) * ratio + ((float) draw_b * (1.00 - ratio)));
|
||||
|
||||
new_colr = SDL_MapRGB(canvas->format, new_r, new_g, new_b);
|
||||
putpixels[canvas->format->BytesPerPixel] (canvas, xx, yy, new_colr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue