Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- ColorConverter.py
- Convert 0xRRGGBB to (RR, GG, BB)
- (RR, GG, BB) to 0xRRGGBB
- 0xRRGGBB to PIL Color
- (RR, GG, BB) to PIL Color
- PIL Color to 0xRRGGBB
- PIL Color to (RR, GG, BB)
- """
- __program__ = 'ColorConverter'
- __version__ = '1.0'
- __author__ = 'lnosKHEN'
- __desc__ = 'Convert color codes into different format RGB Color, Hex Color & PIL Color.'
- __credits__ = ["PD PyProgrammers",
- "shadow_chaser",
- "monster_of_zeus",
- "stefanblack",
- "Ak0ztik",
- "Spoke",
- "pentagon09",
- "akira94",
- "wazulu",
- "and to all Members"]
- __date__ = '04/07/12'
- __url__ = ['PinoyDen.com.ph',
- 'Pyoneers.wapka.mobi']
- import e32
- import appuifw
- import globalui
- from graphics import *
- from sysinfo import display_pixels
- class ColorConverter:
- def __init__(self):
- self.rgb_color = []
- self.saved = 0
- self.default = 0
- self.sfStats = None
- self.ssStats = None
- self.pixels = display_pixels()
- self.script_lock = e32.Ao_lock()
- def main(self):
- self.old_s = appuifw.app.screen
- self.old_b = appuifw.app.body
- self.scrn=Image.new((360,640))
- self.c = appuifw.Canvas()
- appuifw.app.body = self.c
- appuifw.app.directional_pad = False
- self.c.blit(self.scrn)
- self.c.clear(0)
- self.main_scrn()
- self.script_lock.wait()
- def main_scrn(self):
- def handle_redraw(rect):
- self.c.blit(self.scrn)
- self.c.clear(0)
- self.c.rectangle((6, 23, 360-6, 45), 0x396994, fill = 0x222222)
- self.c.rectangle((6, 27, 360-6, 70), 0x396994, fill = 0x111111)
- self.c.rectangle((6, 74, 360-6, 175), 0xFFD342, fill = 0x111111)
- self.c.rectangle((8, 101, 360-8, 167), 0x0000ff, fill = 0x444444)
- self.c.rectangle((10, 103, 360-10, 123), 0, fill = 0x104D63)
- self.c.rectangle((10, 124, 360-10, 144), 0, fill = 0x104D63)
- self.c.rectangle((10, 145, 360-10, 165), 0, fill = 0x104D63)
- self.c.rectangle((6, 180, 360-6, 218), 0x396994, fill = 0x111111)
- self.c.text((20, 38), u'Color Converter v1.0', 0x00FF00, font = 'title')
- self.c.text((20, 62), u'Convert color into 3 formats', 0x00fff0, font = 'legend')
- self.c.text((13, 95), u'3 Different Formats:', 0x00fff0, font = 'legend')
- self.c.text((23, 117), u'RGB Color', 0xffffff, font = 'legend')
- self.c.text((23, 137), u'Hex Color', 0xffffff, font = 'legend')
- self.c.text((23, 158), u'PIL Color', 0xffffff, font = 'legend')
- self.c.text((23, 195), u'Developed by:', 0x396994, font = 'legend')
- self.c.text((180, 195), u'lnosKHEN', 0xFFD342, font = 'legend')
- appuifw.app.screen = 'large'
- self.c = appuifw.Canvas(redraw_callback = handle_redraw)
- appuifw.app.body = self.c
- appuifw.app.directional_pad = False
- e32.ao_yield()
- appuifw.app.menu = [
- (u"Convert..",
- ((u"RGB Color", self.cRGB),
- (u"Hex Color", self.cHC),
- (u"PIL Color", self.cPC))),
- (u"Help", self.help),
- (u"About", self.about),
- (u"Exit", self.quit)]
- appuifw.app.exit_key_handler = self.quit
- def r_scrn(self):
- def handle_redraw(rect):
- self.c.blit(self.scrn)
- self.c.clear(0)
- self.c.rectangle((6, 23, 360-6, 45), 0x396994, fill = 0x222222)
- self.c.rectangle((6, 27, 360-6, 70), 0x396994, fill = 0x111111)
- self.c.rectangle((6, 74, 360-6, 155), 0xFFD342, fill = 0)
- self.c.rectangle((10, 90, 360-10, 90), 0x333333, fill = self.default)
- self.c.rectangle((6, 180, 360-6, 218), 0x396994, fill = 0x111111)
- self.c.text((20, 38), u'Color Converter v1.0', 0x00FF00, font = 'title')
- self.c.text((20, 62), u'Color Results:', 0x00fff0, font = 'legend')
- self.c.text((40, 115), self.sfStats, 0x84AAA5, font = 'legend')
- self.c.text((40, 135), self.ssStats, 0x84AAA5, font = 'legend')
- self.c.text((23, 195), u'Developed by:', 0x396994, font = 'legend')
- self.c.text((180, 195), u'lnosKHEN', 0xFFD342, font = 'legend')
- appuifw.app.screen = 'large'
- self.c = appuifw.Canvas(redraw_callback = handle_redraw)
- appuifw.app.body = self.c
- appuifw.app.directional_pad = False
- e32.ao_yield()
- def RGBForm(self):
- RGBMnu = [
- (u'RR - Red (0-255)', \
- 'number', 0),
- (u'GG - Green (0-255)', \
- 'number', 0),
- (u'BB - Blue (0-255)', \
- 'number', 0)]
- flags = appuifw.FFormEditModeOnly | appuifw.FFormDoubleSpaced
- clr=appuifw.Form(RGBMnu, flags)
- clr.save_hook = self.save
- clr.execute()
- if self.saved == 1:
- if clr[0][2] > 255 or clr[1][2] > 255 or clr[2][2] > 255:
- appuifw.note(u'RGB Range: 0-255 only!', 'error')
- return None
- Red = clr[0][2]
- Green = clr[1][2]
- Blue = clr[2][2]
- return Red, Green, Blue
- return None
- def save(self, sav):
- self.saved = 1
- return 1
- def cRGB(self):
- mmenu = [u'Hex Color', u'PIL Color']
- ccolor = appuifw.popup_menu(mmenu, u"Convert into:")
- if ccolor == 0:
- '''
- appuifw.app.screen = \
- self.old_s
- appuifw.app.body = \
- self.old_b
- '''
- RGB = self.RGBForm()
- self.main_scrn()
- if RGB is None or RGB is 0:
- return None
- nHex = self.RGB_Hex(RGB)
- appuifw.query(u'(%d, %d, %d) in Hex Color:' %(RGB), 'text', nHex)
- self.default = self.Hex_RGB(nHex)
- self.sfStats = u'RGB Color: ' +str(RGB).replace('L', '')
- self.ssStats = u'Hex Color: ' +nHex
- self.r_scrn()
- if ccolor == 1:
- '''
- appuifw.app.screen = \
- self.old_s
- appuifw.app.body = \
- self.old_b
- '''
- RGB = self.RGBForm()
- self.main_scrn()
- if RGB is None:
- return None
- nPIL = self.RGB_PIL(RGB)
- appuifw.query(u'(%d, %d, %d) in PIL Color:' %(RGB), 'text', nPIL)
- self.default = self.PIL_RGB(nPIL)
- self.sfStats = u'RGB Color: ' +str(RGB).replace('L', '')
- self.ssStats = u'PIL Color: ' +nPIL
- self.r_scrn()
- def cHC(self):
- mmenu = [u'RGB Color', u'PIL Color']
- ccolor = appuifw.popup_menu(mmenu, u"Convert into:")
- if ccolor == 0:
- hxc = appuifw.query(u'Hex Color:\n(0xXXXXXX or #XXXXXX):', 'text', u'0x')
- if hxc is None:
- return None
- nRGB = self.Hex_RGB(hxc)
- if nRGB is None:
- return None
- appuifw.query(u'%s in RGB Color:' %hxc, 'text', unicode(nRGB))
- self.default = self.Hex_RGB(hxc)
- self.sfStats = u'Hex Color: ' +hxc
- self.ssStats = u'RGB Color: ' +str(nRGB).replace('L', '')
- self.r_scrn()
- if ccolor == 1:
- hxc = appuifw.query(u'Hex Color:\n(0xXXXXXX or #XXXXXX):', 'text', u'0x')
- if hxc is None:
- return None
- nPIL = self.Hex_PIL(hxc)
- if nPIL is None:
- return None
- appuifw.query(u'%s in PIL Color:' %hxc, 'text', unicode(nPIL))
- self.default = self.Hex_RGB(hxc)
- self.sfStats = u'Hex Color: ' +hxc
- self.ssStats = u'PIL Color: ' +nPIL
- self.r_scrn()
- def cPC(self):
- mmenu = [u'RGB Color', u'Hex Color']
- ccolor = appuifw.popup_menu(mmenu, u"Convert into:")
- if ccolor == 0:
- PILc = appuifw.query(u'PIL Color:', 'number')
- if PILc is None:
- return None
- if len(str(PILc)) > 8:
- appuifw.note(u'Invalid PIL Color!', 'error')
- return None
- nRGB = self.PIL_RGB(PILc)
- appuifw.query(u'%s in RGB Color:' %PILc, 'text', unicode(nRGB))
- self.default = self.PIL_RGB(PILc)
- self.sfStats = u'PIL Color: ' +str(PILc)
- self.ssStats = u'RGB Color: ' +str(nRGB).replace('L', '')
- self.r_scrn()
- if ccolor == 1:
- PILc = appuifw.query(u'PIL Color:', 'number')
- if PILc is None:
- return None
- if len(str(PILc)) > 8:
- appuifw.note(u'Invalid PIL Color!', 'error')
- return None
- nHex = self.PIL_Hex(PILc)
- if nHex is None:
- return None
- appuifw.query(u'%s in Hex Color:' %PILc, 'text', unicode(nHex))
- self.default = self.PIL_RGB(PILc)
- self.sfStats = u'PIL Color: ' +str(PILc)
- self.ssStats = u'Hex Color: ' +nHex
- self.r_scrn()
- def Hex_RGB(self, color):
- sColor = color.strip()
- if sColor.startswith('#'):
- sColor = sColor[1:]
- if sColor.startswith('0x'):
- sColor = sColor[2:]
- if color < 7 or len(sColor) != 6:
- appuifw.note(u'Only 0xRRGGBB or #RRGGBB format!', 'error')
- return None
- RGB = sColor[:-4], \
- sColor[2:-2], \
- sColor[4:]
- RR, GG, BB = [int(C, 16) for C in RGB]
- return (RR, GG, BB)
- def RGB_Hex(self, color):
- hex_color = '%02X%02X%02X' %color
- return u'0x'+hex_color
- def Hex_PIL(self, color):
- sColor = color.strip()
- if sColor.startswith('#'):
- sColor = sColor[1:]
- if sColor.startswith('0x'):
- sColor = sColor[2:]
- if len(color) != 8 and len(color) != 7:
- appuifw.note(u'Only 0xRRGGBB or #RRGGBB format!', 'error')
- return None
- return unicode(int(sColor, 16))
- def RGB_PIL(self, color):
- hexColor=self.RGB_Hex(color)
- pilColor=self.Hex_PIL(hexColor)
- return pilColor
- def PIL_Hex(self, color):
- hexColor = '0x'+'%06X'%int(color)
- return hexColor
- def PIL_RGB(self, color):
- hexColor = self.PIL_Hex(color)
- return self.Hex_RGB(hexColor)
- def help(self):
- globalui.global_msg_query(u'RGB Color: 0-255 in Range.\ne.g. (255, 255, 255)\n\nHex Color: You can use HTML Colors (#XXXXXX) or use Python Colors (0xXXXXXX).\ne.g. 0xFFFFFF\n\nPIL Color: PIL stands for Python Image Library. PIL colors - Base16.\ne.g. 16777215', u'Help')
- def about(self):
- Inf1 = u'Version: '+__version__+u'\n'
- Inf2 = u'Description: ' +__desc__+u'\n'
- Inf3 = u'Author: '+__author__+u'\n'
- Inf4 = u'Credits: '+__credits__[0]+u'\n'
- Inf5 = 'URL: '+__url__[0]+u'\n'+__url__[1]
- mInfo=Inf1+Inf2+Inf3+Inf4+Inf5
- Title = u'About ' +__program__
- globalui.global_msg_query(mInfo, Title)
- def quit(self):
- AskUser = [u'Yes', u'No']
- cexit = appuifw.popup_menu(AskUser, u"Are you sure to exit?")
- if cexit == 0:
- self.script_lock.signal()
- # Best Resolution: 176x208
- if __name__ == '__main__':
- ColorConverter().main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement