Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev' into wip_et_validation
Browse files Browse the repository at this point in the history
  • Loading branch information
isolver committed Apr 12, 2021
2 parents 2f9d8eb + 39c92f2 commit f629e02
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 172 deletions.
6 changes: 5 additions & 1 deletion psychopy/app/_psychopyApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ def colorPicker(self, event=None):
Note: units are psychopy -1..+1 rgb units to three decimal places,
preserving 24-bit color.
"""
dlg = PsychoColorPicker(None) # doesn't need a parent
if self.coder is None:
return

document = self.coder.currentDoc
dlg = PsychoColorPicker(document) # doesn't need a parent
dlg.ShowModal()
dlg.Destroy()

Expand Down
3 changes: 2 additions & 1 deletion psychopy/app/builder/dialogs/paramCtrls.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,11 @@ def __init__(self, parent, valType,
self.validate()

def colorPicker(self, evt):
dlg = PsychoColorPicker(self.GetTopLevelParent().frame)
dlg = PsychoColorPicker(self) # open a color picker
dlg.ShowModal()
dlg.Destroy()


def validate(obj, valType):
val = str(obj.GetValue())
valid = True
Expand Down
89 changes: 40 additions & 49 deletions psychopy/app/colorpicker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

import wx
import wx.stc as stc
from .panels import ColorPresets, ColorPreview
from .pages import ColorPickerPageHSV, ColorPickerPageRGB
from psychopy.colors import Color
Expand Down Expand Up @@ -34,9 +35,11 @@ def __init__(self, parent):
id=wx.ID_ANY,
title=_translate("Color Picker"),
pos=wx.DefaultPosition,
size=wx.Size(640, 480),
size=wx.Size(680, 480),
style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)

self.parent = parent

self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetMinSize(wx.Size(600, 480))

Expand All @@ -46,26 +49,18 @@ def __init__(self, parent):

# output spaces mapped to the `cboOutputSpace` object
self._spaces = {
0: 'rgba',
1: 'rgb',
2: 'rgba1',
3: 'rgb1',
4: 'rgba255',
5: 'rgb255',
6: 'hex',
7: 'hsva',
8: 'hsv'
0: 'rgb',
1: 'rgb1',
2: 'rgb255',
3: 'hex',
4: 'hsv'
}
# what to show in the output selection, maps to the spaces above
self._outputChoices = [
u'PsychoPy RGBA (rgba)',
u'PsychoPy RGB (rgb)',
u'Normalized RGBA (rgba1)',
u'Normalized RGB (rgb1)',
u'8-bit RGBA (rgba255)',
u'8-bit RGBA (rgb255)',
u'8-bit RGB (rgb255)',
u'Hex/HTML (hex)',
u'Hue-Saturation-Value-Alpha (hsva)',
u'Hue-Saturation-Value (hsv)'
]

Expand Down Expand Up @@ -120,7 +115,7 @@ def _addColorSpacePages(self):
self.pnlHSV = ColorPickerPageHSV(self.nbColorSpaces)

self.nbColorSpaces.AddPage(self.pnlRGB, _translate(u"RGB"), True)
self.nbColorSpaces.AddPage(self.pnlHSV, _translate(u"HSV/HSB"), False)
self.nbColorSpaces.AddPage(self.pnlHSV, _translate(u"HSV"), False)

def _setupUI(self):
"""Setup the UI for the color picker dialog box.
Expand Down Expand Up @@ -216,21 +211,21 @@ def _setupUI(self):

szrDlgCtrls.Add(self.cboOutputSpace, 1, wx.ALIGN_CENTER_VERTICAL, 5)

self.cmdCopyObject = wx.Button(
self, wx.ID_ANY, _translate(u"Copy as &Object"), wx.DefaultPosition,
self.cmdInsertColor = wx.Button(
self, wx.ID_ANY, _translate(u"&Insert"), wx.DefaultPosition,
wx.DefaultSize, 0)
self.cmdCopy = wx.Button(
self, wx.ID_ANY, _translate(u"Copy as &Value"), wx.DefaultPosition,
self, wx.ID_ANY, _translate(u"&Copy"), wx.DefaultPosition,
wx.DefaultSize, 0)
self.cmdClose = wx.Button(
self, wx.ID_ANY, _translate(u"&Cancel"), wx.DefaultPosition, wx.DefaultSize, 0)
self, wx.ID_ANY, _translate(u"Canc&el"), wx.DefaultPosition, wx.DefaultSize, 0)

