commit 4e154c92a7b98d7a461c4871ae6dab9f47a96cac Author: Doki21yy Date: Fri Apr 24 19:06:29 2026 +0800 Add ComfyUI ColorCode bridge node diff --git a/README.md b/README.md new file mode 100644 index 0000000..a2c7c28 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# ComfyUI ColorCode Bridge + +Tiny type adapter for workflows where one node outputs a hex color string and another node requires a `COLORCODE` input. + +## Node + +- `utils/color > String To ColorCode` + +## Usage + +Connect an automatic hex-color string output such as `#00FF00` into `color_hex`, then connect `color_code` into the downstream `COLORCODE` input. + +This node validates and normalizes: + +- `00FF00` -> `#00FF00` +- `#0F0` -> `#00FF00` +- `#00ff00` -> `#00FF00` diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..5ce85d8 --- /dev/null +++ b/__init__.py @@ -0,0 +1,48 @@ +import re + + +HEX_RE = re.compile(r"^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$") + + +class StringToColorCode: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "color_hex": ("STRING", {"forceInput": True}), + } + } + + RETURN_TYPES = ("COLORCODE",) + RETURN_NAMES = ("color_code",) + FUNCTION = "convert" + CATEGORY = "utils/color" + + def convert(self, color_hex): + value = self._first_value(color_hex).strip() + match = HEX_RE.match(value) + if not match: + raise ValueError( + f"String To ColorCode expected a hex color like #00FF00, got: {value!r}" + ) + + hex_value = match.group(1) + if len(hex_value) == 3: + hex_value = "".join(ch * 2 for ch in hex_value) + + return (f"#{hex_value.upper()}",) + + @staticmethod + def _first_value(value): + if isinstance(value, (list, tuple)): + value = value[0] if value else "" + return str(value) + + +NODE_CLASS_MAPPINGS = { + "StringToColorCode": StringToColorCode, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "StringToColorCode": "String To ColorCode", +}