Skip to content

Commit

Permalink
Add WAS_Hex_to_HSL
Browse files Browse the repository at this point in the history
  • Loading branch information
WAS-PlaiLabs committed Jun 20, 2024
1 parent d09b5e6 commit 51bb836
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions WAS_Node_Suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4878,6 +4878,64 @@ def image_generate_palette(self, image, colors=16, mode="chart"):
return (pil2tensor(palette_image), [palette,])


# HEX TO HSL

class WAS_Hex_to_HSL:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"hex_color": ("STRING", {"default": "#FF0000"}),
},
"optional": {
"include_alpha": ("BOOLEAN", {"default": False})
}
}

RETURN_TYPES = ("INT", "INT", "INT", "FLOAT", "STRING")
RETURN_NAMES = ("hue", "saturation", "lightness", "alpha", "hsl")

FUNCTION = "hex_to_hsl"
CATEGORY = "WAS Suite/Utilities"

@staticmethod
def hex_to_hsl(hex_color, include_alpha=False):
if hex_color.startswith("#"):
hex_color = hex_color[1:]

red = int(hex_color[0:2], 16) / 255.0
green = int(hex_color[2:4], 16) / 255.0
blue = int(hex_color[4:6], 16) / 255.0
alpha = int(hex_color[6:8], 16) / 255.0 if include_alpha and len(hex_color) == 8 else 1.0
max_val = max(red, green, blue)
min_val = min(red, green, blue)
delta = max_val - min_val
luminance = (max_val + min_val) / 2.0

if delta == 0:
hue = 0
saturation = 0
else:
saturation = delta / (1 - abs(2 * luminance - 1))
if max_val == red:
hue = ((green - blue) / delta) % 6
elif max_val == green:
hue = (blue - red) / delta + 2
elif max_val == blue:
hue = (red - green) / delta + 4
hue *= 60
if hue < 0:
hue += 360

luminance = luminance * 100
saturation = saturation * 100

hsl_string = f'hsl({round(hue)}, {round(saturation)}%, {round(luminance)}%)' if not include_alpha else f'hsla({round(hue)}, {round(saturation)}%, {round(luminance)}%, {round(alpha, 2)})'
output = (round(hue), round(saturation), round(luminance), round(alpha, 2), hsl_string)

return output


# IMAGE ANALYZE

class WAS_Image_Analyze:
Expand Down Expand Up @@ -13875,6 +13933,7 @@ def count_places(self, int_input):
"Logic Comparison XOR": WAS_Logical_XOR,
"Logic NOT": WAS_Logical_NOT,
"Lora Loader": WAS_Lora_Loader,
"Hex to HSL": WAS_Hex_to_HSL,
"Image SSAO (Ambient Occlusion)": WAS_Image_Ambient_Occlusion,
"Image SSDO (Direct Occlusion)": WAS_Image_Direct_Occlusion,
"Image Analyze": WAS_Image_Analyze,
Expand Down

0 comments on commit 51bb836

Please sign in to comment.