Ken Kinder's personal website

WingIDE Colorpicker

Dun dun dun! This is a plugin for WingIDE. When editing code, I got tired of constantly googling “color picker” when I needed to make a hex color for my CSS.

"""
Adds a color chooser to WingIDE.
 
Copyright (c) 2010, Ken Kinder All rights reserved.
 
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
 
from guiutils import wgtk as gtk
import wingapi
 
def _getclipboard():
    editor = wingapi.gApplication.GetActiveEditor()
    doc = editor.GetDocument()
    start, end = editor.GetSelection()
    return doc.GetCharRange(start, end)
 
def _setclipboard(txt):
    editor = wingapi.gApplication.GetActiveEditor()
    doc = editor.GetDocument()
    start, end = editor.GetSelection()
 
    doc.BeginUndoAction()
    try:
        doc.DeleteChars(start, end-1)
        doc.InsertChars(start, txt)
    finally:
        doc.EndUndoAction()
 
def colorpicker():
    w = gtk.ColorSelectionDialog(title="Color Picker")
    w.colorsel.set_has_palette(True)
 
    if _getclipboard():
        w.colorsel.set_current_color(gtk.gdk.color_parse(_getclipboard()))
 
    response = w.run()
    if response == gtk.RESPONSE_OK:
        r = ('%02X' % w.colorsel.get_current_color().red)[0:2]
        g = ('%02X' % w.colorsel.get_current_color().green)[0:2]
        b = ('%02X' % w.colorsel.get_current_color().blue)[0:2]
        _setclipboard('#%s%s%s' % (r, g, b))
 
    w.hide()
    w.destroy()

Dump this code in a file in your ~/.wingide3/scripts directory, run reload-scripts, and you have a colorpicker command. It will show a gtk color picker and insert that color in your code in #rrggbb format. If you have a color selected in your buffer at the time you invoke the command, it will be parsed, be the default in the editor, and replaced when you’re done.

It doesn’t add any menu items. You just have to use the command directly or add your on shortcuts.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">