Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'gd'
- --[[
- USAGE: ePlot(filename of source image, element init. resolution)
- Init. resolution should always be 255, but it might do well with lower ones.
- (Init. resolution: how close in color an element should be to be considered
- 'close'. It may seem better to have a low value, but that just means pixels
- with no close-colored elements will simply be ignored.)
- -]]
- local fileWriteStr = [[
- DUST,255,224,160
- WATR,32,48,208
- OIL,64,64,16
- STNE,160,160,160
- LAVA,224,80,16
- GUN,192,192,208
- NITR,32,224,16
- CLNE,255,208,16
- GAS,224,255,32
- C-4,208,128,224
- GOO,128,64,0
- ICE,160,192,255
- METL,64,64,96
- SPRK,255,255,128
- SNOW,192,224,255
- WOOD,192,160,64
- NEUT,32,224,255
- PLUT,64,112,32
- PLNT,12,172,0
- ACID,237,85,255
- VOID,121,11,11
- WTRV,160,160,255
- CNCT,192,192,192
- DSTW,16,32,192
- SALT,255,255,255
- SLTW,64,80,240
- DMND,204,255,255
- BMTL,80,80,112
- BRMT,112,80,96
- PHOT,255,255,255
- URAN,112,112,32
- WAX,240,240,187
- MWAX,224,224,170
- PSCN,128,80,80
- NSCN,80,80,128
- LN2,128,160,223
- INSL,158,163,182
- VACU,48,48,48
- VENT,239,239,239
- RBDM,204,204,204
- LRBD,170,170,170
- NTCT,80,80,64
- SAND,255,208,144
- GLAS,64,64,64
- PTCT,64,80,80
- BGLA,96,96,96
- THDR,255,255,160
- ETRD,64,64,64
- NICE,192,224,255
- NBLE,235,73,23
- BTRY,133,133,5
- LCRY,80,80,80
- SWCH,16,59,17
- DESL,68,0,0
- COAL,34,34,34
- LOXY,128,160,239
- OXYG,128,160,255
- INWR,84,65,65
- YEST,238,224,192
- DYST,187,176,160
- THRM,160,128,144
- BRCK,128,128,128
- FIRW,255,160,64
- FUSE,10,87,6
- FSEP,99,173,95
- AMTR,128,128,128
- BCOL,51,51,51
- PCLN,59,59,10
- HSWC,59,10,10
- IRON,112,112,112
- MORT,224,224,224
- LIFE,12,172,0
- DLAY,117,53,144
- CO2,102,102,102
- DRIC,224,224,224
- BUBW,32,48,208
- STOR,80,223,223
- PVOD,121,32,32
- CONV,10,171,10
- CAUS,128,255,160
- LIGH,255,255,192
- TESC,112,112,64
- DEST,255,51,17
- SPNG,255,190,48
- RIME,204,204,204
- FOG,170,170,170
- BCLN,255,208,64
- LOVE,255,48,255
- DEUT,0,21,63
- PUMP,10,10,59
- FWRK,102,102,102
- PIPE,68,68,68
- FRZZ,192,224,255
- FRZW,16,32,192
- GRAV,255,224,160
- BIZR,0,255,119
- BIZG,0,255,187
- BIZS,0,228,85
- INST,64,64,57
- ISOZ,170,48,208
- ISZS,102,32,137
- PRTI,235,89,23
- PRTO,0,32,235
- PSTE,170,153,170
- PSTS,119,102,119
- ANAR,255,255,238
- VINE,7,154,0
- INVS,0,204,204
- EQVE,255,224,160
- SHLD,170,170,170
- SHD2,119,119,119
- SHD3,68,68,68
- SHD4,33,33,33
- LOLZ,86,146,18
- WIFI,64,160,96
- FILT,0,0,86
- ARAY,255,187,0
- BOMB,255,242,136
- C-5,32,80,224
- SING,36,36,36
- QRTZ,170,221,221
- PQRT,136,187,187
- EMP,102,170,255
- BREL,112,112,96
- ELEC,223,239,255
- ACEL,0,153,204
- DCEL,153,204,0
- TNT,192,80,80
- IGNC,192,176,80
- BOYL,10,50,0
- GEL,255,153,0
- TTAN,144,144,144
- EMBR,255,242,136
- HYGN,80,112,255
- SOAP,245,245,220
- BHOL,32,32,32
- WHOL,255,255,255
- MERC,115,107,109
- PBCN,59,29,10
- GPMP,10,59,59
- CLST,228,164,164
- WWLD,255,204,0
- GBMB,17,68,187
- FRAY,0,187,255
- RPEL,153,204,0
- PPIP,68,68,102
- DTEC,253,157,24
- DMG,136,255,136
- TSNS,253,0,213
- VIBR,0,80,0
- BVBR,0,80,0
- CRAY,187,255,0
- PSTN,170,153,153
- FRME,153,153,136
- GOLD,220,173,44
- TUNG,80,80,80
- PSNS,219,32,32
- ]]
- local f = io.open('element colors.txt', 'w')
- f:write(fileWriteStr)
- f:close()
- function getNums(str) -- Get the key values out of the string (used later)
- local numMat = {}
- local numStr = ''
- local count = 1
- for k = 1, #str do
- cstr = string.sub(str, k, k)
- if string.gsub(cstr, '%a', '') ~= '' and string.gsub(cstr, '%d', ' ') == ' ' then
- isNum = true
- numStr = numStr .. cstr
- if k == #str then
- numMat[count] = tonumber(numStr)
- end
- elseif cstr == ',' and isNum == true then
- numMat[count] = tonumber(numStr)
- numStr = ''
- count = count + 1
- else
- isNum = false
- end
- end
- return numMat
- end
- function lines(file) -- Used for reading lines of a file and returning them as a matrix
- local rLines = {}
- local lines = io.lines(file)
- local bLines = io.lines(file)
- local k = 1
- while lines() do
- rLines[k] = bLines()
- k = k +1
- end
- return rLines
- end
- function generateFile(f1, f2) -- used to set up colors file
- local clines = lines(f2)
- local f = io.open(f1, 'w')
- local r = {}
- local g = {}
- local b = {}
- local e = {}
- for k = 1, #clines do
- local cline = clines[k]
- local nums = getNums(cline)
- local celem = string.gsub(string.sub(cline, 1, 4), ',', '')
- r[k] = nums[1]
- g[k] = nums[2]
- b[k] = nums[3]
- e[k] = celem
- end
- f:write('r = {')
- for k = 1, #clines do
- if k == #clines then
- f:write(r[k], '}\ng = {')
- else
- f:write(r[k], ', ')
- end
- end
- for k = 1, #clines do
- if k == #clines then
- f:write(g[k], '}\nb = {')
- else
- f:write(g[k], ', ')
- end
- end
- for k = 1, #clines do
- if k == #clines then
- f:write(b[k], '}\ne = {')
- else
- f:write(b[k], ', ')
- end
- end
- for k = 1, #clines do
- if k == #clines then
- f:write('\'', string.gsub(e[k], '-', ''), '\'', '}')
- else
- f:write('\'', string.gsub(e[k], '-', ''), '\'', ', ')
- end
- end
- f:close()
- end
- generateFile('plottingElems.txt', 'element colors.txt')
- dofile('plottingelems.txt')
- function hextonum(str)
- local num = 0
- for k = 1, #str do
- local cstr = string.lower(string.sub(str, k, k))
- if cstr == 'a' then n = 10
- elseif cstr == 'b' then n = 11
- elseif cstr == 'c' then n = 12
- elseif cstr == 'd' then n = 13
- elseif cstr == 'e' then n = 14
- elseif cstr == 'f' then n = 15
- else n = tonumber(cstr)
- end
- num = num + (n * 16 ^ (#str - k))
- end
- return num
- end
- function range(num1, num2, range)
- if type(num1) == 'number' and type(num2) == 'number' and type(range) == 'number' then
- return math.abs(num1 - num2) <= range
- else
- return nil
- end
- end
- function getElem(res, pTest, nTest)
- if nTest == nil or nTest == false then
- nTestBool = true
- else
- nTestBool = false
- end
- local rec = res
- for k = 1, #r do
- local tr = r[k]
- local tg = g[k]
- local tb = b[k]
- if tr >= cr - rec and tr <= cr + rec then
- if tg >= cg - rec and tg <= cg + rec then
- if tb >= cb - rec and tb <= cb + rec then
- if isPositive(tr - cr) == isPositive(tg - cg) and isPositive(tr - cr) == isPositive(tb - cb) or pTest then
- --if range(tr, cr, nTest) and range(tg, cg, nTest) and range(tb, cb, nTest) or nTestBool then -- WIP
- elemDrawType = e[k]
- rec = math.floor((math.abs(r[k] - cr) + math.abs(g[k] - cg) + math.abs(b[k] - cb)) / 3)
- --end
- end
- end
- end
- end
- end
- end
- function isPositive(num) --For testing divergence direction of colors
- return num > 0
- end
- function ePlot(file, res, ptest, ltest)
- --Get image type
- itype = string.sub(file, -4, -1)
- itype = string.gsub(itype, '%.', '')
- --Open image
- if itype == 'jpeg' or itype == 'jpg' then
- im = gd.createFromJpeg(file)
- elseif itype == 'png' then
- im = gd.createFromPng(file)
- elseif itype == 'gif' then
- im = gd.createFromGif(file)
- end
- --Height, width
- local w, h = im:sizeXY()
- --is it bigger than the screen?
- if w >= 610 then
- w = 610
- end
- if h > 380 then
- h = 380
- end
- --Run through the image
- for x = 0, w do
- for y = 0, h do
- --Get color as hex string...
- color = tostring(bit.tohex(im:getPixel(x, y)))
- --and take the r, g, b individual values.
- cr = hextonum(string.sub(color, 3, 4))
- cg = hextonum(string.sub(color, 5, 6))
- cb = hextonum(string.sub(color, 7, 8))
- if cr <= 32 and cg <= 32 and cb <= 32 then --Black - Improving speed by not looking through all the elements
- elemDrawType = 'BRAY'
- elseif cr == 255 and cg == 255 and cb == 255 then -- White
- elemDrawType = 'WHOL'
- else
- getElem(res, ptest, ltest)
- end
- --Draw a pixel
- tpt.create(x, y, elemDrawType)
- if elemDrawType == 'PUMP' or elemDrawType == 'GPMP' or elemDrawType == 'LAVA' then
- tpt.set_property('life', 0, x, y)
- elseif elemDrawType == 'QRTZ' or elemDrawType == 'PQRT' or elemDrawType == 'STOR' or elemDrawType == 'CLST' then
- tpt.set_property('tmp', 5, x, y)
- elseif elemDrawType == 'SPRK' then
- tpt.create(x, y, 'METL')
- tpt.create(x, y, 'SPRK')
- elseif elemDrawType == 'BRAY' then
- tpt.set_property('life', (cr + cg + cb) * 1.3, x, y)
- tpt.set_property('tmp', 1, x, y)
- end
- if debugging == true then print(color) end
- tpt.set_property('dcolor', '0xFF' .. bit.tohex(cr, 2) .. bit.tohex(cg, 2) .. bit.tohex(cb, 2), x, y)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment