Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix(pptx-to-svg): validate HSL color channels

hugohe3d6dade5bd0e2
diff --git a/skills/ppt-master/references/shared-standards.md b/skills/ppt-master/references/shared-standards.md
index be34ace5..0c801bba 100644
--- a/skills/ppt-master/references/shared-standards.md
+++ b/skills/ppt-master/references/shared-standards.md
@@ -756,6 +756,13 @@ Imported `a:prstClr` has exactly one `val` attribute selected from the complete
RGB mapping. Missing/unknown/case-altered values, extra attributes, namespace
aliases, and interleaved text stop import instead of falling back to black or
an adjacent CSS color name.
+Imported `a:hslClr` has exactly the required `hue`, `sat`, and `lum`
+attributes. The registered mapping accepts integer DrawingML units only:
+`hue` is `0..21599999` in 1/60000 degree, while `sat` and `lum` are
+`0..100000` in thousandths of a percent. Missing, malformed, percentage-literal,
+or out-of-range channels, extra attributes, namespace aliases, and interleaved
+text stop import instead of receiving zero defaults or being clamped into a
+different color.
---
diff --git a/skills/ppt-master/scripts/pptx_to_svg/color_resolver.py b/skills/ppt-master/scripts/pptx_to_svg/color_resolver.py
index 4dbaf0d9..119cb4b9 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/color_resolver.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/color_resolver.py
@@ -102,6 +102,7 @@ SCHEME_ALIASES = {
"tx1": "dk1", "tx2": "dk2",
}
_SRGB_HEX_RE = re.compile(r"[0-9A-Fa-f]{6}")
+_OOXML_INTEGER_RE = re.compile(r"[+-]?[0-9]+")
_SCHEME_COLOR_VALUES = {
"bg1",
"tx1",
@@ -302,12 +303,7 @@ def resolve_color(
elif tag == "prstClr":
base_hex = _preset_color_hex(color_elem)
elif tag == "hslClr":
- # DrawingML hue is in 1/60000 deg ([0, 21_600_000) maps to [0°, 360°));
- # _hsl_to_hex expects a fraction in [0, 1), so divide by 60000 * 360.
- h = float(color_elem.attrib.get("hue", "0")) / 21_600_000.0
- s = float(color_elem.attrib.get("sat", "0")) / 100000.0
- lum = float(color_elem.attrib.get("lum", "0")) / 100000.0
- base_hex = _hsl_to_hex(h, s, lum)
+ base_hex = _hsl_color_hex(color_elem)
elif tag == "scrgbClr":
# 0..100000 per channel
r = float(color_elem.attrib.get("r", "0")) / 100000.0
@@ -398,6 +394,63 @@ def _preset_color_hex(color_elem: ET.Element) -> str:
return hex_
+def _hsl_color_hex(color_elem: ET.Element) -> str:
+ """Resolve one closed DrawingML HSL base color."""
+ children = list(color_elem)
+ if (
+ color_elem.tag != f"{{{NS['a']}}}hslClr"
+ or set(color_elem.attrib) != {"hue", "sat", "lum"}
+ or (color_elem.text or "").strip()
+ or any((child.tail or "").strip() for child in children)
+ ):
+ raise ValueError("Invalid DrawingML HSL color structure")
+ hue = _bounded_integer_attribute(
+ color_elem,
+ "hue",
+ minimum=0,
+ maximum=21_599_999,
+ label="HSL color",
+ )
+ saturation = _bounded_integer_attribute(
+ color_elem,
+ "sat",
+ minimum=0,
+ maximum=100000,
+ label="HSL color",
+ )
+ luminance = _bounded_integer_attribute(
+ color_elem,
+ "lum",
+ minimum=0,
+ maximum=100000,
+ label="HSL color",
+ )
+ return _hsl_to_hex(
+ hue / 21_600_000.0,
+ saturation / 100000.0,
+ luminance / 100000.0,
+ )
+
+
+def _bounded_integer_attribute(
+ elem: ET.Element,
+ attr: str,
+ *,
+ minimum: int,
+ maximum: int,
+ label: str,
+) -> int:
+ """Parse one required bounded DrawingML integer attribute."""
+ raw = elem.attrib[attr]
+ token = raw.strip()
+ if _OOXML_INTEGER_RE.fullmatch(token) is None:
+ raise ValueError(f"Invalid DrawingML {label} {attr}: {raw!r}")
+ value = int(token)
+ if not minimum <= value <= maximum:
+ raise ValueError(f"Invalid DrawingML {label} {attr}: {raw!r}")
+ return value
+
+
def _apply_modifiers(hex_color: str, color_elem: ET.Element) -> tuple[str, float]:
"""Apply tint / shade / lumMod / lumOff / satMod / hueMod / alpha modifiers
in document order. DrawingML stacks these on the color in order.
diff --git a/skills/ppt-master/scripts/test_svg_quality_checker.py b/skills/ppt-master/scripts/test_svg_quality_checker.py
index 4be1beda..2427708e 100644
--- a/skills/ppt-master/scripts/test_svg_quality_checker.py
+++ b/skills/ppt-master/scripts/test_svg_quality_checker.py
@@ -3111,6 +3111,80 @@ class SVGQualityCheckerCompatibilityTests(unittest.TestCase):
</a:prstClr>''')
self.assertEqual(resolve_color(valid, None), ('#00008B', 0.5))
+ def test_native_hsl_color_requires_bounded_integer_channels(self):
+ invalid_colors = (
+ (
+ '<a:hslClr sat="100000" lum="50000"/>',
+ 'HSL color structure',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100000" lum="50000" '
+ 'future="x"/>',
+ 'HSL color structure',
+ ),
+ (
+ '<a:hslClr hue="-1" sat="100000" lum="50000"/>',
+ 'HSL color hue',
+ ),
+ (
+ '<a:hslClr hue="21600000" sat="100000" lum="50000"/>',
+ 'HSL color hue',
+ ),
+ (
+ '<a:hslClr hue="0.0" sat="100000" lum="50000"/>',
+ 'HSL color hue',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100%" lum="50000"/>',
+ 'HSL color sat',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100001" lum="50000"/>',
+ 'HSL color sat',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100000" lum="-1"/>',
+ 'HSL color lum',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100000" lum="1e2"/>',
+ 'HSL color lum',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100000" lum="50000">payload'
+ '</a:hslClr>',
+ 'HSL color structure',
+ ),
+ (
+ '<a:hslClr hue="0" sat="100000" lum="50000">'
+ '<a:alpha val="50000"/>payload</a:hslClr>',
+ 'HSL color structure',
+ ),
+ )
+ namespace = (
+ 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"'
+ )
+ for color, expected in invalid_colors:
+ with self.subTest(color=color):
+ fill = ET.fromstring(
+ f'<a:solidFill {namespace}>{color}</a:solidFill>'
+ )
+ with self.assertRaisesRegex(ValueError, expected):
+ resolve_fill(fill, None)
+
+ foreign_color = ET.fromstring(
+ '<future:hslClr xmlns:future="urn:future" '
+ 'hue="0" sat="100000" lum="50000"/>'
+ )
+ with self.assertRaisesRegex(ValueError, 'HSL color structure'):
+ resolve_color(foreign_color, None)
+
+ valid = ET.fromstring(f'''
+<a:hslClr {namespace} hue="0" sat="100000" lum="50000">
+ <a:alpha val="50000"/>
+</a:hslClr>''')
+ self.assertEqual(resolve_color(valid, None), ('#FF0000', 0.5))
+
def test_native_gradient_stop_position_round_trips(self):
for position in (0, 1, 7, 13, 33333, 99999, 100000):
with self.subTest(position=position):