-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpic2pico.rb
144 lines (128 loc) · 4.78 KB
/
pic2pico.rb
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
# pic2pico. converts pico-8 palette ready .png images to native p8-format
# (not sprites), that you can load images with code.
# 2016, m.wisniowski, nodepond.com
#!/usr/bin/env ruby
filename='pic2pico-test.png'
ARGV.each do |a|
filename=a
end
require 'chunky_png'
abort=''
$plz_convert_me = ChunkyPNG::Image.from_file(filename)
$the_converted_me = []
if $plz_convert_me.width != 128 || $plz_convert_me.height != 128
abort = 'Sorry, currently only support for 128x128 pixel images.'
puts abort
end
pico8_loader = 'local img="%STRING-SEQUENCE%"
function draw_img(data)
for i=0,#data+1 do
local chr=sub(data,i+1,i+1)
pset(i%128,
flr(i/128),
convert_hex2num(chr))
end
end
local str2hex_table={}
str2hex_table["0"]=0
str2hex_table["1"]=1
str2hex_table["2"]=2
str2hex_table["3"]=3
str2hex_table["4"]=4
str2hex_table["5"]=5
str2hex_table["6"]=6
str2hex_table["7"]=7
str2hex_table["8"]=8
str2hex_table["9"]=9
str2hex_table["a"]=10
str2hex_table["b"]=11
str2hex_table["c"]=12
str2hex_table["d"]=13
str2hex_table["e"]=14
str2hex_table["f"]=15
function convert_hex2num(value)
return str2hex_table[value]
end
draw_img(img)'
def is_color_near?(sr, sg, sb, tr, tg, tb, tolerance)
isNear = false
if sr.between?(tr-tolerance/2, tr+tolerance/2) &&
sg.between?(tg-tolerance/2, tg+tolerance/2) &&
sb.between?(tb-tolerance/2, tb+tolerance/2) then
isNear = true
end
return isNear
end
def convert()
for i in 0..(128*128)-1
x = i%128
y = (i/128).floor()
colorTolerance = 15
val = $plz_convert_me[x,y]
if is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 0, 0, 0, colorTolerance)
# index 0 - Black
$the_converted_me << "0"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 29, 43, 83, colorTolerance)
# index 1 - Dark-blue
$the_converted_me << "1"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 126, 37, 83, colorTolerance)
# index 2 - Dark-purple
$the_converted_me << "2"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 0, 136, 81, colorTolerance)
# index 3 - Dark-green
$the_converted_me << "3"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 171, 82, 54, colorTolerance)
# index 4 - Brown
$the_converted_me << "4"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 95, 87, 79, colorTolerance)
# index 5 - Dark-gray
$the_converted_me << "5"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 194, 195, 199, colorTolerance)
# index 6 - Light-gray
$the_converted_me << "6"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 255, 241, 232, colorTolerance)
# index 7 - White
$the_converted_me << "7"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 255, 0, 77, colorTolerance)
# index 8 - Red
$the_converted_me << "8"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 255, 163, 0, colorTolerance)
# index 9 - Orange
$the_converted_me << "9"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 255, 236, 39, colorTolerance)
# index 10 - Yellow
$the_converted_me << "a"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 0, 228, 54, colorTolerance)
# index 11 - Green
$the_converted_me << "b"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 41, 173, 255, colorTolerance)
# index 12 - Blue
$the_converted_me << "c"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 131, 118, 156, colorTolerance)
# index 13 - Indigo
$the_converted_me << "d"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 255, 119, 168, colorTolerance)
# index 14 - Pink
$the_converted_me << "e"
elsif is_color_near?(ChunkyPNG::Color.r(val), ChunkyPNG::Color.g(val), ChunkyPNG::Color.b(val), 255, 204, 170, colorTolerance)
# index 15 - Peach
$the_converted_me << "f"
else
# not found? pure black...
$the_converted_me << "0"
end
end
end
# the "main"
if abort.length > 0
puts 'we abort!'
else
convert()
img_data = ''
for val in $the_converted_me
img_data += String(val)+''
end
# puts img_data
end
puts '-- copy and paste this into pico8'
puts pico8_loader.sub!('%STRING-SEQUENCE%', img_data)