Sublime Forum

Web Colors

#1

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]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)
[/code]
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:

{ "keys": "ctrl+shift+u"], "command": "web_colors" }

Have fun.

0 Likes

Dev Build 2069
Color picker - color insertion
#2

Have you test it before posting ?

Doesn’t work at all:

[code]

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[/code]
And you don’t check -1 return value when user hit ESCAPE

0 Likes

#3

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!

0 Likes

#4

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]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)[/code]

0 Likes

#5

[quote=“bizoo”]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.[/quote]

Can you tell I’m not a Python developer?

I was wondering why it was being created every time! Now I know. Thanks! :smile:

0 Likes

#6

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

0 Likes

#7

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

colorList has to be accessed using self.colorList.

0 Likes

#8

[quote=“rchl”]BTW. Updated version, without global variable, doesn’t work either.

colorList has to be accessed using self.colorList.[/quote]

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 :imp:

Sorry.

0 Likes

#9

Crap.

I thought I copied the new version across to Sublime, but I musn’t have actually copied anything :blush:

0 Likes

#10

Updated first post. Check it out :smile:

0 Likes

#11

Hello @jbrooksuk,

Here is an updated version, with some pep8 fixes and removing some unused modules:

import 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)
0 Likes

#12

I’ve uploaded this to GitHub. Few things I want to do:

  • Add/remove custom colours/names

  • Formatting of the quick panel

  • I need to add the Linux keymap file

0 Likes