Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pyc
*.pyc
.DS_Store
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.DS_Store is a platform-specific file and should be ignored in your global .gitignore, not on a per-project basis.

157 changes: 136 additions & 21 deletions sublimecp.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ class ColorPickCommand(sublime_plugin.TextCommand):
"yellow": "FFFF00",
"yellowgreen": "9ACD32"
}
isRGB = False
isRGBA = False
colorLine = None
opacity = 1

def run(self, edit):
paste = None
Expand All @@ -210,7 +214,21 @@ def run(self, edit):
# get the currently selected color - if any
if len(sel) > 0:
selected = self.view.substr(self.view.word(sel[0])).strip()
if selected.startswith('#'): selected = selected[1:]
if selected.startswith('#'):
selected = selected[1:]
elif self.__is_valid_hex_color(selected):
selected = selected
else:
self.colorLine = RgbaColorLine(self.view.substr(self.view.line(sel[0])), self.view.line(sel[0]).begin(), sel[0].begin())
selected = self.colorLine.get_color()

if selected.startswith('rgba'):
self.isRGBA = True
selected = self.__rgba_to_hex(selected[4:])
elif selected.startswith('rgb'):
print selected[3:]
self.isRGB = True
selected = self.__rgba_to_hex(selected[3:])

svg_color_hex = self.SVGColors.get(selected, None)
if svg_color_hex != None:
Expand All @@ -220,7 +238,6 @@ def run(self, edit):
start_color = "#" + selected
start_color_osx = selected
start_color_win = self.__hexstr_to_bgr(selected)


if sublime.platform() == 'windows':

Expand All @@ -230,7 +247,7 @@ def run(self, edit):
if len(custom_colors) < 16:
custom_colors = ['0']*16
s.set('custom_colors', custom_colors)

cc = CHOOSECOLOR()
ctypes.memset(ctypes.byref(cc), 0, ctypes.sizeof(cc))
cc.lStructSize = ctypes.sizeof(cc)
Expand All @@ -244,14 +261,13 @@ def run(self, edit):
else:
color = None


elif sublime.platform() == 'osx':
location = os.path.join(sublime.packages_path(), 'ColorPicker', 'lib', 'osx_colorpicker')
args = [location]

if not os.access(location, os.X_OK):
os.chmod(location, 0755)

if start_color_osx:
args.append('-startColor')
args.append(start_color_osx)
Expand All @@ -262,30 +278,39 @@ def run(self, edit):

if not os.access(location, os.X_OK):
os.chmod(location, 0755)

if start_color:
args.append(start_color)


if sublime.platform() == 'osx' or sublime.platform() == 'linux':
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
color = proc.communicate()[0].strip()

if color:
# Check wether the color is a rgb or rgba color.
if self.isRGB or self.isRGBA:
color = self.__hex_to_rgba(color)
row = self.colorLine.get_color_line(color)
begin = self.view.line(sel[0]).begin()
end = self.view.line(sel[0]).end()
region = sublime.Region(begin, end)
self.view.replace(edit, region, row)
self.colorLine = None
else:
color = '#' + color
# replace all regions with color
for region in sel:
word = self.view.word(region)
# if the selected word is a valid color, replace it
if self.__is_valid_hex_color(self.view.substr(word)):
# include '#' if present
if self.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)

for region in sel:
word = self.view.word(region)
# if the selected word is a valid color, replace it
if self.__is_valid_hex_color(self.view.substr(word)):
# include '#' if present
if self.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 __get_pixel(self):
hdc = GetDC(0)
Expand Down Expand Up @@ -329,4 +354,94 @@ def __hexstr_to_bgr(self, hexstr):
r = int(hexstr[0:2], 16)
g = int(hexstr[2:4], 16)
b = int(hexstr[4:6], 16)
return (b << 16)| (g << 8) | r
return (b << 16)| (g << 8) | r

def __rgba_to_hex(self, pColor):
result = ''
tmp = pColor.strip()
vals = tmp.split(',')

if len(vals) == 4:
end = vals[3].find(')')
if end == -1:
self.opacity = vals[3].strip()
else:
self.opacity = vals[3][:end].strip()

for val in vals[:3]:
print val + ' | '
if val[0] == '(':
val = val[1:].strip()

if val.find(')') != -1:
val = val[0:val.find(')')]

result += ('0' + hex(int(val.strip()))[2:])[-2:]

return result

def __hex_to_rgba(self, pColor):
result = ''

r = str(int(pColor[0:2], 16))
g = str(int(pColor[2:4], 16))
b = str(int(pColor[4:6], 16))

if self.isRGB:
result = 'rgb(' + r + ', ' + g + ', ' + b + ')'
elif self.isRGBA:
result = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + self.opacity + ')'

return result


class RgbaColorLine():
# Class for managing line with one or more rgb(a)-colors
row = ''
rowBeginPos = 0
cursorPos = 0
colors = []
selectedColor = 0

def __init__(self, row, startPos, cursorPos):
self.row = row
self.rowBeginPos = startPos
self.cursorPos = cursorPos
self.__extract_colors()
if not self.colors[0]['color'] == 'empty':
self.__get_selected_color()

def __extract_colors(self):
# Extract all the colors from the line
color = None
count = self.row.count('rgb')
end = 0

if count == 0:
color = {'color': 'empty'}
self.colors.append(color)
else:
for i in range(count):
begin = self.row.find('rgb', end)
end = self.row.find(')', begin) + 1
color = {'color': self.row[begin:end], 'begin': begin, 'end': end}
self.colors.append(color)

def __get_selected_color(self):
# Get the color where the cursor is set
cursor = self.cursorPos - self.rowBeginPos
for color in range(len(self.colors)):
if self.colors[color]['begin'] < cursor and self.colors[color]['end'] > cursor:
self.selectedColor = color
break

def get_color(self):
# return the selected color
return self.colors[self.selectedColor]['color']

def get_color_line(self, color):
# return the line with new color
begin = self.colors[self.selectedColor]['begin']
end = self.colors[self.selectedColor]['end']

return self.row[0:begin] + color + self.row[end:len(self.row)]