Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-18 of 18 total

HTML 4.0 color names

There are 16 of them
Black  	#000000
Silver 	#C0C0C0
Gray 	#808080
White 	#FFFFFF
Maroon 	#800000
Red 	#FF0000
Purple 	#800080
Fuchsia #FF00FF
Green 	#008000
Lime 	#00FF00
Olive 	#808000
Yellow 	#FFFF00
Navy 	#000080
Blue 	#0000FF
Teal 	#008080
Aqua 	#00FFFF

From this page.

Color picker

I saw someone write a fileselect dialog a few days ago.
I think we should have more dialog type to use in our apps.
So, I write a color picker code below.
It can be converted easily into a dialog component.

// Usual code as always
import e32
from appuifw import *
from key_codes import *

class Keyboard(object):
    def __init__(self):
        self.state = {}  # is this key pressing ?
        self.buffer= {}  # is it waiting to be processed ?
    def handle_event(self, event): # for event_callback
        code = event['scancode']
        if event['type'] == EEventKeyDown:
            self.buffer[code]= 1   # put into queue
            self.state[code] = 1
        elif event['type'] == EEventKeyUp:
            self.state[code] = 0
    def pressing(self, code):      # just check
        return self.state.get(code,0)
    def pressed(self, code):       # check and process the event
        if self.buffer.get(code,0):
            self.buffer[code] = 0  # take out of queue
            return 1
        return 0

key = Keyboard()
app.body = canvas = Canvas(event_callback=key.handle_event)

def quit():
    global running
    running = 0

app.exit_key_handler = quit
running = 1

At first, I wanted to use the standard palette of s60.
But later decide to just use 216 web-safe colors.
The code is simpler, so the concept is easier to understand.
ff00 = range(0xff, -1, -0x33)
pal = [(r,g,b) for r in ff00 for g in ff00 for b in ff00]  # web-safe
map_j = range(0,12,2)+range(11,0,-2)  # make better grouping
for j in range(12):
    for i in range(18):
        k = 18*map_j[j] + i
        canvas.rectangle([(9*i+1, 9*j+1), (9*i+9, 9*j+9)], None, pal[k])

def clear_box(color=0xffffff):
    global x,y
    canvas.rectangle([(9*x, 9*y), (9*x+10, 9*y+10)], color)  # cursor

x, y = 0, 0
black_white = 0
while running:
    if key.pressed(EScancodeLeftArrow):
        clear_box()
        if x > 0: x -= 1
    if key.pressed(EScancodeRightArrow):
        clear_box()
        if x < 17: x += 1
    if key.pressed(EScancodeUpArrow):
        clear_box()
        if y > 0: y -= 1
    if key.pressed(EScancodeDownArrow):
        clear_box()
        if y < 11: y += 1
    if key.pressed(EScancodeSelect):
        color = pal[18*map_j[y] + x]
        canvas.rectangle([(1,109), (17*9+9,130)], None, color)  # show color
    black_white ^= 0xffffff  # toggle
    clear_box(black_white)
    e32.ao_sleep(0.2)

See Screenshot.

Reading a 24-bit icon in an mbm file

Here's an example how I read the icon and display it on canvas
It's a continued part of my previous mbm hack.
>>> from appuifw import *
>>> from struct import unpack
>>> def readL(f, pos=None):     # helping function
...     if pos is not None:
...         f.seek(pos)
...     return unpack('L', f.read(4))[0]
...
>>> fxmbm = 'E:\\system\\apps\\FExplorer\\FExplorer.mbm'
>>> f = open(fxmbm, 'rb')
>>> trailer = readL(f, 16)
>>> num = readL(f, trailer)   # 19 icons
>>> offset = []
>>> for i in range(num):
...   offset.append(readL(f))
...
>>> offset
[20L, 68L, 116L, 474L, 588L, 1038L, 1228L, 1636L, 1864L, 2277L, 2430L, 3735L, 41
69L, 4569L, 4697L, 5197L, 5370L, 5897L, 6062L]
>>> start = offset[2]  # folder icon
>>> f.seek(start)
>>> length = readL(f) - readL(f)    # length of data section
>>> width, height = readL(f), readL(f)  # 16 x 13
>>> f.seek(start+0x28)  # start of data section
>>> data_enc = f.read(length)   # got the data
>>> def rle24_decode(bytes):
...     out = []
...     i = 0
...     while i < len(bytes):
...         n = ord(bytes[i])
...         i += 1
...         if n < 0x80:
...             out.append( bytes[i:i+3] * (n+1) )
...             i += 3
...         else:
...             n = 0x100 - n
...             out.append( bytes[i:i+3*n] )
...             i += 3*n
...     return ''.join(out)
...
>>> data = rel24_decode(data_enc)
>>> app.body = canvas = Canvas()
>>> for j in range(height):
...     for i in range(width):
...         p = 3*(j*width+i)
...         color = [ord(data[p+k]) for k in (2,1,0)]  # It's BGR
...         canvas.point((i,j), tuple(color))
...
>>>  # folder icon is drawn on screen

