-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcolour.py
111 lines (97 loc) · 3.92 KB
/
colour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# This file is part of Mediary's Caspar Client.
# Copyright (C) 2018 Mediary Limited. All rights reserved.
#
'''
Paired colour picker widgetry
'''
import wx
class ColourPicker(wx.Button):
'''
Widget for triggering a colour picker
Colours are stored internally as wx.Colour, converted by get()
'''
def __init__(self, parent, label, initial, font=None, style=0):
''' Initial value should be an HTML #rrggbb triplet (though can be anything that wx.Colour.Set() accepts) '''
super(ColourPicker, self).__init__(parent, label=label, style=style)
self.htmlColor = initial
self.current = wx.Colour()
#self.current.Set(initial)
self.updateColor()
self.parent = parent
if font:
self.SetFont(font)
self.Bind(wx.EVT_BUTTON, self.do_pick)
def do_pick(self, event):
data = wx.ColourData()
#data.SetChooseFull(True)
data.SetColour(self.current)
dlg = wx.ColourDialog(self, data)
if dlg.ShowModal() == wx.ID_OK:
self.htmlColor = dlg.GetColourData().Colour.GetAsString(wx.C2S_HTML_SYNTAX)
self.updateColor()
self.parent.update_patch()
dlg.Destroy()
def updateColor(self,newColor=None):
if newColor is None:
newColor = self.htmlColor
self.current.Set(newColor)
def get(self):
''' Returns the internal wxColour as an RGB triplet '''
return self.current.GetAsString(wx.C2S_HTML_SYNTAX)
class PairedColourPicker(wx.Panel):
'''
Widget for choosing a pair of colours for text.
'''
def __init__(self, parent, initfg='#ffff00', initbg='#0000ff', notifyfn=None, label_patch=' A on B ', label_inverse=' B on A ', buttonAlabel='Colour A', buttonBlabel='Colour B', font=None, sizerFlags = wx.HORIZONTAL, sizerSpace = 10, buttonStyle=0):
super(PairedColourPicker, self).__init__(parent)
self.parent = parent
self.notifyfn = notifyfn
self.patch = None
self.patch2 = None
sizer = wx.BoxSizer(sizerFlags)
self.SetSizer(sizer)
self.fg = ColourPicker(self, buttonAlabel, initfg, font, buttonStyle)
self.bg = ColourPicker(self, buttonBlabel, initbg, font, buttonStyle)
sizer.Add(self.fg)
sizer.AddSpacer(sizerSpace)
sizer.Add(self.bg)
if label_patch is not None:
self.patch = wx.Panel(self)
self.patchtext = wx.StaticText(self.patch, label=label_patch)
font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD)
self.patchtext.SetFont(font)
inner = wx.BoxSizer(wx.VERTICAL)
self.patch.SetSizer(inner)
inner.Add(self.patchtext, 0, wx.CENTRE)
sizer.AddSpacer(10)
sizer.Add(self.patch, flag=wx.LEFT|wx.RIGHT, border=5)
self.update_patch()
else:
self.patch = None
if label_inverse is not None:
self.patch2 = wx.Panel(self)
self.patch2text = wx.StaticText(self.patch2, label=label_inverse)
font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD)
self.patch2text.SetFont(font)
inner = wx.BoxSizer(wx.VERTICAL)
self.patch2.SetSizer(inner)
inner.Add(self.patch2text, 0, wx.CENTRE)
sizer.AddSpacer(10)
sizer.Add(self.patch2, flag=wx.LEFT|wx.RIGHT, border=5)
self.update_patch()
else:
self.patch2 = None
def update_patch(self):
if self.patch:
self.patch.SetBackgroundColour(self.bg.current)
self.patchtext.SetForegroundColour(self.fg.current)
if self.patch2:
self.patch2.SetBackgroundColour(self.fg.current)
self.patch2text.SetForegroundColour(self.bg.current)
if self.notifyfn:
self.notifyfn()
def get_bg(self):
return self.bg.get()
def get_fg(self):
return self.fg.get()