Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 18:37, 29 March 2007.

A color picker in python

From Forum Nokia Wiki


When you want to let the user choose a color for a GUI element you need to display colors without annoying the user with technical details about RGB values ! This example gives you a way to do it.

Remark : this code is using the easy keyboard handling method.


// 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
 
ff00 = range(0xff, -1, -0x33)
pal = [(r,g,b) for r in ff00 for g in ff00 for b in ff00]  # web-safe 216 colors
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)
Related Discussions
Thread Thread Starter Forum Replies Last Post
Change color on CAknTextQueryDialog erst Symbian Media (Graphics & Sounds) 2 2007-07-19 22:27
Log Deletion ::CLogClient yogesh.bhople General Symbian C++ 8 2006-11-18 10:19
Adding a certificate to certificate store programmatically manishraj General Symbian C++ 44 2008-05-06 13:24
python with J2ME prashant332 Mobile Java General 1 2006-03-22 17:17
Can't switch numeric input to text input in RichTextEditor mobilepig Symbian User Interface 2 2008-01-03 05:14
 
Powered by MediaWiki