From 8cb8ffb8679b3e7fcce441ece434c53069061abf Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Wed, 8 Sep 2021 10:10:07 +0800 Subject: [PATCH] Allow loosy ASS/SSA color codes (#192) * Allow loosy ASS/SSA color codes Signed-off-by: Jack Cherng * Use named regex capture group in AssABGR.MATCH Signed-off-by: Jack Cherng * fixup: lossy color codes Signed-off-by: Jack Cherng * fixup: normalize the trailing "&" in color codes Signed-off-by: Jack Cherng --- color_helper.sublime-settings | 2 +- custom/ass_abgr.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/color_helper.sublime-settings b/color_helper.sublime-settings index 43cb521d..c237a505 100755 --- a/color_helper.sublime-settings +++ b/color_helper.sublime-settings @@ -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]" } ], diff --git a/custom/ass_abgr.py b/custom/ass_abgr.py index 1d620e77..e9079128 100644 --- a/custom/ass_abgr.py +++ b/custom/ass_abgr.py @@ -10,7 +10,7 @@ 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&H)?(?P[0-9a-fA-F]{1,8})(?P&|\b)") @classmethod def match(cls, string: str, start: int = 0, fullmatch: bool = True): @@ -18,7 +18,7 @@ def match(cls, string: str, start: int = 0, fullmatch: bool = True): 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 @@ -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(