How to create a color eyedropper tool (Phoenix)

Keywords : ScreenDC, PaintDC, Timer, GetMousePosition, GetPixel.


Introduction

Simple ScreenDC example code to make a color eyedropper tool.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Example

Note : will not work on wxMac due to current limitations of the ScreenDC buffer being write only !

img_sample_one.png

   1 # eye_dropper_toy.py
   2 
   3 import wx
   4 
   5 #---------------------------------------------------------------------------
   6 
   7 class EyeDropperFrame(wx.Frame):
   8     """
   9     Simple eyedropper toy.
  10     """
  11     def __init__(self, parent, title=""):
  12         wx.Frame.__init__(self, parent, title=title)
  13 
  14         self.SetIcon(wx.Icon("wxwin.ico"))
  15 
  16         #------------
  17 
  18         # Attributes
  19         self.panel = EyeDroppwerPanel(self)
  20 
  21         #------------
  22 
  23         # Layout
  24         self.__DoLayout()
  25 
  26     #-----------------------------------------------------------------------
  27 
  28     def __DoLayout(self):
  29         """
  30         ...
  31         """
  32 
  33         sizer = wx.BoxSizer()
  34 
  35         sizer.Add(self.panel, 1, wx.EXPAND)
  36         self.SetSizer(sizer)
  37         self.SetInitialSize((300, 300))
  38 
  39 #---------------------------------------------------------------------------
  40 
  41 class EyeDroppwerPanel(wx.Panel):
  42     def __init__(self, parent):
  43         wx.Panel.__init__(self, parent)
  44 
  45         # Attributes.
  46         self.palette = PreviewPalette(self, size=(100, 100))
  47 
  48         #------------
  49 
  50         # Layout.
  51         self.__DoLayout()
  52 
  53     #-----------------------------------------------------------------------
  54 
  55     def __DoLayout(self):
  56         """
  57         ...
  58         """
  59 
  60         hsizer = wx.BoxSizer(wx.HORIZONTAL)
  61         vsizer = wx.BoxSizer(wx.VERTICAL)
  62 
  63         vsizer.AddStretchSpacer()
  64         vsizer.Add(self.palette, 0, wx.ALL | wx.EXPAND, 30)
  65         vsizer.AddStretchSpacer()
  66         hsizer.AddStretchSpacer()
  67         hsizer.Add(vsizer, 0, wx.EXPAND)
  68         hsizer.AddStretchSpacer()
  69 
  70         self.SetSizer(hsizer)
  71 
  72 #---------------------------------------------------------------------------
  73 
  74 class PreviewPalette(wx.Panel):
  75     """
  76     Panel to display the color sampleing from the eyedropper.
  77     """
  78     def __init__(self, parent, id=wx.ID_ANY,
  79                  pos=wx.DefaultPosition, size=wx.DefaultSize):
  80         wx.Panel.__init__(self, parent, id, pos, size)
  81 
  82         # Attributes.
  83         self.color = wx.BLACK
  84         self.brush = wx.Brush(self.color)
  85         self.timer = wx.Timer(self)
  86 
  87         #------------
  88 
  89         # Event Handlers.
  90         self.Bind(wx.EVT_PAINT, self.OnPaint)
  91         self.Bind(wx.EVT_TIMER, self.OnTimer)
  92         self.timer.Start(250)
  93 
  94     #-----------------------------------------------------------------------
  95 
  96     def __del__(self):
  97         """
  98         ...
  99         """
 100 
 101         if self.timer.IsRunning():
 102             self.timer.Stop()
 103 
 104 
 105     def OnPaint(self, evt):
 106         """
 107         ...
 108         """
 109 
 110         dc = wx.PaintDC(self)
 111         dc.SetBrush(self.brush)
 112         rect = self.GetClientRect()
 113 
 114         # Draw the preview square.
 115         sq_x = (rect.width / 2) - 25
 116         sq_y = (rect.height / 2) - 25
 117         dc.DrawRectangle(int(sq_x), int(sq_y), int(50), int(50))
 118 
 119         hexcode = self.color.GetAsString(wx.C2S_HTML_SYNTAX)
 120         tsize = self.GetTextExtent(hexcode)
 121         lbl_rect = wx.Rect(int(sq_x - 20), int(sq_y + 55), int(90), int(20))
 122         print(lbl_rect)
 123         dc.SetPen(wx.BLACK_PEN)
 124         dc.SetFont(self.GetFont())
 125         dc.DrawLabel(hexcode, lbl_rect, wx.ALIGN_CENTER)
 126 
 127 
 128     def OnTimer(self, evt):
 129         """
 130         ...
 131         """
 132 
 133         pos = wx.GetMousePosition()
 134         print("position :", pos)
 135 
 136         dc = wx.ScreenDC()
 137         print("ScreenDC :", dc)
 138 
 139         color = dc.GetPixel(pos[0], pos[1])
 140         if color != self.color:
 141             self.color = color
 142             self.brush.SetColour(self.color)
 143             self.Refresh()
 144 
 145 #---------------------------------------------------------------------------
 146 
 147 if __name__ == '__main__':
 148     app = wx.App(False)
 149     frame = EyeDropperFrame(None, title="Eyedropper toy")
 150     frame.Show()
 151     app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Cody Precord (eye_dropper_toy.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

12/01/20 - Ecco (Updated page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a color eyedropper tool (Phoenix) (last edited 2020-12-13 16:50:51 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.