Getting symbian's default palette table

After a night hacking what is the 8-bit color for mbm format.
I finally found this thead about standard palette.

So, I make this pallete mapping table to use with 8-bit icons.
palette_string = '''0x00000000
0x00CCFFFF
0x0099FFFF
0x0066FFFF
0x0033FFFF
0x0000FFFF
0x00FFCCFF
0x00CCCCFF
0x0099CCFF
0x0066CCFF
0x0033CCFF
0x0000CCFF
0x00FF99FF
0x00CC99FF
0x009999FF
0x006699FF
0x003399FF
0x000099FF
0x00FF66FF
0x00CC66FF
0x009966FF
0x006666FF
0x003366FF
0x000066FF
0x00FF33FF
0x00CC33FF
0x009933FF
0x006633FF
0x003333FF
0x000033FF
0x00FF00FF
0x00CC00FF
0x009900FF
0x006600FF
0x003300FF
0x000000FF
0x00FFFFCC
0x00CCFFCC
0x0099FFCC
0x0066FFCC
0x0033FFCC
0x0000FFCC
0x00FFCCCC
0x00CCCCCC
0x0099CCCC
0x0066CCCC
0x0033CCCC
0x0000CCCC
0x00FF99CC
0x00CC99CC
0x009999CC
0x006699CC
0x003399CC
0x000099CC
0x00FF66CC
0x00CC66CC
0x009966CC
0x006666CC
0x003366CC
0x000066CC
0x00FF33CC
0x00CC33CC
0x009933CC
0x006633CC
0x003333CC
0x000033CC
0x00FF00CC
0x00CC00CC
0x009900CC
0x006600CC
0x003300CC
0x000000CC
0x00FFFF99
0x00CCFF99
0x0099FF99
0x0066FF99
0x0033FF99
0x0000FF99
0x00FFCC99
0x00CCCC99
0x0099CC99
0x0066CC99
0x0033CC99
0x0000CC99
0x00FF9999
0x00CC9999
0x00999999
0x00669999
0x00339999
0x00009999
0x00FF6699
0x00CC6699
0x00996699
0x00666699
0x00336699
0x00006699
0x00FF3399
0x00CC3399
0x00993399
0x00663399
0x00333399
0x00003399
0x00FF0099
0x00CC0099
0x00990099
0x00660099
0x00330099
0x00000099
0x00FFFF66
0x00CCFF66
0x0099FF66
0x0066FF66
0x0033FF66
0x0000FF66
0x00FFCC66
0x00CCCC66
0x0099CC66
0x0066CC66
0x0033CC66
0x0000CC66
0x00FF9966
0x00CC9966
0x00999966
0x00669966
0x00339966
0x00009966
0x00FF6666
0x00CC6666
0x00996666
0x00666666
0x00336666
0x00006666
0x00FF3366
0x00CC3366
0x00993366
0x00663366
0x00333366
0x00003366
0x00FF0066
0x00CC0066
0x00990066
0x00660066
0x00330066
0x00000066
0x00FFFF33
0x00CCFF33
0x0099FF33
0x0066FF33
0x0033FF33
0x0000FF33
0x00FFCC33
0x00CCCC33
0x0099CC33
0x0066CC33
0x0033CC33
0x0000CC33
0x00FF9933
0x00CC9933
0x00999933
0x00669933
0x00339933
0x00009933
0x00FF6633
0x00CC6633
0x00996633
0x00666633
0x00336633
0x00006633
0x00FF3333
0x00CC3333
0x00993333
0x00663333
0x00333333
0x00003333
0x00FF0033
0x00CC0033
0x00990033
0x00660033
0x00330033
0x00000033
0x00FFFF00
0x00CCFF00
0x0099FF00
0x0066FF00
0x0033FF00
0x0000FF00
0x00FFCC00
0x00CCCC00
0x0099CC00
0x0066CC00
0x0033CC00
0x0000CC00
0x00FF9900
0x00CC9900
0x00999900
0x00669900
0x00339900
0x00009900
0x00FF6600
0x00CC6600
0x00996600
0x00666600
0x00336600
0x00006600
0x00FF3300
0x00CC3300
0x00993300
0x00663300
0x00333300
0x00003300
0x00FF0000
0x00CC0000
0x00990000
0x00660000
0x00330000
0x00000000
0x00EEEEEE
0x00DDDDDD
0x00BBBBBB
0x00AAAAAA
0x00888888
0x00777777
0x00555555
0x00444444
0x00222222
0x00111111
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00FFFFFF'''

Split and map them into (RR, GG, BB) array
palette = [tuple([int(col[i:i+2], 16) for i in range(8,2,-2)]) for col in palette_string.split('\n')]

Last night, I lost 3-4 hours figuring if 8-bit is a 8-bit true color.
I should have thought about standard palette and google them eariler.

updated
=======
I just notice the sequence pattern which should make the code shorter.
ff00 = range(0xff, -1, -0x33)
palette = [(r,g,b) for r in ff00 for g in ff00 for b in ff00] #0x336699
palette[0] = (0,0,0)
palette += [(17*i,17*i,17*i) for i in (14,13, 11,10, 8,7, 5,4, 2,1)] # grey
palette += [(0,0,0) for i in range(30)]  # empty
palette[-1] = (255,255,255)

get pixel colors of an image

I wrote a hack to get the color of a pixel.
Now pys60 1.2 has support for getpixel (though undocumented).
Cyke64 has shown me in an example.
>>> from graphics import Image
>>> im = Image.new((10,10))   # create a new 10x10 image
>>> im.clear(0xff0000)        # make it all red
>>> im.getpixel((0,0))   # top-left is red
[(255, 0, 0)]
>>> im.getpixel([(0,0), (10,10)])  # you can get multiple points
[(255, 0, 0), (255, 0, 0)]
>>> 

Get pixel colors of an image

Pys60 now has a decent Canvas and Image class.
We can draw many shapes and color them.
However, one important feature is missing, ie. getpixel().
So, you can only 'write' but not 'read' from graphics.

To get around this problem, I write a small library that
add 'getpixel' to the Image class.
Now, you can call im.getpixel(x,y) and get an (R,G,B) tuple.
# some setup
from graphics import *
im = screenshot()  # sample image

# http://larndham.net/service/pys60/getpixel.py
import getpixel
getpixel.enable(im)  # magically give Image.getpixel()
r, g, b = im.getpixel(0,0)  # top left corner
print 'Red: %s, Green:%s, Blue:%s' % (r,g,b)

Now you can do some easy image processing with getpixel.

Implementation note
===================
- This is a pure python module
- It saves an image as an uncompressed PNG file
- It reads pixel data from the file and attach it to the image.
- You need to call enable() every time you change the image.

Get the inverse of a hex color in Ruby

This method will return the inverse of a hex color, which is useful if you want to make sure that your text will show up on a colored background.

def invert_color(color)
  color.gsub!(/^#/, '')
  sprintf("%X", color.hex ^ 0xFFFFFF)
end


Example:

invert_color('#c0c0c0') #=> "3F3F3F"


Limitations:

Doesn't handle named colors or 3 digit colors (i.e. #FFF == #FFFFFF)

Python syntax coloring for series60

The new Text() control allows rich text which can be
used for syntax coloring
(screenshot)
http://photos22.flickr.com/30187174_8598b800ec_o.jpg
from appuifw import *
from pyfontify import fontify
import os, e32

src = ur"""
__version__ = "0.4"
import string, re, keyword  
commentPat = "#.*"  
# Build up a regular expression
pat = "q[^\q\n]*(\\\\[\000-\377][^\q\n]*)*q"
"""

color = { 'keyword': 0x0000ff,
          'string': 0xff00ff,
          'comment': 0x008000,
          'function': 0x008080,
          'class': 0x008080 }

t = Text()
pos = 0
for tag, start, end, sl in fontify(src):
    t.color = 0
    t.add(src[pos:start])
    t.color = color[tag]
    t.add(src[start:end])
    pos = end
t.color = 0
t.add(src[pos:])

app.body = t
e32.ao_sleep(10)

You first need to install the pyfontify module from here
http://linux.duke.edu/~mstenner/free-docs/diveintopython-3.9-1/py/pyfontify.py
« Newer Snippets
Older Snippets »
Showing 11-18 of 18 total