Skip to content

Commit

Permalink
Add ability to choose colours using hex format to the ColourPickerDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Lewis committed May 20, 2019
1 parent d07f8e7 commit 2153bf3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
11 changes: 9 additions & 2 deletions brywidgets/widgetset.css
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,22 @@ input {
position: absolute;
top: 30px;
left: 286px;
width:60px;
width:70px;
height: 30px;
}

#colourpickerdialog #hexcolourbox {
position: absolute;
top: 300px;
left: 286px;
width:80px;
}

#colourpickerdialog #colourpickerselect {
position: absolute;
top: 70px;
left: 286px;
width:60px;
width:70px;
}

#colourpickerdialog #whitemask, #blackmask, #colourpointer, #hues, #huepointer {
Expand Down
28 changes: 21 additions & 7 deletions brywidgets/widgetset.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def delete(element):
def rgbtotuple(colour):
return tuple([int(x) for x in colour[4:-1].split(",")])

def tupletohex(colourtuple):
return "#"+sum((format(n, '02x') for n in colourtuple), '')

def hextotuple(hexcolour):
return tuple(int(hexcolour[i:i+2], 16) for i in [1, 3, 5])

def rgbtohwb(colour):
if isinstance(colour, str): colour = rgbtotuple(colour)
maxc = max(colour); maxi = colour.index(maxc)
Expand Down Expand Up @@ -142,15 +148,15 @@ def __init__(self, choices, onchange, size=None, initialchoice=None, id=None):

class InputBox(html.INPUT):
'''Standard input box. Parameters:
enterkeyaction: function to use if the Enter key is pressed. Takes the keypress event as argument.'''
enterkeyaction: function to use if the Enter key is pressed. Takes the string value of the input as argument.'''
def __init__(self, enterkeyaction, id=None):
html.INPUT.__init__(self, id=id)
self.enterkeyaction = enterkeyaction
self.bind("keypress", self.onKeypress)

def onKeypress(self, event):
if event.keyCode != 13: return
self.enterkeyaction(event)
self.enterkeyaction(self.value)

class SpinControl(html.DIV):
def __init__(self, initialvalue, minvalue, maxvalue, action, id=None):
Expand Down Expand Up @@ -301,7 +307,7 @@ def onClick(self, event):
global colourpickerdialog
if not colourpickerdialog: colourpickerdialog = ColourPickerDialog()
colourpickerdialog.returnaction = self.onChange
colourpickerdialog.setupfromcolour(self.style.backgroundColor)
colourpickerdialog.setupfromtuple(rgbtotuple(self.style.backgroundColor))
colourpickerdialog.show()

def onChange(self, colour):
Expand Down Expand Up @@ -791,9 +797,14 @@ def __init__(self, returnaction=None):

self.colourdemo = html.DIV("", id="colourdemo")
self <= self.colourdemo
self.hexcolourbox = InputBox(self.onhexinput, id="hexcolourbox")
self <= self.hexcolourbox
self <= Button("Select", self.onSelect, id="colourpickerselect")

self.setupfromcolour("rgb(0, 255, 255)")
self.setupfromtuple((0, 255, 255))

def onhexinput(self, hexcolour):
self.setupfromtuple(hextotuple(hexcolour))

def selecthue(self, event):
hueswatch = event.currentTarget.getBoundingClientRect()
Expand All @@ -804,17 +815,20 @@ def selecthue(self, event):
self.hue, self.colour = hwbtorgb(huenumber, self.whitealpha, self.blackalpha)
self.basecolourbox.style.backgroundColor = "rgb({},{},{})".format(*self.hue)
self.colourdemo.style.backgroundColor = "rgb({},{},{})".format(*self.colour)
self.hexcolourbox.value = tupletohex(self.colour)

def selectcolour(self, event):
x, y = event.clientX - event.currentTarget.getBoundingClientRect().left, event.clientY- event.currentTarget.getBoundingClientRect().top
(self.colourpointer.left, self.colourpointer.top) = (x-5, y-5)
(self.whitealpha, self.blackalpha) = (x/255, y/255)
hue, self.colour = hwbtorgb(self.hue, self.whitealpha, self.blackalpha)
self.colourdemo.style.backgroundColor = "rgb({},{},{})".format(*self.colour)
self.hexcolourbox.value = tupletohex(self.colour)

def setupfromcolour(self, colour):
self.colour = rgbtotuple(colour)
self.colourdemo.style.backgroundColor = colour
def setupfromtuple(self, colour):
self.colour = colour
self.colourdemo.style.backgroundColor = "rgb({},{},{})".format(*colour)
self.hexcolourbox.value = tupletohex(self.colour)

self.hue, huenumber, self.whitealpha, self.blackalpha = rgbtohwb(colour)

Expand Down

0 comments on commit 2153bf3

Please sign in to comment.