From e37dd1ad202d3f7ac3f5262c0360de4e4aa601d8 Mon Sep 17 00:00:00 2001 From: Level Pixel Dev Date: Mon, 9 Jun 2025 19:45:10 +0600 Subject: [PATCH] Add color input --- node_list.json | 1 + nodes/image/rmbg_LP.py | 60 +++++++++++++++++++++++++++++++++++++++++- pyproject.toml | 2 +- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/node_list.json b/node_list.json index 7308d85..a22167d 100644 --- a/node_list.json +++ b/node_list.json @@ -13,5 +13,6 @@ "Image Remove Background (rembg) [LP]":"Removes background for images with user-defined model. This node uses the rembg library. It is NOT recommended for use in normal scenarios.", "Image Remove Background (BiRefNet) [LP]":"Removes background for images with user-defined model type of BiRefNet. Recommended for fast and high-quality background removal", "Image Remove Background (RMBG) [LP]":"Removes background for images with user-defined model. This node uses the RMBG models. Recommended for ultra-high precision", + "Color Input [LP]":"Color Input for remove background nodes.", "Multimodal Generator Advanced [LP]":"Node for Multimodal neural models based on GGUF. Supports Qwen2.5-VL of GGUF format. Based on llama-mtmd." } \ No newline at end of file diff --git a/nodes/image/rmbg_LP.py b/nodes/image/rmbg_LP.py index 99267db..d3d310a 100644 --- a/nodes/image/rmbg_LP.py +++ b/nodes/image/rmbg_LP.py @@ -35,6 +35,15 @@ device = "cuda" if torch.cuda.is_available() else "cpu" folder_paths.add_model_folder_path("rembg", os.path.join(folder_paths.models_dir, "rembg")) +COLOR_PRESETS = { + "black": "#000000", "white": "#FFFFFF", "red": "#FF0000", "green": "#00FF00", "blue": "#0000FF", + "yellow": "#FFFF00", "cyan": "#00FFFF", "magenta": "#FF00FF", "gray": "#808080", "silver": "#C0C0C0", + "maroon": "#800000", "olive": "#808000", "purple": "#800080", "teal": "#008080", "navy": "#000080", + "orange": "#FFA500", "pink": "#FFC0CB", "brown": "#A52A2A", "violet": "#EE82EE", "indigo": "#4B0082", + "light_gray": "#D3D3D3", "dark_gray": "#A9A9A9", "light_blue": "#ADD8E6", "dark_blue": "#00008B", + "light_blue": "#ADD8E6", "dark_blue": "#00008B", "light_green": "#90EE90", "dark_green": "#006400" +} + AVAILABLE_MODELS = { "RMBG-2.0": { "type": "rmbg", @@ -655,10 +664,59 @@ class ImageRemoveBackgroundRMBG: empty_mask_image = empty_mask.reshape((-1, 1, empty_mask.shape[-2], empty_mask.shape[-1])).movedim(1, -1).expand(-1, -1, -1, 3) return (image, empty_mask, empty_mask_image) +def fix_color_format(color: str) -> str: + """Fix color format to valid hex code""" + if not color: + return "" + + color = color.strip().upper() + if not color.startswith('#'): + color = f"#{color}" + + color = color[1:] + if len(color) == 3: + r, g, b = color[0], color[1], color[2] + return f"#{r}{r}{g}{g}{b}{b}" + elif len(color) < 6: + raise ValueError(f"Invalid color format: {color}") + elif len(color) > 6: + color = color[:6] + + return f"#{color}" + +class ColorInput: + @classmethod + def INPUT_TYPES(self): + return { + "required": { + "preset": (list(COLOR_PRESETS.keys()),), + "color": ("STRING", {"default": "", "placeholder": "Enter color code (e.g. #FF0000 or #F00)"}), + }, + } + + RETURN_TYPES = ("COLOR",) + RETURN_NAMES = ("COLOR",) + FUNCTION = 'get_color' + CATEGORY = 'LevelPixel/Image' + + def get_color(self, preset, color): + if not color: + return (COLOR_PRESETS[preset],) + + try: + fixed_color = fix_color_format(color) + if not all(c in '0123456789ABCDEFabcdef' for c in fixed_color[1:]): + raise ValueError(f"Invalid hex characters in {color}") + return (fixed_color,) + except Exception as e: + raise RuntimeError(f"Invalid color format: {color}. Please use format like #FF0000 or #F00") + NODE_CLASS_MAPPINGS = { - "ImageRemoveBackgroundRMBG|LP": ImageRemoveBackgroundRMBG + "ImageRemoveBackgroundRMBG|LP": ImageRemoveBackgroundRMBG, + "ColorInput|LP": ColorInput, } NODE_DISPLAY_NAME_MAPPINGS = { "ImageRemoveBackgroundRMBG|LP": "Image Remove Background (RMBG) [LP]", + "ColorInput|LP": "Color Input [LP]", } \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 2a9109d..64a958f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-levelpixel-advanced" description = "Advanced nodes of the Level Pixel company (aka levelpixel, LP). Includes convenient advanced nodes for working with LLM и VLM models (LLaVa, Multimodal Generator) with GGUF format. Qwen2.5-VL and Qwen2.5 supported, based on llama-mtmd. Also included is a node for the RAM model. Nodes have the ability to automatically unload models from VRAM. This package also includes Autotagger (WD), Image Remove Background (based on RMBG-2.0, on BiRefNet, on BiRefNet-HR-Matter, on RemBG). Licensed under GNU GPLv3. Repository: https://github.com/LevelPixel/ComfyUI-LevelPixel-Advanced" -version = "1.2.9" +version = "1.3.0" license = { file = "LICENSE.txt" } dependencies = [ "torch>=2.0.1",