Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

I swear, this is just whitespace!

  • Loading branch information...
commit f6ce5b9a06b81ca5e62d95469946fad29dfb59a4 1 parent 1dc5b53
@colinta colinta authored
Showing with 46 additions and 41 deletions.
  1. +46 −41 ColorPick.py
View
87 ColorPick.py
@@ -1,47 +1,52 @@
-import sublime, sublime_plugin
+import sublime
+import sublime_plugin
import subprocess
from os import path
+
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
+ 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(), 'ColorPick', 'bin', 'colorpick')]
- if start_color:
- args.append('-startColor')
- args.append(start_color)
- proc = subprocess.Popen(args, stdout=subprocess.PIPE)
- color = proc.communicate()[0]
-
- 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)
+ 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(), 'ColorPick', 'bin', 'colorpick')]
+ if start_color:
+ args.append('-startColor')
+ args.append(start_color)
+ proc = subprocess.Popen(args, stdout=subprocess.PIPE)
+ color = proc.communicate()[0]
+
+ 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)
Please sign in to comment.
Something went wrong with that request. Please try again.