Skip to content

Commit

Permalink
Allow loosy ASS/SSA color codes (facelessuser#192)
Browse files Browse the repository at this point in the history
* Allow loosy ASS/SSA color codes

Signed-off-by: Jack Cherng <[email protected]>

* Use named regex capture group in AssABGR.MATCH

Signed-off-by: Jack Cherng <[email protected]>

* fixup: lossy color codes

Signed-off-by: Jack Cherng <[email protected]>

* fixup: normalize the trailing "&" in color codes

Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng authored Sep 8, 2021
1 parent e189456 commit 8cb8ffb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion color_helper.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@
"base_scopes": ["text.ass"],
"scanning": ["constant.other.color"],
"color_class": "ass_abgr",
"color_trigger": "(?i)&h"
"color_trigger": "(?:&H|(?<=\\\\[1-4]c)|(?<=\\\\c))[0-9a-fA-F]"
}
],

Expand Down
11 changes: 6 additions & 5 deletions custom/ass_abgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class AssABGR(SRGB):
"""ASS `ABGR` color space."""

MATCH = re.compile(r"&H([0-9a-fA-f]{8}|[0-9a-fA-f]{6})\b")
MATCH = re.compile(r"(?P<prefix>&H)?(?P<color>[0-9a-fA-F]{1,8})(?P<suffix>&|\b)")

@classmethod
def match(cls, string: str, start: int = 0, fullmatch: bool = True):
"""Match a color string."""

m = cls.MATCH.match(string, start)
if m is not None and (not fullmatch or m.end(0) == len(string)):
return cls.split_channels(m.group(1)), m.end(0)
return cls.split_channels(m.group("color")), m.end(0)
return None, None

@classmethod
Expand All @@ -32,9 +32,10 @@ def translate_channel(cls, channel: int, value: str):
def split_channels(cls, color: str):
"""Split string into the appropriate channels."""

# convert `BBGGRR` to `AABBGGRR`
if len(color) == 6:
color = "00" + color
# convert `RR` / `GGRR` / `BBGGRR` to `AABBGGRR`
# consecutive leading 0s can be omitted and the alpha is 00 (opaque) by default
color = color.zfill(8)

# deal with `AABBGGRR`
if len(color) == 8:
return cls.null_adjust(
Expand Down

0 comments on commit 8cb8ffb

Please sign in to comment.