|
1 | | -# Simple Linear Gradient |
2 | | -# |
| 1 | +# Simple Linear Gradient |
| 2 | +# |
3 | 3 | # The lerp_color function is useful for interpolating |
4 | 4 | # between two colors. |
5 | 5 | # |
6 | | - |
| 6 | + |
7 | 7 | Y_AXIS = 1 |
8 | 8 | X_AXIS = 2 |
9 | 9 |
|
10 | 10 | def setup |
11 | | - size 640, 360 |
| 11 | + size 640, 360 |
12 | 12 | # Define colors |
13 | 13 | b1 = color(255) |
14 | 14 | b2 = color(0) |
15 | 15 | c1 = color(204, 102, 0) |
16 | 16 | c2 = color(0, 102, 153) |
17 | | - |
18 | 17 | # Background |
19 | | - set_gradient(0, 0, width/2, height, b1, b2, X_AXIS) |
20 | | - set_gradient(width/2, 0, width/2, height, b2, b1, X_AXIS) |
| 18 | + set_gradient(0, 0, width / 2, height, b1, b2, X_AXIS) |
| 19 | + set_gradient(width / 2, 0, width / 2, height, b2, b1, X_AXIS) |
21 | 20 | # Foreground |
22 | 21 | set_gradient(50, 90, 540, 80, c1, c2, Y_AXIS) |
23 | 22 | set_gradient(50, 190, 540, 80, c2, c1, X_AXIS) |
24 | 23 | end |
25 | 24 |
|
26 | | -def set_gradient(x, y, w, h, c1, c2, axis ) |
| 25 | +def set_gradient(x, y, w, h, c1, c2, axis) |
27 | 26 | no_fill |
28 | 27 | if (axis == Y_AXIS) # Top to bottom gradient |
29 | | - (y ... y + h).each do |i| |
30 | | - inter = map(i, y, y + h, 0, 1) |
| 28 | + (y...y + h).each do |i| |
| 29 | + inter = map1d(i, (y..y + h), (0..1.0)) |
31 | 30 | stroke lerp_color(c1, c2, inter) |
32 | 31 | line(x, i, x + w, i) |
33 | 32 | end |
34 | 33 | elsif (axis == X_AXIS) # Left to right gradient |
35 | | - (x ... x + w).each do |i| |
36 | | - inter = map(i, x, x + w, 0, 1) |
| 34 | + (x...x + w).each do |i| |
| 35 | + inter = map1d(i, (x..x + w), (0..1)) |
37 | 36 | stroke lerp_color(c1, c2, inter) |
38 | 37 | line(i, y, i, y + h) |
39 | 38 | end |
40 | 39 | end |
41 | 40 | end |
42 | | - |
0 commit comments