-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_book.gd
More file actions
123 lines (99 loc) · 3 KB
/
random_book.gd
File metadata and controls
123 lines (99 loc) · 3 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
extends Node
const COLOR_COLORS = [
Color.aqua,
Color.rebeccapurple,
Color.chartreuse,
Color.crimson,
Color.darkorange,
Color.deeppink,
Color.forestgreen,
Color.gold,
Color.lightseagreen,
Color.royalblue,
Color.yellowgreen,
]
const COVER = [
preload("res://random_book/book_cover.png"),
preload("res://random_book/book_cover2.png"),
preload("res://random_book/book_cover3.png"),
]
const PAPER = preload("res://random_book/paper.png")
const BOW = [
preload("res://random_book/bow.png"),
preload("res://random_book/bow2.png"),
preload("res://random_book/bow3.png"),
]
const TEXTS = [
preload("res://random_book/text1.png"),
preload("res://random_book/text2.png"),
preload("res://random_book/text3.png"),
preload("res://random_book/text4.png"),
]
var book_image:Image
func generate_book() -> Image:
book_image = Image.new()
book_image.create(16, 16, false, Image.FORMAT_RGBA8)
#
var cover:Image = COVER[randi()%COVER.size()].get_data()
_draw_layer(16, cover, book_image, 3.0)
#
var paper:Image = PAPER.get_data()
_draw_layer(16, paper, book_image, 1.0,
Color.white,
Color.white,
Color.white
)
#
if randi()%100 > 33:
var text:Image = TEXTS[randi()%TEXTS.size()].get_data()
_draw_layer(16, text, book_image, 1.0,
Color(0.1, 0.1, 0.1, 0.3),
Color(0.1, 0.1, 0.1, 0.3),
Color(0.1, 0.1, 0.1, 0.3)
)
#
if randi()%100 > 33:
var bow:Image = BOW[randi()%BOW.size()].get_data()
_draw_layer(16, bow, book_image, 1.0)
return book_image
const CRS = 0.7
const CRE = 0.98
func _draw_layer(res:int, from_image:Image, to_image:Image, lightness:float = 1.0, tint_color1:Color = Color.black, tint_color2:Color = Color.black, tint_color3:Color = Color.black):
if tint_color1 == Color.black:
tint_color1 = rand_color()
if tint_color2 == Color.black:
tint_color2 = rand_color()
if tint_color3 == Color.black:
tint_color3 = rand_color()
for x in res:
for y in res:
from_image.lock()
var col:Color = from_image.get_pixel(x,y)
from_image.unlock()
if col.a == 0: # Skip to visible pixel...
continue
col *= lightness
if col.r > 0.0: # Uses Tint1
if tint_color1.a >= 1.0:
col = Color(col.r,col.r,col.r) * tint_color1
else:
col = Color(col.r,col.r,col.r).linear_interpolate(Color(col.r,col.r,col.r) * tint_color1, tint_color1.a)
elif col.g > 0.0: # Uses Tint2
if tint_color2.a >= 1.0:
col = Color(col.g,col.g,col.g) * tint_color2
else:
col = Color(col.g,col.g,col.g).linear_interpolate(Color(col.g,col.g,col.g) * tint_color2, tint_color2.a)
elif col.b > 0.0: # Uses Tint3
if tint_color3.a >= 1.0:
col = Color(col.b,col.b,col.b) * tint_color3
else:
col = Color(col.b,col.b,col.b).linear_interpolate(Color(col.b,col.b,col.b) * tint_color3, tint_color3.a)
else: # Just black
col = Color.black
# Lazy fix
col.a = 1.0
to_image.lock()
to_image.set_pixel(x,y,col)
to_image.unlock()
func rand_color() -> Color:
return COLOR_COLORS[randi() % COLOR_COLORS.size()].contrasted()