0 Members and 2 Guests are viewing this topic.
can you please try to make a drawing-program for the NI NSPIRE CX CAS?
--DATA--drawingstatus="drawing"color={225,170,0}imgString=nilimgTable=nilimgZoom=1imgName="Open any file"--color selectnewColor={200,100,255}selColor=1--text requestrequested=""rqstr=""--errorerrtype=""--MENUfunction menuNew() on.charIn("N") endfunction menuOpen() on.charIn("O") endfunction menuSave() on.charIn("S") endfunction menuZIn() on.charIn("+") endfunction menuZOut() on.charIn("-") endfunction menuDraw() on.charIn("d") endfunction menuPaint() on.charIn("p") endfunction menuErase() on.charIn("e") endfunction menuCHex() on.charIn("h") endfunction menuCSel() on.charIn("s") endmenu={{"File", {"New (Shift+n)",menuNew}, {"Open (Shift+o)",menuOpen}, {"Save (Shift+s)",menuSave}},{"Edit", {"Zoom in (+)",menuZIn}, {"Zoom out (-)",menuZOut}},{"Tools", {"Draw (d)",menuDraw}, {"Paint (p)",menuPaint}, {"Erase (e)",menuErase}},{"Color", {"Hexadecimal (h)",menuCHex}, {"Select (s)",menuCSel}}}toolpalette.register(menu)
--MATHTOOLSmathTools={}function mathTools.base10to2(n) local str str="" if n~=0 then while n~=1 do str=str..tostring(n%2) n=math.floor(n/2) end str=str..tostring(n%2) return string.reverse(str) else return "0" endendfunction mathTools.base2to10(n) local num = 0 local ex = string.len(n) - 1 local l = 0 l = ex + 1 for i = 1, l do b = string.sub(n, i, i) if b == "1" then num = num + 2^ex end ex = ex - 1 end return num endfunction mathTools.int2Hex(int) if int<10 then return tostring(int) else if int==10 then return "A" elseif int==11 then return "B" elseif int==12 then return "C" elseif int==13 then return "D" elseif int==14 then return "E" elseif int==15 then return "F" end endend--MISCELLANEOUSfunction width() return platform.window:width() endfunction height() return platform.window:height() endfunction refresh() platform.window:invalidate() endfunction strch(str,ch) local test test=nil for i=1,string.len(str),1 do if string.sub(str,i,i)==ch then test=1 end end return testendfunction saveFile() if rqstr=="" then status="error" errtype="Please type a name" elseif strch("0123456789",string.sub(rqstr,1,1)) then status="error" errtype="Invalid name" else var.store(rqstr,imgString) imgName=rqstr endendfunction loadFile() local test test=var.recall(rqstr) if not test then status="error" errtype="File does not exist" elseif type(test)~="string" then status="error" errtype="Invalid file" else imgZoom=1 imgString=test imgTools.img2table(imgString) imgName=rqstr endendfunction loadHexColor() local isHex,tmptable isHex=1 tmptable={} if string.len(rqstr)==6 then for i=1,6,1 do currentch=string.sub(rqstr,i,i) if strch("0123456789",currentch) then tmptable[i]=tonumber(currentch) elseif strch("Aa",currentch) then tmptable[i]=10 elseif strch("Bb",currentch) then tmptable[i]=11 elseif strch("Cc",currentch) then tmptable[i]=12 elseif strch("Dd",currentch) then tmptable[i]=13 elseif strch("Ee",currentch) then tmptable[i]=14 elseif strch("Ff",currentch) then tmptable[i]=15 else isHex=nil end end else isHex=nil end if isHex then color={tmptable[1]*16+tmptable[2],tmptable[3]*16+tmptable[4],tmptable[5]*16+tmptable[6]} else status="error" errtype="Invalid hexadecimal" endend
imgTools={}function imgTools.getPixel(byte1,byte2) local str2 str2=imgTools.eightChars(mathTools.base10to2(tonumber(byte2)))..imgTools.eightChars(mathTools.base10to2(tonumber(byte1))) return {tonumber(string.sub(str2,1,1)),mathTools.base2to10(string.sub(str2,2,6)),mathTools.base2to10(string.sub(str2,7,11)),mathTools.base2to10(string.sub(str2,12,16))}endfunction imgTools.getSize(img) imgWidth=mathTools.base2to10(mathTools.base10to2(tonumber(string.sub(img,10,12)..string.sub(img,7,9)..string.sub(img,4,6)..string.sub(img,1,3)))) imgHeight=mathTools.base2to10(mathTools.base10to2(tonumber(string.sub(img,22,24)..string.sub(img,19,21)..string.sub(img,16,18)..string.sub(img,13,15))))endfunction imgTools.threeNumbers(nb) if string.len(tostring(nb))==1 then return "00"..tostring(nb) elseif string.len(tostring(nb))==2 then return "0"..tostring(nb) else return tostring(nb) endendfunction imgTools.eightChars(str) return string.rep("0",8-string.len(str))..strendfunction imgTools.convertChars(img) local finished,img2,index index=1 img2="" finished=nil while not finished do if string.sub(img,index,index)~=[[\]] then img2=img2..imgTools.threeNumbers(string.byte(string.sub(img,index,index))) index=index+1 else img2=img2..string.sub(img,index+1,index+3) index=index+4 end if index>string.len(img) then finished=1 end end return img2endfunction imgTools.img2table(str) local index str2=imgTools.convertChars(str) imgTable={} imgTools.getSize(str2) for raw=1,imgHeight do imgTable[raw]={} for column=1,imgWidth do index=(column-1)*6+61+(raw-1)*6*imgWidth imgTable[raw][column]=imgTools.getPixel(string.sub(str2,index,index+2),string.sub(str2,index+3,index+5)) end endendfunction imgTools.drawTable(gc,x,y) for raw=1,imgHeight do for column=1,imgWidth do if imgTable[raw][column][1]==1 then gc:setColorRGB(imgTable[raw][column][2]*8,imgTable[raw][column][3]*8,imgTable[raw][column][4]*8) gc:fillRect((column-1)*imgZoom+x,(raw-1)*imgZoom+y,imgZoom,imgZoom) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(x-1,y-1,imgWidth*imgZoom+1,imgHeight*imgZoom+1) end end endend
--EVENTSfunction on.backspaceKey() if status=="requesting" then rqstr=string.sub(rqstr,1,string.len(rqstr)-1) platform.window:invalidate() endendfunction on.charIn(ch) if status=="drawing" then if ch=="+" and imgZoom<8 then imgZoom=imgZoom*2 elseif ch=="-" and imgZoom>1 then imgZoom=imgZoom/2 elseif ch=="O" then status="requesting" rqstr="" requested="File name" elseif ch=="S" then if not imgString then status="error" errtype="No opened file" else status="requesting" rqstr=imgName requested="Save as" end elseif ch=="s" then status="selectingColor" selColor=1 newColor={math.floor(color[1]/5)*5,math.floor(color[2]/5)*5,math.floor(color[3]/5)*5} elseif ch=="h" then status="requesting" rqstr="" requested="Hex code" end elseif status=="requesting" and string.len(rqstr)<10 and strch("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",ch) then rqstr=rqstr..ch end refresh()endfunction on.escapeKey() status="drawing" refresh()endfunction on.enterKey() if status=="selectingColor" then color=newColor status="drawing" elseif status=="requesting" then status="drawing" if requested=="Hex code" then loadHexColor() elseif requested=="File name" then rqstr=string.lower(rqstr) loadFile() elseif requested=="Save as" then rqstr=string.lower(rqstr) saveFile() end elseif status=="error" then status="drawing" end refresh()endfunction on.arrowKey(ar) if status=="selectingColor" then if ar=="up" then selColor=selColor-1 elseif ar=="down" then selColor=selColor+1 elseif ar=="right" and newColor[selColor]<251 then newColor[selColor]=newColor[selColor]+5 elseif ar=="left" and newColor[selColor]>4 then newColor[selColor]=newColor[selColor]-5 end if selColor>3 then selColor=1 elseif selColor<1 then selColor=3 end end refresh()end
--DRAWINGfunction drawColorSelect(gc) gc:setColorRGB(200,200,255) gc:fillRect(width()/2-75,height()/2-50,150,100) gc:setColorRGB(0,0,0) gc:fillRect(width()/2-75,height()/2-50,150,15) gc:setPen("thin","smooth") gc:drawRect(width()/2-75,height()/2-50,150,100) gc:setFont("sansserif","r",10) gc:setColorRGB(255,255,255) gc:drawString("Select your color",width()/2-73,height()/2-53,"top") gc:setColorRGB(0,0,0) gc:drawString("Red :",width()/2-73,height()/2-35,"top") gc:drawString("Green :",width()/2-73,height()/2-20,"top") gc:drawString("Blue :",width()/2-73,height()/2-5,"top") for i=0,63 do gc:setColorRGB(i*4,newColor[2],newColor[3]) gc:fillRect(width()/2-23+i,height()/2-30,1,10) gc:setColorRGB(newColor[1],i*4,newColor[3]) gc:fillRect(width()/2-23+i,height()/2-15,1,10) gc:setColorRGB(newColor[1],newColor[2],i*4) gc:fillRect(width()/2-23+i,height()/2,1,10) end gc:setColorRGB(0,0,0) gc:drawRect(width()/2-23,height()/2-46+selColor*15,64,11) for i=1,3 do gc:drawString(tostring(newColor[i]),width()/2+52,height()/2-50+i*15,"top") gc:fillRect(width()/2-24+newColor[i]/4,height()/2-47+i*15,3,14) end gc:setColorRGB(color[1],color[2],color[3]) gc:fillRect(width()/2-70,height()/2+20,30,20) gc:setColorRGB(newColor[1],newColor[2],newColor[3]) gc:fillRect(width()/2-28,height()/2+20,30,20) gc:setColorRGB(0,0,0) gc:drawRect(width()/2-70,height()/2+20,30,20) gc:drawRect(width()/2-28,height()/2+20,30,20) gc:drawString(mathTools.int2Hex(math.floor(newColor[1]/16))..mathTools.int2Hex(newColor[1]%16)..mathTools.int2Hex(math.floor(newColor[2]/16))..mathTools.int2Hex(newColor[2]%16)..mathTools.int2Hex(math.floor(newColor[3]/16))..mathTools.int2Hex(newColor[3]%16),width()/2+20,height()/2+20,"top") gc:drawString("Old New",width()/2-65,height()/2+21,"top")endfunction drawRequest(gc,msg) gc:setColorRGB(200,200,255) gc:fillRect(width()/2-75,height()/2-25,150,50) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(width()/2-75,height()/2-25,150,50) gc:fillRect(width()/2-75,height()/2-25,150,15) gc:setFont("sansserif","r",10) gc:setColorRGB(255,255,255) gc:drawString(msg,width()/2-70,height()/2-28,"top") gc:setColorRGB(0,0,0) gc:drawRect(width()/2-70,height()/2,140,20) gc:drawString(rqstr,width()/2-65,height()/2,"top")endfunction drawError(gc) gc:setColorRGB(200,200,255) gc:fillRect(width()/2-75,height()/2-20,150,40) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(width()/2-75,height()/2-20,150,40) gc:fillRect(width()/2-75,height()/2-20,150,15) gc:setFont("sansserif","r",10) gc:setColorRGB(255,255,255) gc:drawString("Error",width()/2-73,height()/2-23,"top") gc:setColorRGB(0,0,0) gc:drawString(errtype,width()/2-70,height()/2-5,"top")endfunction on.paint(gc) gc:setColorRGB(color[1],color[2],color[3]) gc:fillRect(0,0,20,15) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(0,0,20,15) gc:setFont("sansserif","r",10) gc:drawString(imgName.." ("..tostring(imgZoom*100).."%)",25,0,"top") if imgTable then imgTools.drawTable(gc,30,30) end if status=="selectingColor" then drawColorSelect(gc) elseif status=="requesting" then drawRequest(gc,requested) elseif status=="error" then drawError(gc) end gc:setColorRGB(0,0,0) gc:setFont("sansserif","r",8) gc:drawString("Lua Paintbrush - Par Loic Pujet",10,height()-12,"top")end
It was supposed to be called Paintbrush, but Google told me that a MacOS software is already called "Paintbrush". Do you have any suggestions ?
Thanks to Inspired Lua for this page !
I am a proud cynic.
Draw and erase tools !