/
Monkey-X - Smoothed noisy rainbow from pallete - code example.monkey
185 lines (169 loc) · 4.68 KB
/
Monkey-X - Smoothed noisy rainbow from pallete - code example.monkey
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
Import mojo
Class theimage
Field image:Image
Field pixels:Int[]
Field width:Int
Field height:Int
Method New()
Self.width = DeviceWidth()
Self.height = DeviceHeight()
pixels = New Int[width*height]
image = CreateImage(width,height)
makeimage
image.WritePixels(pixels, 0, 0, width, height, 0)
End Method
Method makeimage()
' here we make the
' noisy rainbow
Local x:Int
Local ty:Int
Local cnt:Int
Local sc:Int 'dheight with num of cols
For Local i:Int = 0 Until width*height
Local col:Int=sc
If sc<15
If Rnd((16-ty)+4) < 2
col+=1
End If
End If
pixels[i] = argb( spread.r[col],
spread.g[col],
spread.b[col])
x+=1
If x>width
x=0
cnt+=1
ty+=1
If ty>height/16
ty=0
End If
If cnt>height/16
cnt=0
sc+=1
End If
End If
Next
For Local ii=0 Until 20
For Local i:Int = 0 Until width*(height-1)
Local r1:Int=getred(pixels[i])
Local g1:Int=getgreen(pixels[i])
Local b1:Int=getblue(pixels[i])
Local r2:Int=getred(pixels[i+1])
Local g2:Int=getgreen(pixels[i+1])
Local b2:Int=getblue(pixels[i+1])
Local r3:Int=getred(pixels[i+width])
Local g3:Int=getgreen(pixels[i+width])
Local b3:Int=getblue(pixels[i+width])
Local ra:Int=(r1+r2+r3)/3
Local ga:Int=(g1+g2+g3)/3
Local ba:Int=(b1+b2+b3)/3
pixels[i] = argb(ra,ga,ba)
Next
next
End Method
End Class
Class colorspread16
Field r:Float[16]
Field g:Float[16]
Field b:Float[16]
Method New( r1:Int,g1:Int,b1:Int,
r2:Int,g2:Int,b2:Int)
Local sr:Float
Local sg:Float
Local sb:Float
If r1>r2
sr = (r1-r2)/16
For Local i:Float=0 Until 16 Step 1
r[i] = r1-(i*sr)
Next
Else
sr = (r2-r1)/16
For Local i:Float=0 Until 16 Step 1
r[i] = r1+i*sr
Next
End If
If g1>g2
sg = (g1-g2)/16
For Local i:Float=0 Until 16 Step 1
g[i] = g1-(i*sg)
Next
Else
sg = (g2-g1)/16
For Local i:Float=0 Until 16 Step 1
g[i] = g1+i*sg
Next
End If
If b1>b2
sb = (b1-b2)/16
For Local i:Float=0 Until 16 Step 1
b[i] = b1-(i*sb)
Next
Else
sb = (b2-b1)/16
For Local i:Float=0 Until 16 Step 1
b[i] = b1+i*sb
Next
End If
End Method
End Class
Global spread:colorspread16
Global myimage:theimage
Class MyGame Extends App
Field cnt:Int
Field r:Int[2]
Field g:Int[2]
Field b:Int[2]
Method OnCreate()
SetUpdateRate(60)
Seed = GetDate[5]
For Local i=0 To 1
r[i] = Rnd(0,255)
g[i] = Rnd(0,255)
b[i] = Rnd(0,255)
Next
spread = New colorspread16( r[0],g[0],b[0],
r[1],g[1],b[1])
myimage = New theimage()
End Method
Method OnUpdate()
cnt+=1
If cnt>200
cnt=0
For Local i=0 To 1
r[i] = Rnd(0,255)
g[i] = Rnd(0,255)
b[i] = Rnd(0,255)
Next
spread = New colorspread16( r[0],g[0],b[0],
r[1],g[1],b[1])
myimage = New theimage()
End If
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
DrawImage myimage.image,0,0
' Draw the text
SetColor 255,255,255
DrawText "Monkey-X - Making Noisy Smoothed "+
" Rainbows - Example",0,0
End Method
End Class
Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b
End Function
Function getred:Int(rgba:Int)
Return((rgba Shr 16) & $FF)
End Function
Function getgreen:Int(rgba:Int)
Return((rgba Shr 8) & $FF)
End Function
Function getblue:Int(rgba:Int)
Return(rgba & $FF)
End Function
Function getalpha:Int(rgba:Int)
Return ((rgba Shr 24) & $FF)
End Function
Function Main()
New MyGame()
End Function