From 29428db0c9d5338db8b48c3f77d8007b6cbd1d6b Mon Sep 17 00:00:00 2001 From: Navelpluisje Date: Wed, 31 Oct 2012 09:43:06 +0100 Subject: [PATCH 1/4] Added rgb(a) compatibility --- .DS_Store | Bin 0 -> 6148 bytes sublimecp.py | 73 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c49abbabc39b407293e1826487fdb9b61021b5c7 GIT binary patch literal 6148 zcmeHK%TB{E5FFDA%0s9}4i)4G2P*Lgp|l4i5QrY&zyZ>LRMD4ODyYOEpT>{yEoIiW zP?Cm2F9^Y|v^$RNo!ArEjscj)G(G~V04j9B@&-eJNxS%ht@xTXqOlxfT%wB(p75~H zTPsunRp7TNAZxdV6AaO#r`PWobxbft#Fj4lT+xSI*+Z84xW@o7Ca1k&C+ZC&a>xd9 zOW&JYV+q(rlUa}KJtfC80_@|ATmwG8;-G+E_Klh4A7#1Cc8ZkFVI&+!HY zyw`TjG3Wk5MV1=3)#NRuNy(}i70fQ(YaLf;;h1ANC)TM*oXe1Lr!%&4->QQV^G29u z^Y}G^5BlnS&ZzdE72ugIR@ro@Q58@HRDmxAWPb?gf}zL6q1`$d>=l4mX05qVwL4zvrqY8Wi8LETb literal 0 HcmV?d00001 diff --git a/sublimecp.py b/sublimecp.py index d76f88a..d38cd95 100644 --- a/sublimecp.py +++ b/sublimecp.py @@ -199,6 +199,9 @@ class ColorPickCommand(sublime_plugin.TextCommand): "yellow": "FFFF00", "yellowgreen": "9ACD32" } + isRGB = False + isRGBA = False + opacity = 1 def run(self, edit): paste = None @@ -210,7 +213,15 @@ 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 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: @@ -220,7 +231,6 @@ def run(self, edit): start_color = "#" + selected start_color_osx = selected start_color_win = self.__hexstr_to_bgr(selected) - if sublime.platform() == 'windows': @@ -230,7 +240,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) @@ -244,14 +254,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) @@ -262,16 +271,20 @@ 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) + else: + color = '#' + color # replace all regions with color for region in sel: word = self.view.word(region) @@ -281,11 +294,10 @@ def run(self, edit): if self.view.substr(word.a - 1) == '#': word = sublime.Region(word.a - 1, word.b) # replace - self.view.replace(edit, word, '#' + color) + self.view.replace(edit, word, color) # otherwise just replace the selected region else: - self.view.replace(edit, region, '#' + color) - + self.view.replace(edit, region, color) def __get_pixel(self): hdc = GetDC(0) @@ -329,4 +341,43 @@ 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 \ No newline at end of file + 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): + print 'color: ' + 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 From e60b6ea416abeb092cdc060b9e49694097f1f8e6 Mon Sep 17 00:00:00 2001 From: Navelpluisje Date: Wed, 14 Nov 2012 15:40:55 +0100 Subject: [PATCH 2/4] Deleted DS_Store --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index c49abbabc39b407293e1826487fdb9b61021b5c7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%TB{E5FFDA%0s9}4i)4G2P*Lgp|l4i5QrY&zyZ>LRMD4ODyYOEpT>{yEoIiW zP?Cm2F9^Y|v^$RNo!ArEjscj)G(G~V04j9B@&-eJNxS%ht@xTXqOlxfT%wB(p75~H zTPsunRp7TNAZxdV6AaO#r`PWobxbft#Fj4lT+xSI*+Z84xW@o7Ca1k&C+ZC&a>xd9 zOW&JYV+q(rlUa}KJtfC80_@|ATmwG8;-G+E_Klh4A7#1Cc8ZkFVI&+!HY zyw`TjG3Wk5MV1=3)#NRuNy(}i70fQ(YaLf;;h1ANC)TM*oXe1Lr!%&4->QQV^G29u z^Y}G^5BlnS&ZzdE72ugIR@ro@Q58@HRDmxAWPb?gf}zL6q1`$d>=l4mX05qVwL4zvrqY8Wi8LETb diff --git a/.gitignore b/.gitignore index 7e99e36..d0a113f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.pyc \ No newline at end of file +*.pyc +.DS_Store \ No newline at end of file From bb301f3b89fa8e750c55cfb1348e6791f0115992 Mon Sep 17 00:00:00 2001 From: Navelpluisje Date: Mon, 24 Dec 2012 01:31:46 +0100 Subject: [PATCH 3/4] modified recognition rgb(a) Without a selection the rgb-color wasn't catched. Now when there is no selection (just the cursor) the row is fetched and the color will be extracted from it. --- sublimecp.py | 69 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/sublimecp.py b/sublimecp.py index d38cd95..a42be08 100644 --- a/sublimecp.py +++ b/sublimecp.py @@ -197,10 +197,12 @@ class ColorPickCommand(sublime_plugin.TextCommand): "white": "FFFFFF", "whitesmoke": "F5F5F5", "yellow": "FFFF00", + "yellowgreen": "rgba(78, 0, 255, 0.8)", "yellowgreen": "9ACD32" } isRGB = False isRGBA = False + replaceLine = False opacity = 1 def run(self, edit): @@ -215,13 +217,18 @@ def run(self, edit): selected = self.view.substr(self.view.word(sel[0])).strip() if selected.startswith('#'): selected = selected[1:] - elif 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:]) + else: + if not selected.startswith('rgb'): + self.replaceLine = True + selected = self.__rgb_from_row(self.view.substr(self.view.line(sel[0]))) + + 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: @@ -286,18 +293,25 @@ def run(self, edit): 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) + if not self.replaceLine: + 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) + else: + row = self.__create_new_line(self.view.substr(self.view.line(sel[0])), 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) def __get_pixel(self): hdc = GetDC(0) @@ -362,7 +376,7 @@ def __rgba_to_hex(self, pColor): if val.find(')') != -1: val = val[0:val.find(')')] - + result += ('0' + hex(int(val.strip()))[2:])[-2:] return result @@ -381,3 +395,18 @@ def __hex_to_rgba(self, pColor): result = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + self.opacity + ')' return result + + def __rgb_from_row(self, row): + begin = row.find('rgb') + if begin == -1: + return 'empty' + + end = row.find(')', begin) + 1 + + return row[begin:end] + + def __create_new_line(self, row, color): + begin = row.find('rgb') + end = row.find(')', begin) + 1 + + return row[0:begin] + color + row[end:len(row)] From f207ab08e03f0b4112e9552570258b7a0d3a645f Mon Sep 17 00:00:00 2001 From: Navelpluisje Date: Thu, 27 Dec 2012 13:03:33 +0100 Subject: [PATCH 4/4] Modification for multiple instances of rgba colors in line Added class for managing line with one or more rgb(a) colors. Modifications for handling multiple rgb(a) colors --- sublimecp.py | 87 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/sublimecp.py b/sublimecp.py index a42be08..5e68259 100644 --- a/sublimecp.py +++ b/sublimecp.py @@ -197,12 +197,11 @@ class ColorPickCommand(sublime_plugin.TextCommand): "white": "FFFFFF", "whitesmoke": "F5F5F5", "yellow": "FFFF00", - "yellowgreen": "rgba(78, 0, 255, 0.8)", "yellowgreen": "9ACD32" } isRGB = False isRGBA = False - replaceLine = False + colorLine = None opacity = 1 def run(self, edit): @@ -217,10 +216,11 @@ def run(self, edit): selected = self.view.substr(self.view.word(sel[0])).strip() if selected.startswith('#'): selected = selected[1:] + elif self.__is_valid_hex_color(selected): + selected = selected else: - if not selected.startswith('rgb'): - self.replaceLine = True - selected = self.__rgb_from_row(self.view.substr(self.view.line(sel[0]))) + 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 @@ -290,10 +290,15 @@ def run(self, edit): # 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 - if not self.replaceLine: for region in sel: word = self.view.word(region) # if the selected word is a valid color, replace it @@ -306,12 +311,6 @@ def run(self, edit): # otherwise just replace the selected region else: self.view.replace(edit, region, color) - else: - row = self.__create_new_line(self.view.substr(self.view.line(sel[0])), 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) def __get_pixel(self): hdc = GetDC(0) @@ -382,7 +381,6 @@ def __rgba_to_hex(self, pColor): return result def __hex_to_rgba(self, pColor): - print 'color: ' + pColor result = '' r = str(int(pColor[0:2], 16)) @@ -396,17 +394,54 @@ def __hex_to_rgba(self, pColor): return result - def __rgb_from_row(self, row): - begin = row.find('rgb') - if begin == -1: - return 'empty' - - end = row.find(')', begin) + 1 - return row[begin:end] - - def __create_new_line(self, row, color): - begin = row.find('rgb') - end = row.find(')', begin) + 1 - - return row[0:begin] + color + row[end:len(row)] +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)]