Add ComfyUI ColorCode bridge node
This commit is contained in:
@@ -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`
|
||||
+48
@@ -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",
|
||||
}
|
||||
Reference in New Issue
Block a user