Advertisement
phjoe

Hex Color Viewer

Jul 28th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Hex Color Viewer
  2. # Joe, 29/07/2015
  3.  
  4. import appuifw as A
  5. import graphics as G
  6. sc=G.sysinfo.display_pixels()
  7. img=G.Image.new(sc)
  8.  
  9. def _tsize(text,font):
  10.  if not isinstance(text, unicode):
  11.   text=unicode(text)
  12.  (a,b,c)=img.measure_text(text, font=font)
  13.  w,h=(a[2]-a[0],a[3]-a[1])
  14.  return (w,h)
  15.  
  16. class Color:
  17.  def __init__(self):
  18.   self.run=0
  19.   self.val=[48]*6
  20.   self.hval=[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102]
  21.   self.curx=0
  22.   self.yy=[0]*len(self.hval)
  23.   A.app.screen='full'
  24.   self.can=A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
  25.   A.app.body=self.can
  26.   A.app.menu=[]
  27.   A.app.exit_key_handler=self.exit
  28.  
  29.  def _redraw(self,x):
  30.   if img:self.can.blit(img)
  31.  
  32.  def _event(self,sc):
  33.   k=sc['scancode']
  34.   if sc['type'] is not A.EEventKey:return
  35.   elif k==0xe: #kiri
  36.    self.curx-=1
  37.    if self.curx<0:
  38.     self.curx=len(self.val)-1
  39.   elif k==0xf: #kanan
  40.    self.curx+=1
  41.    if self.curx>len(self.val)-1:
  42.     self.curx=0
  43.   elif k==0x10: #atas
  44.    self.yy[self.curx]+=1
  45.    if self.yy[self.curx]>len(self.hval)-1:
  46.     self.yy[self.curx]=0
  47.    self.val[self.curx] = self.hval[self.yy[self.curx]]
  48.   elif k==0x11: #bawah
  49.    self.yy[self.curx]-=1
  50.    if self.yy[self.curx]<0:
  51.     self.yy[self.curx]=len(self.hval)-1
  52.    self.val[self.curx] = self.hval[self.yy[self.curx]]
  53.  
  54.  def exit(self):
  55.   self.run=0
  56.  
  57.  def _drawbox(self,rw,rh):
  58.   txt,fnt='HEX COLOR VIEWER','legend'
  59.   color=eval('0x'+''.join([chr(i) for i in self.val]))
  60.   x,y=sc[0]/2,sc[1]/2-40
  61.   yy=y+rh+20
  62.   img.rectangle((x-rw,y-rh,x+rw,y+rh),0xffffff,color)
  63.   img.text(((x-_tsize(txt,fnt)[0]/2),yy),u'%s' %txt,0xffffff,fnt)
  64.  
  65.  def _drawval(self):
  66.   vlen=len(self.val)
  67.   x,y,n=sc[0]/2,sc[1]-14,14
  68.   xx=x-(((vlen-1)*14)/2)
  69.   yy=y+8
  70.   for i in range(vlen):
  71.    img.text((xx+(i*n)-2,y),u'%s' %chr(self.val[i]),0xffffff,'legend')
  72.   img.point((xx+(self.curx*n),yy),0xdd0000,width=4)
  73.  
  74.  def main(self):
  75.   self.run=1
  76.   while self.run:
  77.    img.clear(0)
  78.    self._drawbox(80,40)
  79.    self._drawval()
  80.    self._redraw(0)
  81.    A.e32.ao_sleep(1e-04)
  82.  
  83. col=Color()
  84. col.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement