Home Download Buy Blog Forum Support

Web Colors

Web Colors

Postby jbrooksuk on Wed Jun 22, 2011 4:21 pm

New feature, new plugin. Makes use of the new show_quick_panel() API!

Description:
Displays a list of web colors in the quick panel API along with the corresponding hex code (indigo => #4B0082). Simply select the color from the quick panel and the hex code will be inserted into the current cursor or cursors positions.

Code:
Code: Select all
import sublime, sublime_plugin
import webcolors

class WebColorsCommand(sublime_plugin.WindowCommand):
   colorList = []

   def __init__(self, *args, **kwargs):
      super(WebColorsCommand, self).__init__(*args, **kwargs)
      self.generateColorDialog()

   def run(self):
      self.window.show_quick_panel(self.colorList , self.callback)

   def callback(self, index):
      if (index > -1):
         colorValue = self.colorList[index][1]
         self.window.active_view().run_command("insert_web_colors", {"value": colorValue})

   def generateColorDialog(self):
      for name, color in webcolors.css3_names_to_hex.iteritems():
         self.colorList.append([name, color.upper()])

class InsertWebColorsCommand(sublime_plugin.TextCommand):
   def run(self, edit, value):
      for region in self.view.sel():
         self.view.replace(edit, region, value)

Thanks to bizoo for the tweaks and Python tuitions!

Dependencies:
This plugin relies on webcolors.py, you'll need a copy and can grab it from webcolors.py, copy it in to your User directory.

I set the binding to:
Code: Select all
{ "keys": ["ctrl+shift+u"], "command": "web_colors" }


Have fun.
Last edited by jbrooksuk on Thu Jun 23, 2011 1:55 pm, edited 2 times in total.
jbrooksuk
 
Posts: 782
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Web Colors

Postby bizoo on Thu Jun 23, 2011 9:18 am

Have you test it before posting ?

Doesn't work at all:
Code: Select all
>>> window.run_command('web_colors')
Traceback (most recent call last):
  File ".\web_colors.py", line 8, in callback
    colorValue = colorValueKeys[index]
NameError: global name 'colorValueKeys' is not defined

And you don't check -1 return value when user hit ESCAPE
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Web Colors

Postby jbrooksuk on Thu Jun 23, 2011 9:51 am

I did test it, I've been using it. Not sure what happened but I've updated the code in the original post!

Ah it has a -1 return value. Didn't even notice that! Fixed!
jbrooksuk
 
Posts: 782
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Web Colors

Postby bizoo on Thu Jun 23, 2011 10:21 am

May I give you an advice ?
Don't use global variable if you don't share it between classes or modules.

You could make colorList a class variable and only initialize it in the constructor of the class.
This way you only run generateColorDialog once and not at each call.

Code: Select all
import sublime, sublime_plugin
import webcolors

class WebColorsCommand(sublime_plugin.WindowCommand):
   colorList = []

   def __init__(self, *args, **kwargs):
      super(WebColorsCommand, self).__init__(*args, **kwargs)
      self.generateColorDialog()

   def run(self):
      self.window.show_quick_panel(self.colorList , self.callback)

   def callback(self, index):
      if (index > -1):
         colorValue = self.colorList[index][1]
         self.window.active_view().run_command("insert_web_colors", {"value": colorValue})

   def generateColorDialog(self):
      for name, color in webcolors.css3_names_to_hex.iteritems():
         self.colorList.append([name, color.upper()])

class InsertWebColorsCommand(sublime_plugin.TextCommand):
   def run(self, edit, value):
      for region in self.view.sel():
         self.view.replace(edit, region, value)
Last edited by bizoo on Thu Jun 23, 2011 12:48 pm, edited 1 time in total.
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Web Colors

Postby jbrooksuk on Thu Jun 23, 2011 10:45 am

bizoo wrote:May I give you an advice ?
Don't use global variable if you don't share it between classes or modules.

You could make colorList a class variable and only initialize it in the constructor of the class.
This way you only run generateColorDialog once and not at each call.

Can you tell I'm not a Python developer?

I was wondering why it was being created every time! Now I know. Thanks! :)
jbrooksuk
 
Posts: 782
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Web Colors

Postby rchl on Thu Jun 23, 2011 12:27 pm

It wouldn't hurt to say what it does and how to use it. And you could update your first post with latest changes.
rchl
 
Posts: 148
Joined: Fri Mar 04, 2011 11:02 pm

Re: Web Colors

Postby rchl on Thu Jun 23, 2011 12:33 pm

BTW. Updated version, without global variable, doesn't work either.

colorList has to be accessed using self.colorList.
rchl
 
Posts: 148
Joined: Fri Mar 04, 2011 11:02 pm

Re: Web Colors

Postby bizoo on Thu Jun 23, 2011 12:52 pm

rchl wrote:BTW. Updated version, without global variable, doesn't work either.

colorList has to be accessed using self.colorList.

Updated my previous post.

This is why you MUST ALWAYS try your plugin with a new instance of ST, not the one you have used to develop it.
And this is why global variable are evil :evil:

Sorry.
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Web Colors

Postby jbrooksuk on Thu Jun 23, 2011 1:52 pm

Crap.

I thought I copied the new version across to Sublime, but I musn't have actually copied anything :oops:
jbrooksuk
 
Posts: 782
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Web Colors

Postby jbrooksuk on Thu Jun 23, 2011 1:56 pm

rchl wrote:It wouldn't hurt to say what it does and how to use it. And you could update your first post with latest changes.

Updated first post. Check it out :)
jbrooksuk
 
Posts: 782
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 5 guests