forked from ggordan/GutterColor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.py
More file actions
115 lines (96 loc) · 3.7 KB
/
line.py
File metadata and controls
115 lines (96 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from os.path import join, dirname, realpath, isfile
from os import system, remove
from sublime import HIDDEN, PERSISTENT, load_settings, cache_path
import subprocess
import re
class Line:
# A digit is one-three numbers, with an optional floating point part,
# and maybe a % sign, and maybe surrounded by whitespace.
DIGIT = '\s*\d{1,3}(\.\d*)?%?\s*'
# Three digits is three digits, with commas between.
THREE_DIGITS = DIGIT + ',' + DIGIT + ',' + DIGIT
# Four digits is three digits (which we save for later),
# and then a comma and then the fourth digit.
FOUR_DIGITS = '(' + THREE_DIGITS + '),' + DIGIT
HEX_REGEXP = '#((?:[0-9a-fA-F]{3}){1,2})'
RGB_REGEXP = 'rgb\(' + THREE_DIGITS + '\)'
RGBA_REGEXP = 'rgba\(' + FOUR_DIGITS + '\)'
HSL_REGEXP = 'hsl\(' + THREE_DIGITS + '\)'
HSLA_REGEXP = 'hsla\(' + FOUR_DIGITS + '\)'
def __init__(self, view, region, file_id):
self.view = view
self.region = region
self.file_id = file_id
self.settings = load_settings("GutterColor.sublime-settings")
self.text = self.view.substr(self.region)
def has_color(self):
"""Returns True/False depending on whether the line has a color in it"""
return True if self.color() else False
def color(self):
"""Returns the color in the line, if any."""
if self.hex_color():
return self.hex_color()
if self.rgb_color():
return self.rgb_color()
if self.rgba_color():
return self.rgba_color()
if self.hsl_color():
return self.hsl_color()
return self.hsla_color()
def hex_color(self):
"""Returns the color in the line, if any hex is found."""
matches = re.search(Line.HEX_REGEXP, self.text)
if matches:
return matches.group(0)
def rgb_color(self):
"""Returns the color in the line, if any rgb is found."""
matches = re.search(Line.RGB_REGEXP, self.text)
if matches:
return matches.group(0)
def rgba_color(self):
"""Returns the color in the line, if any rgba is found."""
matches = re.search(Line.RGBA_REGEXP, self.text)
if matches:
return 'rgb(' + matches.group(1) + ')'
def hsl_color(self):
"""Returns the color in the line, if any hsl is found."""
matches = re.search(Line.HSL_REGEXP, self.text)
if matches:
return matches.group(0)
def hsla_color(self):
"""Returns the color in the line, if any rgba is found."""
matches = re.search(Line.HSLA_REGEXP, self.text)
if matches:
return 'hsl(' + matches.group(1) + ')'
def icon_path(self):
"""Returns the absolute path to the icons"""
return join(cache_path(), 'GutterColor', '%s.png' % self.color())
def relative_icon_path(self):
"""The relative location of the color icon"""
return "Cache/GutterColor/%s.png" % (self.color())
def add_region(self):
"""Add the icon to the gutter"""
self.create_icon()
self.view.add_regions(
"gutter_color_%s" % self.region.a,
[self.region],
"gutter_color",
self.relative_icon_path(),
HIDDEN | PERSISTENT
)
def erase_region(self):
"""Remove icon from the gutter"""
self.view.erase_regions("gutter_color_%s" % self.region.a)
def create_icon(self):
"""Create the color icon using ImageMagick convert"""
script = "%s -units PixelsPerCentimeter -type TrueColorMatte -channel RGBA " \
"-size 32x32 -alpha transparent xc:none " \
"-fill \"%s\" -draw \"circle 15,16 8,10\" png32:\"%s\"" % \
(self.settings.get("convert_path"), self.color(), self.icon_path())
if not isfile(self.icon_path()):
pr = subprocess.Popen(script,
shell = True,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
stdin = subprocess.PIPE)
(result, error) = pr.communicate()