Skip to content

Commit

Permalink
ADD: BK_ColorLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
JayLyu committed Jul 3, 2024
1 parent f51c637 commit dd13279
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
133 changes: 133 additions & 0 deletions BK_ColorLimit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import colorsys


def rgb_to_hsl(rgb):
# 将 rgb 元组转换成 hsl
r, g, b = rgb
h, l, s = colorsys.rgb_to_hls(r / 255.0, g / 255.0, b / 255.0)
return h, s, l


def hsl_to_rgb(hsl):
# 将 hsl 元组转换成 rgb
h, s, l = hsl
r, g, b = colorsys.hls_to_rgb(h, l, s)
r = int(r * 255.0)
g = int(g * 255.0)
b = int(b * 255.0)
return r, g, b


class BK_ColorLimit:

@classmethod
def INPUT_TYPES(s):

return {
"required": {
"hex_color": ("STRING", {
"default": "#FF0036"
}),
},
"optional": {
"saturation_start": ("FLOAT", {
"default": 0,
"min": 0,
"max": 1,
"step": 0.01,
"display": "slider"
}),
"saturation_end": ("FLOAT", {
"default": 1,
"min": 0,
"max": 1,
"step": 0.01,
"display": "slider"
}),
"brightness_start": ("FLOAT", {
"default": 0,
"min": 0,
"max": 1,
"step": 0.01,
"display": "slider"
}),
"brightness_end": ("FLOAT", {
"default": 0.5,
"min": 0,
"max": 1,
"step": 0.01,
"display": "slider"
}),
}
}

CATEGORY = "⭐️Baikong"
RETURN_TYPES = (
"STRING",
# "STRING","STRING"
)
# RETURN_NAMES = ("hex", "rgb", "hsl", )
FUNCTION = "color_limit"
# OUTPUT_NODE = True

def color_limit(
self,
hex_color,
saturation_start: float = 0,
saturation_end: float = 1,
brightness_start: float = 0,
brightness_end: float = 0.5,
):
# 将 hex_color 转换成 rgb 元组
hex_color = hex_color.lstrip('#')
rgb_color = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))

# 将 rgb 转换成 hsl
h, s, l = rgb_to_hsl(rgb_color)

# 四舍五入到小数点后两位
h = round(h, 6)
s = round(s, 6)
l = round(l, 6)

# s 的值必须在 saturation_start 和 saturation_end 之间
s_new = max(saturation_start, min(s, saturation_end))

# l 的值必须在 brightness_start 和 brightness_end 之间
l_new = max(brightness_start, min(l, brightness_end))

# print(f"hue:{h}")
# print(
# f"saturation:{saturation_start} ~ {saturation_end}, value:{s}→{s_new}")
# print(
# f"brightness:{brightness_start} ~ {brightness_end}, value:{l}→{l_new}")

# 将 hsl 的值转换为 rgb
rgb_new = hsl_to_rgb((h, s_new, l_new))

# 构造新的 hex 表示
hex_new = '#{:02x}{:02x}{:02x}'.format(*rgb_new)

# 构造 RGB 的字符串表示
rgb_string = f"({rgb_new[0]}, {rgb_new[1]}, {rgb_new[2]})"

# 构造 HSL 的字符串表示
hsl_string = f"({h}, {s_new}, {l_new})"

return (
hex_new,
# rgb_string, hsl_string,
)


if __name__ == "__main__":
selector_node = BK_ColorLimit()
print(
selector_node.color_limit(
hex_color="#FF0036",
saturation_start=0,
saturation_end=1,
brightness_start=0,
brightness_end=1,
)
)
4 changes: 2 additions & 2 deletions BK_Img2Color.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def INPUT_TYPES(s):
"input_image": ("IMAGE",),
},
"optional": {
"num_colors": ("INT", {"default": 3, "min": 1}),
"num_colors": ("INT", {"default": 1, "min": 1}),
"get_complementary": (
"BOOLEAN",
{
Expand All @@ -43,7 +43,7 @@ def INPUT_TYPES(s):
"accuracy": (
"INT",
{
"default": 60,
"default": 80,
"display": "slider",
"min": 1,
"max": 100,
Expand Down
7 changes: 5 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from .BK_Img2Color import BK_Img2Color
from .BK_ColorSelector import BK_ColorSelector
from .BK_GradientImage import BK_GradientImage
from .BK_ColorLimit import BK_ColorLimit

NODE_CLASS_MAPPINGS = {
"BK_Img2Color": BK_Img2Color,
"BK_ColorSelector": BK_ColorSelector,
"BK_GradientImage": BK_GradientImage
"BK_GradientImage": BK_GradientImage,
"BK_ColorLimit": BK_ColorLimit
}

NODE_DISPLAY_NAME_MAPPINGS = {
"BK_Img2Color": "BK Img To Color",
"BK_ColorSelector": "BK Color Selector",
"BK_GradientImage": "BK Gradient Image"
"BK_GradientImage": "BK Gradient Image",
"BK_ColorLimit": "BK Color Limit"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui_baikong_node"
description = "Nodes:BK Img To Color, BK Color Selector"
version = "1.0.1"
version = "1.0.2"
license = "LICENSE"
dependencies = ["colornamer>=0.2.4", "scikit_learn>=1.4.0", "webcolors==1.11"]

Expand Down

0 comments on commit dd13279

Please sign in to comment.