Skip to content

Commit

Permalink
Merge pull request #87 from ggordan/web-colors
Browse files Browse the repository at this point in the history
Added support for web color names. Closes #16
  • Loading branch information
jbrooksuk committed Aug 26, 2015
2 parents 1e03aed + fc9151d commit 4084b5c
Show file tree
Hide file tree
Showing 5 changed files with 762 additions and 2 deletions.
1 change: 0 additions & 1 deletion gutter_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def fix_scheme_in_view(view, regenerate=False, ignore_flags=False):
return
print("Could not find or access the settings file where current color_scheme ("+current_scheme+") is set.")


def fix_scheme_in_settings(settings_file,current_scheme, new_scheme, regenerate=False):
"""Change the color scheme in the given Settings to a background-corrected one"""
from os.path import join, normpath, isfile
Expand Down
19 changes: 18 additions & 1 deletion line.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os.path import join, dirname, realpath, isfile
from sublime import HIDDEN, PERSISTENT, load_settings, cache_path
import subprocess, os, glob, re, platform
import GutterColor.webcolors as webcolors

class Line:

Expand All @@ -20,8 +21,12 @@ class Line:
RGBA_REGEXP = 'rgba\(' + FOUR_DIGITS + '\)'
HSL_REGEXP = 'hsl\(' + THREE_DIGITS + '\)'
HSLA_REGEXP = 'hsla\(' + FOUR_DIGITS + '\)'
WEB_COLORS_REGEX = ''
WEB_COLORS = []

def __init__(self, view, region, file_id):
self.generate_webcolors()

self.view = view
self.region = region
self.file_id = file_id
Expand All @@ -34,6 +39,8 @@ def has_color(self):

def color(self):
"""Returns the color in the line, if any."""
if self.web_color():
return self.web_color()
if self.hex_color():
return self.hex_color()
if self.rgb_color():
Expand All @@ -47,6 +54,17 @@ def color(self):
if not self.settings.get("custom_colors") == None:
return self.custom_color()

def generate_webcolors(self):
"""Generates a list of web color names."""
self.WEB_COLORS = dict((name, color) for (name, color) in webcolors.css3_names_to_hex.items())
self.WEB_COLORS_REGEX = "("+ "|".join(self.WEB_COLORS.keys()) +")"

def web_color(self):
"""Returns the color in the line, if any CSS color name is found."""
matches = re.search(self.WEB_COLORS_REGEX, self.text)
if matches:
return matches.group(0)

def hex_color(self):
"""Returns the color in the line, if any hex is found."""
matches = re.search(Line.HEX_REGEXP, self.text)
Expand Down Expand Up @@ -94,7 +112,6 @@ def custom_color(self):
if matches:
return prefix+matches.group(group_id)+suffix


def icon_path(self):
"""Returns the absolute path to the icons"""
return join(cache_path(), 'GutterColor', '%s.png' % self.color())
Expand Down
1 change: 1 addition & 0 deletions messages/v0.3.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This version introduces highlighting of web color names!
18 changes: 18 additions & 0 deletions test.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
/* From https://developer.mozilla.org/en-US/docs/Web/CSS/color_value */

.COLORS {
background-color: black;
background-color: gray;
background-color: white;
background-color: maroon;
background-color: red;
background-color: purple;
background-color: fuchsia;
background-color: green;
background-color: lime;
background-color: olive;
background-color: yellow;
background-color: navy;
background-color: blue;
background-color: teal;
background-color: aqua;
}

.HEX3 {
background-color: #000; /* black */
background-color: #888; /* gray */
Expand Down
Loading

0 comments on commit 4084b5c

Please sign in to comment.