Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
weslly committed Oct 11, 2011
0 parents commit a622b41
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
3 changes: 3 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["ctrl+shift+c"], "command": "color_pick" }
]
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# sublime-colorpicker

Color picker plugin for Sublime Text 2 (Linux) using pygtk color selection dialog.

![Example](http://i.minus.com/ihwLvn8m29GxZ.png "Example")

Source for the original colorpicker plugin for OSX can be found at https://github.com/jnordberg/sublime-colorpick/

## installation

cd ~/.config/sublime-text-2/Packages
git clone git://github.com/weslly/sublime-colorpicker.git ColorPicker

It's important that you check out the repo as ColorPicker as the plugin will look for the color picker script in `<sublime packages dir>/ColorPicker/colorchooser.py`

## usage

`ctrl+shift+c` to insert or change a selected color
35 changes: 35 additions & 0 deletions colorchooser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import sys

color_sel = gtk.ColorSelectionDialog("Sublime Color Picker")

if len(sys.argv) > 1:
if gtk.gdk.Color(sys.argv[1]):
color_sel.colorsel.set_current_color(gtk.gdk.Color(sys.argv[1]))

if color_sel.run() == gtk.RESPONSE_OK:
color = color_sel.colorsel.get_current_color()
#Convert to 8bit channels
red = color.red / 256
green = color.green / 256
blue = color.blue / 256
#Convert to hexa strings
red = str(hex(red))[2:]
green = str(hex(green))[2:]
blue = str(hex(blue))[2:]
#Format
if len(red) == 1:
red = "0%s" % red
if len(green) == 1:
green = "0%s" % green
if len(blue) == 1:
blue = "0%s" % blue

finalcolor = red+green+blue
print finalcolor.upper()

color_sel.destroy()
48 changes: 48 additions & 0 deletions sublimecp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sublime
import sublime_plugin
from os import path
import subprocess

def is_valid_hex_color(s):
if len(s) not in (3, 6):
return False
try:
return 0 <= int(s, 16) <= 0xffffff
except ValueError:
return False

class ColorPickCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
sel = view.sel()
start_color = None

# get the currently selected color - if any
if len(sel) > 0:
selected = view.substr(view.word(sel[0])).strip()
if selected.startswith('#'): selected = selected[1:]
if is_valid_hex_color(selected):
start_color = "#"+selected

# get new color from picker
args = [path.join(sublime.packages_path(), 'ColorPicker', 'colorchooser.py')]
if start_color:
args.append(start_color)

proc = subprocess.Popen(args, stdout=subprocess.PIPE)
color = proc.communicate()[0].strip()

if color:
# replace all regions with color
for region in sel:
word = view.word(region)
# if the selected word is a valid color, replace it
if is_valid_hex_color(view.substr(word)):
# include '#' if present
if view.substr(word.a - 1) == '#':
word = sublime.Region(word.a - 1, word.b)
# replace
self.view.replace(edit, word, '#' + color)
# otherwise just replace the selected region
else:
self.view.replace(edit, region, '#' + color)

0 comments on commit a622b41

Please sign in to comment.