self.cmdCopyObject.SetToolTip(
_translate(u"Copy as a `psychopy.colors.Color` object."))
self.cmdInsertColor.SetToolTip(
_translate(u"Insert color value."))
self.cmdCopy.SetToolTip(
_translate(u"Copy as a value (tuple)."))
_translate(u"Copy color value to clipboard."))

szrDlgCtrls.Add(self.cmdCopyObject, 0, wx.EXPAND, 5)
szrDlgCtrls.Add(self.cmdInsertColor, 0, wx.EXPAND, 5)
szrDlgCtrls.Add(self.cmdCopy, 0, wx.EXPAND, 5)
szrDlgCtrls.Add(self.cmdClose, 0, wx.EXPAND, 5)

Expand All @@ -245,12 +240,16 @@ def _setupUI(self):

# Connect Events
self.nbColorSpaces.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged)
self.cmdCopyObject.Bind(wx.EVT_BUTTON, self.onCopyObject)
self.cmdInsertColor.Bind(wx.EVT_BUTTON, self.onInsertValue)
self.cmdCopy.Bind(wx.EVT_BUTTON, self.onCopyValue)
self.cboOutputSpace.Bind(
wx.EVT_CHOICE, self.onOutputSpaceChanged)
self.cmdClose.Bind(wx.EVT_BUTTON, self.onClose)

# disable insert if parent is not a valid context, allow copying though
if self.parent is None:
self.cmdInsertColor.Enable(False)

def _copyToClipboard(self, text):
"""Copy text to the clipboard.
Expand Down Expand Up @@ -289,27 +288,17 @@ def getOutputValue(self):
"""
outputSpace = self.cboOutputSpace.GetSelection()
dlgCol = self.GetTopLevelParent().color
if outputSpace == 0: # RGBA
colorOut = '({:.4f}, {:.4f}, {:.4f}, {:.4f})'.format(*dlgCol.rgba)
elif outputSpace == 1: # RGB
colorOut = '({:.4f}, {:.4f}, {:.4f})'.format(*dlgCol.rgb)
elif outputSpace == 2: # RGBA1
colorOut = '({:.4f}, {:.4f}, {:.4f}, {:.4f})'.format(*dlgCol.rgba1)
elif outputSpace == 3: # RGB1
colorOut = '({:.4f}, {:.4f}, {:.4f})'.format(*dlgCol.rgb1)
elif outputSpace == 4: # RGBA255
r, g, b = [int(i) for i in dlgCol.rgba255[:3]]
alpha = dlgCol.alpha
colorOut = '({:d}, {:d}, {:d}, {:.4f})'.format(r, g, b, alpha)
elif outputSpace == 5: # RGB255
colorOut = '({:d}, {:d}, {:d})'.format(
if outputSpace == 0: # RGB
colorOut = '{:.4f}, {:.4f}, {:.4f}'.format(*dlgCol.rgb)
elif outputSpace == 1: # RGB1
colorOut = '{:.4f}, {:.4f}, {:.4f}'.format(*dlgCol.rgb1)
elif outputSpace == 2: # RGB255
colorOut = '{:d}, {:d}, {:d}'.format(
*[int(i) for i in dlgCol.rgb255])
elif outputSpace == 6: # Hex
elif outputSpace == 3: # Hex
colorOut = "'{}'".format(dlgCol.hex)
elif outputSpace == 7: # HSVA
colorOut = '({:.4f}, {:.4f}, {:.4f}, {:.4f})'.format(*dlgCol.hsva)
elif outputSpace == 8: # HSV
colorOut = '({:.4f}, {:.4f}, {:.4f})'.format(*dlgCol.hsv)
elif outputSpace == 4: # HSV
colorOut = '{:.4f}, {:.4f}, {:.4f}'.format(*dlgCol.hsv)
else:
raise ValueError(
"Invalid output color space selection. Have you added any "
Expand All @@ -332,14 +321,16 @@ def onPageChanged(self, event):
self._updateColorSpacePage()
event.Skip()

def onCopyObject(self, event):
def onInsertValue(self, event):
"""Event to copy the color to the clipboard as a an object.
"""
outputSpace = self.cboOutputSpace.GetSelection()
toReturn = "colors.Color({}, space='{}')".format(
self.getOutputValue(), self._spaces[outputSpace])
self._copyToClipboard(toReturn)
if isinstance(self.parent, wx.TextCtrl):
self.parent.SetValue(self.getOutputValue())
elif isinstance(self.parent, stc.StyledTextCtrl):
self.parent.InsertText(
self.parent.GetCurrentPos(), "(" + self.getOutputValue() + ")")

self.Close()
event.Skip()

Expand Down
Loading

0 comments on commit f629e02

Please sign in to comment.