Skip to content

Commit

Permalink
Add initial hwb support
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Dec 1, 2015
1 parent c21197f commit f75d72a
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
{
"caption": "Color Palettes",
"command": "color_helper"
"command": "color_helper",
"args": {"mode": "palette"}
},
{
"caption": "Color Picker",
Expand Down
5 changes: 4 additions & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
},
{
"caption": "Color Helper: Show Palettes",
"command": "color_helper"
"command": "color_helper",
"args": {
"mode": "palette"
}
},
{
"caption": "Color Helper: Show Color Info",
Expand Down
83 changes: 73 additions & 10 deletions color_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
HSLA_COLOR = '''[>>>](__convert__:%s:hsla){: .color-helper .small} \
<span class="keyword">hsla</span>(<span class="constant numeric">%s, %s%%, %s%%, %s</span>)
'''
HWB_COLOR = '''[&gt;&gt;&gt;](__convert__:%s:hwb){: .color-helper .small} \
<span class="keyword">hwb</span>(<span class="constant numeric">%s, %s%%, %s%%</span>)
'''
HWBA_COLOR = '''[&gt;&gt;&gt;](__convert__:%s:hwba){: .color-helper .small} \
<span class="keyword">hwb</span>(<span class="constant numeric">%s, %s%%, %s%%, %s</span>)
'''

# These convert commands will use their current alpha.
HEXA_COLOR_ALPHA = '''[&gt;&gt;&gt;](__convert_alpha__:%s:hexa){: .color-helper .small} \
Expand All @@ -62,6 +68,9 @@
HSLA_COLOR_ALPHA = '''[&gt;&gt;&gt;](__convert_alpha__:%s:hsla){: .color-helper .small} \
<span class="keyword">hsla</span>(<span class="constant numeric">%s, %s%%, %s%%, %s</span>)
'''
HWBA_COLOR_ALPHA = '''[&gt;&gt;&gt;](__convert_alpha__:%s:hwba){: .color-helper .small} \
<span class="keyword">hwb</span>(<span class="constant numeric">%s, %s%%, %s%%, %s</span>)
'''

BACK_INFO_MENU = '[back](__info__){: .color-helper .small} '
BACK_PALETTE_MENU = '[back](__palettes__){: .color-helper .small} '
Expand Down Expand Up @@ -393,12 +402,23 @@ def insert_color(self, target_color, convert=None, picker=False, alpha=False):
h, l, s = hsl.tohls()
value = "%s, %s%%, %s%%" % (
util.fmt_float(h * 360.0),
util.fmt_float(s * 100.0, 1),
util.fmt_float(l * 100.0, 1)
util.fmt_float(s * 100.0),
util.fmt_float(l * 100.0)
)
if calc.alpha:
value += ', %s' % calc.alpha
value = ("hsla(%s)" if calc.alpha else "hsl(%s)") % value
elif calc.convert_hwb:
hwb = RGBA(calc.color)
h, w, b = hwb.tohwb()
value = "%s, %s%%, %s%%" % (
util.fmt_float(h * 360.0),
util.fmt_float(w * 100.0),
util.fmt_float(b * 100.0)
)
if calc.alpha:
value += ', %s' % calc.alpha
value = "hwb(%s)" % value
else:
use_upper = ch_settings.get("upper_case_hex", False)
color = calc.color
Expand Down Expand Up @@ -557,12 +577,24 @@ def format_info(self, color, alpha=None):
h, l, s = rgba.tohls()
info.append(
HSL_COLOR % (
color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0, 1), util.fmt_float(l * 100.0, 1)
color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0)
)
)
info.append(
HSLA_COLOR % (
color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0, 1), util.fmt_float(l * 100.0, 1),
color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0),
alpha if alpha else '1'
)
)
h, w, b = rgba.tohwb()
info.append(
HWB_COLOR % (
color, util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0)
)
)
info.append(
HWBA_COLOR % (
color, util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0),
alpha if alpha else '1'
)
)
Expand Down Expand Up @@ -606,13 +638,27 @@ def show_insert(self, color, palette_type, palette_name, update=False):
txt.append(
HSL_COLOR % (
rgba.get_rgb(),
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0, 1), util.fmt_float(l * 100.0, 1)
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0)
)
)
txt.append(
HSLA_COLOR_ALPHA % (
rgba.get_rgba() + "@%d" % dlevel,
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0, 1), util.fmt_float(l * 100.0, 1),
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0),
alpha
)
)
h, w, b = rgba.tohwb()
txt.append(
HWB_COLOR % (
rgba.get_rgb(),
util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0)
)
)
txt.append(
HWBA_COLOR_ALPHA % (
rgba.get_rgba() + "@%d" % dlevel,
util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0),
alpha
)
)
Expand All @@ -630,7 +676,14 @@ def show_insert(self, color, palette_type, palette_name, update=False):
txt.append(
HSLA_COLOR % (
rgba.get_rgb(),
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0, 1), util.fmt_float(l * 100.0, 1),
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0),
calc.alpha
)
)
txt.append(
HWBA_COLOR % (
rgba.get_rgb(),
util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0),
calc.alpha
)
)
Expand Down Expand Up @@ -894,7 +947,7 @@ def set_sizes(self):
sizes["medium"]
)

def run(self, edit, mode="palette", palette_name=None, color=None, auto=False):
def run(self, edit, mode, palette_name=None, color=None, auto=False):
"""Run the specified tooltip."""

self.set_sizes()
Expand Down Expand Up @@ -926,7 +979,7 @@ def run(self, edit, mode="palette", palette_name=None, color=None, auto=False):
self.no_palette = False
self.show_color_info()

def is_enabled(self, mode="palette", palette_name=None, color=None, auto=False):
def is_enabled(self, mode, palette_name=None, color=None, auto=False):
"""Check if command is enabled."""

s = sublime.load_settings('color_helper.sublime-settings')
Expand Down Expand Up @@ -1141,6 +1194,10 @@ def index_colors(self):
continue
elif m.group('hsla') and not self.color_okay('hsla'):
continue
elif m.group('hwb') and not self.color_okay('hwb'):
continue
elif m.group('hwba') and not self.color_okay('hwba'):
continue
color, alpha, alpha_dec = util.translate_color(m, self.use_argb)
color += alpha if alpha is not None else 'ff'
if not color.lower().endswith('ff'):
Expand Down Expand Up @@ -1227,6 +1284,8 @@ def payload(self):
(m.group('rgba') and self.color_okay(allowed_colors, 'rgba')) or
(m.group('hsl') and self.color_okay(allowed_colors, 'hsl')) or
(m.group('hsla') and self.color_okay(allowed_colors, 'hsla')) or
(m.group('hwb') and self.color_okay(allowed_colors, 'hwb')) or
(m.group('hwba') and self.color_okay(allowed_colors, 'hwba')) or
(m.group('webcolors') and self.color_okay(allowed_colors, 'webcolors'))
):
info = True
Expand All @@ -1243,7 +1302,11 @@ def payload(self):
(m.group('rgb_open') and self.color_okay(allowed_colors, 'rgb')) or
(m.group('rgba_open') and self.color_okay(allowed_colors, 'rgba')) or
(m.group('hsl_open') and self.color_okay(allowed_colors, 'hsl')) or
(m.group('hsla_open') and self.color_okay(allowed_colors, 'hsla'))
(m.group('hsla_open') and self.color_okay(allowed_colors, 'hsla')) or
(
m.group('hwb_open') and
(self.color_okay(allowed_colors, 'hwb') or self.color_okay(allowed_colors, 'hwba'))
)
):
execute = True
break
Expand Down
21 changes: 21 additions & 0 deletions color_helper_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self, view, point, target_color, convert, use_argb):

self.convert_rgb = False
self.convert_hsl = False
self.convert_hwb = False
self.alpha = None
self.use_argb = use_argb
self.alpha_hex = None
Expand Down Expand Up @@ -46,6 +47,9 @@ def __init__(self, view, point, target_color, convert, use_argb):
elif convert in ('hsl', 'hsla'):
self.convert_hsl = True
self.force_alpha = convert == 'hsla'
elif convert in ('hwb', 'hwba'):
self.convert_hwb = True
self.force_alpha = convert == 'hwba'

def replacement(self, m):
"""See if match is a convert replacement of an existing color."""
Expand Down Expand Up @@ -79,6 +83,13 @@ def replacement(self, m):
content = [x.strip().rstrip('%') for x in m.group('hsla_content').split(',')]
self.alpha = content[3]
self.alpha_hex = "%02x" % util.round_int(float(self.alpha) * 255.0)
elif m.group('hwb'):
self.region = sublime.Region(m.start('hwb') + self.start, m.end('hwb') + self.start)
elif m.group('hwba'):
self.region = sublime.Region(m.start('hwba') + self.start, m.end('hwba') + self.start)
content = [x.strip().rstrip('%') for x in m.group('hwba_content').split(',')]
self.alpha = content[3]
self.alpha_hex = "%02x" % util.round_int(float(self.alpha) * 255.0)
else:
found = False
return found
Expand All @@ -105,6 +116,9 @@ def completion(self, m):
self.region = sublime.Region(m.start('hsla_open') + self.start, m.end('hsla_open') + self.start + offset)
self.alpha = '1'
self.alpha_hex = 'ff'
elif m.group('hwb_open'):
offset = 1 if self.view.substr(self.point) == ')' else 0
self.region = sublime.Region(m.start('hwb_open') + self.start, m.end('hwb_open') + self.start + offset)
else:
found = False
return found
Expand Down Expand Up @@ -177,6 +191,10 @@ def replacement(self, m):
self.region = sublime.Region(m.start('hsl') + self.start, m.end('hsl') + self.start)
elif m.group('hsla'):
self.region = sublime.Region(m.start('hsla') + self.start, m.end('hsla') + self.start)
elif m.group('hwb'):
self.region = sublime.Region(m.start('hwb') + self.start, m.end('hwb') + self.start)
elif m.group('hwba'):
self.region = sublime.Region(m.start('hwba') + self.start, m.end('hwba') + self.start)
else:
found = False
return found
Expand All @@ -203,6 +221,9 @@ def completion(self, m):
self.region = sublime.Region(
m.start('hsla_open') + self.start, m.end('hsla_open') + self.start + offset
)
elif m.group('hwb_open'):
offset = 1 if self.view.substr(self.point) == ')' else 0
self.region = sublime.Region(m.start('hwb_open') + self.start, m.end('hwb_open') + self.start + offset)
else:
found = False
return found
Expand Down
24 changes: 23 additions & 1 deletion color_helper_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@
RGBA_COLOR = '''%s<span class="keyword">rgba</span>(<span class="constant numeric">%d, %d, %d, %s</span>)<br>'''
HSL_COLOR = '''%s<span class="keyword">hsl</span>(<span class="constant numeric">%s, %s%%, %s%%</span>)<br>'''
HSLA_COLOR = '''%s<span class="keyword">hsla</span>(<span class="constant numeric">%s, %s%%, %s%%, %s</span>)<br>'''
HWB_COLOR = '''%s<span class="keyword">hwb</span>(<span class="constant numeric">%s, %s%%, %s%%</span>)<br>'''
HWBA_COLOR = '''%s<span class="keyword">hwb</span>(<span class="constant numeric">%s, %s%%, %s%%, %s</span>)<br>'''

RGB_INSERT = 'rgb(%d, %d, %d)'
RGBA_INSERT = 'rgba(%d, %d, %d, %s)'
HSL_INSERT = 'hsl(%s, %s%%, %s%%)'
HSLA_INSERT = 'hsla(%s, %s%%, %s%%, %s)'
HWB_INSERT = 'hwb(%s, %s%%, %s%%)'
HWBA_INSERT = 'hwb(%s, %s%%, %s%%, %s)'

SPACER = '#00000000'
OUTER_BORDER = '#fefefeff'
Expand Down Expand Up @@ -360,7 +364,9 @@ def get_color_info(self, text):
h, l, s = rgba.tohls()
color = HSL_INSERT % (util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0))
text.append(
HSL_COLOR % (LINK % color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0))
HSL_COLOR % (
LINK % color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0)
)
)
color = HSLA_INSERT % (
util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0), self.alpha
Expand All @@ -371,6 +377,22 @@ def get_color_info(self, text):
self.alpha
)
)
h, w, b = rgba.tohwb()
color = HWB_INSERT % (util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0))
text.append(
HWB_COLOR % (
LINK % color, util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0)
)
)
color = HWBA_INSERT % (
util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0), self.alpha
)
text.append(
HWBA_COLOR % (
LINK % color, util.fmt_float(h * 360.0), util.fmt_float(w * 100.0), util.fmt_float(b * 100.0),
self.alpha
)
)

def set_sizes(self):
"""Get sizes."""
Expand Down
31 changes: 29 additions & 2 deletions color_helper_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
\b(?P<rgb>rgb\(\s*(?P<rgb_content>(?:\d+\s*,\s*){2}\d+)\s*\)) |
\b(?P<rgba>rgba\(\s*(?P<rgba_content>(?:\d+\s*,\s*){3}(?:(?:\d*\.\d+)|\d))\s*\)) |
\b(?P<hsl>hsl\(\s*(?P<hsl_content>\d+\s*,\s*(?:(?:\d*\.\d+)|\d+)%\s*,\s*(?:(?:\d*\.\d+)|\d+)%)\s*\)) |
\b(?P<hsla>hsla\(\s*(?P<hsla_content>\d+\s*,\s*(?:(?:(?:\d*\.\d+)|\d+)%\s*,\s*){2}(?:(?:\d*\.\d+)|\d))\s*\))'''
\b(?P<hsla>hsla\(\s*(?P<hsla_content>\d+\s*,\s*(?:(?:(?:\d*\.\d+)|\d+)%\s*,\s*){2}(?:(?:\d*\.\d+)|\d))\s*\)) |
\b(?P<hwb>hwb\(\s*(?P<hwb_content>\d+\s*,\s*(?:(?:\d*\.\d+)|\d+)%\s*,\s*(?:(?:\d*\.\d+)|\d+)%)\s*\)) |
\b(?P<hwba>hwb\(\s*(?P<hwba_content>\d+\s*,\s*(?:(?:(?:\d*\.\d+)|\d+)%\s*,\s*){2}(?:(?:\d*\.\d+)|\d))\s*\))'''

INCOMPLETE = r'''
(?P<hash>\#) |
\b(?P<rgb_open>rgb\() |
\b(?P<rgba_open>rgba\() |
\b(?P<hsl_open>hsl\() |
\b(?P<hsla_open>hsla\()'''
\b(?P<hsla_open>hsla\() |
\b(?P<hwb_open>hwb\()'''

COLOR_NAMES = r'\b(?P<webcolors>%s)\b' % '|'.join([name for name in csscolors.name2hex_map.keys()])

Expand Down Expand Up @@ -295,6 +298,30 @@ def translate_color(m, use_argb=False, decode=False):
color = rgba.get_rgb()
alpha_dec = content[3]
alpha = "%02X" % round_int(float(alpha_dec) * 255.0)
elif m.group('hwb'):
if decode:
content = [x.strip() for x in m.group('hwb_content').decode('utf-8').split(',')]
else:
content = [x.strip() for x in m.group('hwb_content').split(',')]
rgba = RGBA()
h = float(content[0]) / 360.0
w = float(content[1].strip('%')) / 100.0
b = float(content[2].strip('%')) / 100.0
rgba.fromhwb(h, w, b)
color = rgba.get_rgb()
elif m.group('hwba'):
if decode:
content = [x.strip() for x in m.group('hwba_content').decode('utf-8').split(',')]
else:
content = [x.strip() for x in m.group('hwba_content').split(',')]
rgba = RGBA()
h = float(content[0]) / 360.0
w = float(content[1].strip('%')) / 100.0
b = float(content[2].strip('%')) / 100.0
rgba.fromhwb(h, w, b)
color = rgba.get_rgb()
alpha_dec = content[3]
alpha = "%02X" % round_int(float(alpha_dec) * 255.0)
elif m.group('webcolors'):
try:
if decode:
Expand Down
Loading

0 comments on commit f75d72a

Please sign in to comment.