-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient.py
46 lines (44 loc) · 1.49 KB
/
gradient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pygame
def fill_gradient(surface, color, gradient, rect=None, vertical=True, forward=True):
"""fill a surface with a gradient pattern
Parameters:
color -> starting color
gradient -> final color
rect -> area to fill; default is surface's rect
vertical -> True=vertical; False=horizontal
forward -> True=forward; False=reverse
Pygame recipe: http://www.pygame.org/wiki/GradientCode
"""
if rect is None: rect = surface.get_rect()
x1, x2 = rect.left, rect.right
y1, y2 = rect.top, rect.bottom
if vertical:
h = y2 - y1
else:
h = x2 - x1
if forward:
a, b = color, gradient
else:
b, a = color, gradient
rate = (
float(b[0] - a[0]) / h,
float(b[1] - a[1]) / h,
float(b[2] - a[2]) / h
)
fn_line = pygame.draw.line
if vertical:
for line in range(y1, y2):
color = (
min(max(a[0] + (rate[0] * (line - y1)), 0), 255),
min(max(a[1] + (rate[1] * (line - y1)), 0), 255),
min(max(a[2] + (rate[2] * (line - y1)), 0), 255)
)
fn_line(surface, color, (x1, line), (x2, line))
else:
for col in range(x1, x2):
color = (
min(max(a[0] + (rate[0] * (col - x1)), 0), 255),
min(max(a[1] + (rate[1] * (col - x1)), 0), 255),
min(max(a[2] + (rate[2] * (col - x1)), 0), 255)
)
fn_line(surface, color, (col, y1), (col, y2))