-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_via_test_fn.demo.py
66 lines (44 loc) · 1.46 KB
/
image_via_test_fn.demo.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from utility.write_rgb_image import write_rgb_image as write_image
def select_color(x, y, bounds, color_signature):
'''
Given (x, y, bounds, color_signature),
Select the color for this point.
'''
show_fg = color_signature['test'](x, y, bounds)
return {
'x': x,
'y': y,
'color': color_signature['fg'] if show_fg else color_signature['bg']
}
''' Tests to determine if x,y is colored '''
def if_sum_of_digits_is_lt_20(x, y, bounds):
def digitSum(x, y):
return sum([int(i) for i in str(abs(x))]) + sum([int(i) for i in str(abs(y))])
half_bounds = map(lambda v: v / 2, bounds)
return digitSum(x - half_bounds[0], y - half_bounds[1]) < 20
def if_x_squared_mod_y(x, y, bounds):
if not y:
return False
return x**2 % y == 0
def if_prime(x, y, bounds):
def test_prime(value):
if value <= 0:
return False
if value < 4:
return True
return all(value % case for case in range(2, int(value**0.5) + 1))
# Hint: try out x%(y+1) and x*y
return test_prime(x + y)
# DEMO: change the test to see what happens to the outcome
color_signature = {
'test': if_prime,
'fg': (255, 255, 255), # Selected color if test returns true.
'bg': (0, 0, 0), # Ditto for false.
}
bounds = (600, 600)
colors = [
select_color(x, y, bounds, color_signature)
for x in range(bounds[0])
for y in range(bounds[1])
]
write_image(colors, bounds)