Skip to content

Commit

Permalink
Color fixes (#80)
Browse files Browse the repository at this point in the history
- Adjustments to match Sublime 4069 which now handles HSL color blending
  correctly.
- Handle HSL if a user use the 'deg' unit type
- Sublime doesn't support them, but support 'rad', 'grad', and 'turn'
  unit types in case Sublime ever supports them in HSL an HWB.
  • Loading branch information
facelessuser authored Mar 29, 2020
1 parent 2d4e9cd commit 3d6c3cf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
4 changes: 4 additions & 0 deletions docs/src/markdown/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- **FIX**: Color adjusters with `+` and `-` operator must have a space after the operator.
- **FIX**: `lightness()` and `saturation()` should not accept numbers, only percentages.
- **FIX**: Adjustments to match Sublime 4069 which now handles HSL color blending correctly.
- **FIX**: Handle HSL/HWB if a user uses the 'deg' unit type.
- **FIX**: Sublime doesn't support them, but support 'rad', 'grad', and 'turn' unit types in case Sublime ever supports
them in HSL an HWB.

## 3.6.0

Expand Down
20 changes: 13 additions & 7 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ docs_dir: docs/src/markdown
theme:
name: material
palette:
primary: blue
accent: blue
logo:
icon: description
primary: drac-purple
accent: drac-purple
icon:
logo: material/book-open-page-variant
font:
text: Roboto
code: Roboto Mono
Expand Down Expand Up @@ -58,7 +58,6 @@ markdown_extensions:
class: arithmatex
format: !!python/name:pymdownx.arithmatex.fence_mathjax_format
- pymdownx.highlight:
css_class: codehilite
extend_pygments_lang:
- name: php-inline
lang: php
Expand Down Expand Up @@ -101,7 +100,14 @@ markdown_extensions:

extra:
social:
- icon: brands/github
- icon: fontawesome/brands/github
link: https://github.com/facelessuser
- icon: brands/discord
- icon: fontawesome/brands/discord
link: https://discord.gg/rZ2n3Dy

plugins:
- search
- git-revision-date-localized
- mkdocs_pymdownx_material_extras
- minify:
minify_html: true
11 changes: 8 additions & 3 deletions st3/mdpopups/rgba.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
RGBA.
Licensed under MIT
Copyright (c) 2012 - 2020 Isaac Muse <isaacmuse@gmail.com>
Copyright (c) 2012 - 2016 Isaac Muse <isaacmuse@gmail.com>
"""
import re
from colorsys import rgb_to_hls, hls_to_rgb, rgb_to_hsv, hsv_to_rgb
import decimal
import sublime

HSL_WORKAROUND = int(sublime.version()) < 4069

RGB_CHANNEL_SCALE = 1.0 / 255.0
HUE_SCALE = 1.0 / 360.0
Expand Down Expand Up @@ -54,8 +57,10 @@ def hue_blend_channel(c1, c2, f):
c1 += 360.0
else:
c2 += 360.0
# This shouldn't be necessary and is probably a bug in Sublime.
f = 1.0 - f

if HSL_WORKAROUND:
# This shouldn't be necessary and is probably a bug in Sublime.
f = 1.0 - f

value = abs(c1 * f + c2 * (1 - f))
while value > 360.0:
Expand Down
31 changes: 26 additions & 5 deletions st3/mdpopups/st_color_scheme_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
from collections import namedtuple
from plistlib import readPlistFromBytes
import decimal
import math

NEW_SCHEMES = int(sublime.version()) >= 3150
FONT_STYLE = "font_style" if int(sublime.version()) >= 3151 else "fontStyle"
GLOBAL_OPTIONS = "globals" if int(sublime.version()) >= 3152 else "defaults"

CONVERT_TURN = 360
CONVERT_GRAD = 90 / 100

# XML
XML_COMMENT_RE = re.compile(br"^[\r\n\s]*<!--[\s\S]*?-->[\s\r\n]*|<!--[\s\S]*?-->")

Expand All @@ -47,7 +51,8 @@

COLOR_PARTS = {
"percent": r"[+\-]?(?:(?:\d*\.\d+)|\d+)%",
"float": r"[+\-]?(?:(?:\d*\.\d+)|\d+)"
"float": r"[+\-]?(?:(?:\d*\.\d+)|\d+)",
"angle": r"[+\-]?(?:(?:\d*\.\d+)|\d+)(deg|rad|turn|grad)?"
}

RGB_COLORS = r"""(?x)
Expand All @@ -63,11 +68,11 @@

HSL_COLORS = r"""(?x)
\b(?P<hsl>hsl\(\s*(?P<hsl_content>%(float)s\s*,\s*%(percent)s\s*,\s*%(percent)s)\s*\)) |
\b(?P<hsla>hsla\(\s*(?P<hsla_content>%(float)s\s*,\s*(?:%(percent)s\s*,\s*){2}(?:%(percent)s|%(float)s))\s*\))
\b(?P<hsla>hsla\(\s*(?P<hsla_content>%(angle)s\s*,\s*(?:%(percent)s\s*,\s*){2}(?:%(percent)s|%(float)s))\s*\))
""" % COLOR_PARTS

HWB_COLORS = r"""(?x)
\b(?P<hwb>hwb\(\s*(?P<hwb_content>%(float)s\s*,\s*%(percent)s\s*,\s*%(percent)s
\b(?P<hwb>hwb\(\s*(?P<hwb_content>%(angle)s\s*,\s*%(percent)s\s*,\s*%(percent)s
(?:\s*,\s*(?:%(percent)s|%(float)s))?)\s*\))
""" % COLOR_PARTS

Expand Down Expand Up @@ -137,6 +142,22 @@
}


def norm_angle(angle):
"""Normalize angle units."""

if angle.endswith('turn'):
value = float(angle[:-4]) * CONVERT_TURN
elif angle.endswith('rad'):
value = math.degrees(float(angle[:-3]))
elif angle.endswith('grad'):
value = float(angle[:-3]) * CONVERT_GRAD
elif angle.endswith('deg'):
value = float(angle[:-3])
else:
value = float(angle)
return value


def packages_path(pth):
"""Get packages path."""

Expand Down Expand Up @@ -317,9 +338,9 @@ def translate_color(m, var, var_src):
else:
alpha = alpha_dec_normalize(content[3])
elif groups.get('hsl'):
content = [x.strip() for x in m.group('hsl_content').split(',')]
content = [x.strip().lower() for x in m.group('hsl_content').split(',')]
rgba = RGBA()
hue = float(content[0])
hue = norm_angle(content[0])
if hue < 0.0 or hue > 360.0:
hue = hue % 360.0
h = hue / 360.0
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ commands=

[testenv:documents]
deps=
mkdocs_pymdownx_material_extras==1.0b4
mkdocs_pymdownx_material_extras==1.0b11
mkdocs-git-revision-date-localized-plugin
mkdocs-minify-plugin
pyspelling
commands=
mkdocs build --clean --verbose --strict
Expand Down

0 comments on commit 3d6c3cf

Please sign in to comment.