Color Skill Tester using WxPython

bumsfeld 0 Tallied Votes 395 Views Share

The little program allows eye patients to test their color skills. They are given a random color rectangle by the computer and are then asked to match the color as closely as they can using sliders of the three basic colors. An evaluation button examines the closeness of the results.

# Colour Matching Skill Tester
# compare the color set by the computer with your own slider operated perception
# adopted from vegaseat's slider code by HAB
# tested on a Windows XP computer with Python 2.4

import random
import wx

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel with 2 labels, 3 sliders, inherits wx.Panel """
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour("white")
        str1 = ""
        
        self.Red1 = random.randrange(256)
        self.Green1 = random.randrange(256)
        self.Blue1 = random.randrange(256)
        # label1 with random RGB colour values
        self.label1 = wx.StaticText(self, -1, str1 , (10, 10), (120, 100))
        self.label1.SetBackgroundColour((self.Red1, self.Green1, self.Blue1))
        
        self.Red2 = 125
        self.Green2 = 125
        self.Blue2 = 125
        # label2 with RGB colour values from sliders
        self.label2 = wx.StaticText(self, -1, str1 , (10, 120), (120, 100))
        self.label2.SetBackgroundColour((self.Red2, self.Green2, self.Blue2))
        
        # init value = 125, min value = 0, max value = 255 for colours
        self.sliderR = wx.Slider(self, -1, self.Red2, 0, 255, (140, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Red" , (160, 205))
        self.sliderG = wx.Slider(self, -1, self.Green2, 0, 255, (200, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Green" , (220, 205))
        self.sliderB = wx.Slider(self, -1, self.Blue2, 0, 255, (260, 10), (50, 200),
            wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        wx.StaticText(self, -1, "Blue" , (280, 205))
        
        # result labels
        self.label3 = wx.StaticText(self, -1, "", (160, 230))
        self.label4 = wx.StaticText(self, -1, "", (10, 265))

        # all three sliders ...
        self.Bind(wx.EVT_SCROLL, self.slideColour)
       
        # button click
        self.button1 = wx.Button(self, -1, "Evaluate", (10, 230))
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
        
    def slideColour(self, event):
        self.Red2 = self.sliderR.GetValue()
        self.Green2 = self.sliderG.GetValue()
        self.Blue2 = self.sliderB.GetValue()
        colour1 = wx.Colour(self.Red2, self.Green2, self.Blue2)
        
        # This does the trick, thanks to Peter K. !!!
        self.label2.ClearBackground()
        self.label2.SetBackgroundColour(colour1)
    
    def button1Click(self,event):
        str1 = "Upper  R=%d G=%d B=%d" % (self.Red1, self.Green1, self.Blue1)
        str2 = "Lower  R=%d G=%d B=%d" % (self.Red2, self.Green2, self.Blue2)
        diffR = self.Red1-self.Red2
        diffG = self.Green1-self.Green2
        diffB = self.Blue1-self.Blue2
        str3 = "Diff.  R=%d G=%d B=%d" % (diffR, diffG, diffB)
        self.label3.SetLabel(str1 + '\n' + str2 + '\n' + str3)
        # total up the differences ...
        diffTot = abs(diffR) + abs(diffG) + abs(diffB)
        if diffTot < 20:
            self.label4.SetLabel("very good")
        elif diffTot < 40:
            self.label4.SetLabel("good")
        elif diffTot < 60:
            self.label4.SetLabel("satisfactory")
        elif diffTot < 90:
            self.label4.SetLabel("just passed")
        else:
            self.label4.SetLabel("try it again")


app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "Colour Skill Tester V1.1 HAB", size = (350, 330))
MyPanel(frame,-1)
frame.Show(True)
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.