diff --git a/CHANGELOG.md b/CHANGELOG.md index c68fe5a..d37f091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### v2.2.0 - 2024 Jul.03 +- Add `Adv.` in **Styles Presets** +- Improve **Consts** Logics *(`internal`)* + ### v2.1.0 - 2024 Jul.03 - Support **Randomize** Forever diff --git a/scripts/cc.py b/scripts/cc.py index addcaf0..9d27f1d 100644 --- a/scripts/cc.py +++ b/scripts/cc.py @@ -6,11 +6,11 @@ from scripts.cc_style import StyleManager from scripts.cc_xyz import xyz_support import scripts.cc_const as const +from random import seed import gradio as gr -import random -VERSION = "v2.1.0" +VERSION = "v2.2.0" style_manager = StyleManager() @@ -68,47 +68,44 @@ class VectorscopeCC(scripts.Script): r = gr.Slider( label="R", info="Cyan | Red", - minimum=const.COLOR.minimum, - maximum=const.COLOR.maximum, + minimum=const.Color.minimum, + maximum=const.Color.maximum, step=0.05, - value=const.COLOR.default, + value=const.Color.default, elem_id=f"cc-r-{mode}", ) g = gr.Slider( label="G", info="Magenta | Green", - minimum=const.COLOR.minimum, - maximum=const.COLOR.maximum, + minimum=const.Color.minimum, + maximum=const.Color.maximum, step=0.05, - value=const.COLOR.default, + value=const.Color.default, elem_id=f"cc-g-{mode}", ) b = gr.Slider( label="B", info="Yellow | Blue", - minimum=const.COLOR.minimum, - maximum=const.COLOR.maximum, + minimum=const.Color.minimum, + maximum=const.Color.maximum, step=0.05, - value=const.COLOR.default, + value=const.Color.default, elem_id=f"cc-b-{mode}", ) r.input( None, - [r, g, b], - None, + inputs=[r, g, b], _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", ) g.input( None, - [r, g, b], - None, + inputs=[r, g, b], _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", ) b.input( None, - [r, g, b], - None, + inputs=[r, g, b], _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", ) @@ -132,31 +129,6 @@ class VectorscopeCC(scripts.Script): ) delete_btn = gr.Button(value="Delete Style", scale=2) - apply_btn.click( - fn=style_manager.get_style, - inputs=style_choice, - outputs=[latent, bri, con, sat, r, g, b], - ).then( - None, - [r, g, b], - None, - _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", - ) - save_btn.click( - fn=lambda *args: gr.update(choices=style_manager.save_style(*args)), - inputs=[style_name, latent, bri, con, sat, r, g, b], - outputs=style_choice, - ) - delete_btn.click( - fn=lambda name: gr.update(choices=style_manager.delete_style(name)), - inputs=style_name, - outputs=style_choice, - ) - refresh_btn.click( - fn=lambda _: gr.update(choices=style_manager.list_style()), - outputs=style_choice, - ) - with gr.Accordion("Advanced Settings", open=False): with gr.Row(): doHR = gr.Checkbox(label="Process Hires. fix") @@ -186,6 +158,48 @@ class VectorscopeCC(scripts.Script): value="Flat", ) + comps = ( + latent, + bri, + con, + sat, + r, + g, + b, + doHR, + doAD, + doRN, + method, + scaling, + ) + + apply_btn.click( + fn=style_manager.get_style, + inputs=style_choice, + outputs=[*comps], + ).then( + None, + inputs=[r, g, b], + _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", + ) + + save_btn.click( + fn=lambda *args: gr.update(choices=style_manager.save_style(*args)), + inputs=[style_name, *comps], + outputs=style_choice, + ) + + delete_btn.click( + fn=lambda name: gr.update(choices=style_manager.delete_style(name)), + inputs=style_name, + outputs=style_choice, + ) + + refresh_btn.click( + fn=lambda _: gr.update(choices=style_manager.load_styles()), + outputs=style_choice, + ) + with gr.Row(): reset_btn = gr.Button(value="Reset") random_btn = gr.Button(value="Randomize") @@ -196,9 +210,9 @@ class VectorscopeCC(scripts.Script): gr.update(value=const.Brightness.default), gr.update(value=const.Contrast.default), gr.update(value=const.Saturation.default), - gr.update(value=const.COLOR.default), - gr.update(value=const.COLOR.default), - gr.update(value=const.COLOR.default), + gr.update(value=const.Color.default), + gr.update(value=const.Color.default), + gr.update(value=const.Color.default), gr.update(value=False), gr.update(value=False), gr.update(value=False), @@ -208,85 +222,25 @@ class VectorscopeCC(scripts.Script): def on_random(): return [ - gr.update( - value=round( - random.uniform( - const.Brightness.minimum, const.Brightness.maximum - ), - 2, - ) - ), - gr.update( - value=round( - random.uniform( - const.Contrast.minimum, const.Contrast.maximum - ), - 2, - ) - ), - gr.update( - value=round( - random.uniform( - const.Saturation.minimum, const.Saturation.maximum - ), - 2, - ) - ), - gr.update( - value=round( - random.uniform( - const.COLOR.minimum, const.COLOR.maximum - ), - 2, - ) - ), - gr.update( - value=round( - random.uniform( - const.COLOR.minimum, const.COLOR.maximum - ), - 2, - ) - ), - gr.update( - value=round( - random.uniform( - const.COLOR.minimum, const.COLOR.maximum - ), - 2, - ) - ), + gr.update(value=const.Brightness.rand()), + gr.update(value=const.Contrast.rand()), + gr.update(value=const.Saturation.rand()), + gr.update(value=const.Color.rand()), + gr.update(value=const.Color.rand()), + gr.update(value=const.Color.rand()), ] reset_btn.click( fn=on_reset, - inputs=[], - outputs=[ - latent, - bri, - con, - sat, - r, - g, - b, - doHR, - doAD, - doRN, - method, - scaling, - ], + outputs=[*comps], ).then( None, - [r, g, b], - None, + inputs=[r, g, b], _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", ) - random_btn.click( - fn=on_random, inputs=[], outputs=[bri, con, sat, r, g, b] - ).then( - None, - [r, g, b], + random_btn.click(fn=on_random, outputs=[bri, con, sat, r, g, b]).then( None, + inputs=[r, g, b], _js=f"(r, g, b) => {{ VectorscopeCC.updateCursor(r, g, b, {m}); }}", ) @@ -310,21 +264,7 @@ class VectorscopeCC(scripts.Script): comp.do_not_save_to_config = True self.paste_field_names.append(name) - return [ - enable, - latent, - bri, - con, - sat, - r, - g, - b, - doHR, - doAD, - doRN, - method, - scaling, - ] + return [enable, *comps] def process_batch( self, @@ -408,21 +348,15 @@ class VectorscopeCC(scripts.Script): steps = int(steps * getattr(p, "denoising_strength", 1.0) + 1.0) if cc_seed: - random.seed(cc_seed) + seed(cc_seed) - bri = round( - random.uniform(const.Brightness.minimum, const.Brightness.maximum), 2 - ) - con = round( - random.uniform(const.Contrast.minimum, const.Contrast.maximum), 2 - ) - sat = round( - random.uniform(const.Saturation.minimum, const.Saturation.maximum), 2 - ) + bri = const.Brightness.rand() + con = const.Contrast.rand() + sat = const.Saturation.rand() - r = round(random.uniform(const.COLOR.minimum, const.COLOR.maximum), 2) - g = round(random.uniform(const.COLOR.minimum, const.COLOR.maximum), 2) - b = round(random.uniform(const.COLOR.minimum, const.COLOR.maximum), 2) + r = const.Color.rand() + g = const.Color.rand() + b = const.Color.rand() print(f"\n-> Seed: {cc_seed}") print(f"Brightness:\t{bri}") diff --git a/scripts/cc_const.py b/scripts/cc_const.py index 02b079d..0717055 100644 --- a/scripts/cc_const.py +++ b/scripts/cc_const.py @@ -1,11 +1,17 @@ +import random + + class Param: def __init__(self, minimum: float, maximum: float, default: float): self.minimum = minimum self.maximum = maximum self.default = default + def rand(self) -> float: + return round(random.uniform(self.minimum, self.maximum), 2) + Brightness = Param(-5.0, 5.0, 0.0) Contrast = Param(-5.0, 5.0, 0.0) Saturation = Param(0.25, 1.75, 1.0) -COLOR = Param(-4.0, 4.0, 0.0) +Color = Param(-4.0, 4.0, 0.0) diff --git a/scripts/cc_style.py b/scripts/cc_style.py index 5764d8b..91700ae 100644 --- a/scripts/cc_style.py +++ b/scripts/cc_style.py @@ -1,5 +1,5 @@ import modules.scripts as scripts -import scripts.cc_const as const +import gradio as gr import json import os @@ -14,83 +14,95 @@ class StyleManager: self.STYLE_SHEET = None def load_styles(self): - if self.STYLE_SHEET is not None: - return - - try: + if os.path.isfile(STYLE_FILE): with open(STYLE_FILE, "r", encoding="utf-8") as json_file: - self.STYLE_SHEET = json.loads(json_file.read()) + self.STYLE_SHEET = json.load(json_file) print("[Vec. CC] Style Sheet Loaded...") - except IOError: + else: with open(STYLE_FILE, "w+", encoding="utf-8") as json_file: self.STYLE_SHEET = EMPTY_STYLE - json_file.write(json.dumps(self.STYLE_SHEET)) + json.dump(self.STYLE_SHEET, json_file) print("[Vec. CC] Creating Empty Style Sheet...") + return self.list_style() + def list_style(self): return list(self.STYLE_SHEET["styles"].keys()) - def get_style(self, style_name): - try: - style = self.STYLE_SHEET["styles"][style_name] - return ( - style["alt"], - style["brightness"], - style["contrast"], - style["saturation"], - style["rgb"][0], - style["rgb"][1], - style["rgb"][2], - ) + def get_style(self, style_name: str): + style: dict = self.STYLE_SHEET["styles"].get(style_name, None) - except KeyError: - print(f'\n[Warning] No Style of Name "{style_name}" Found!\n') - return ( - False, - const.Brightness.default, - const.Contrast.default, - const.Saturation.default, - const.COLOR.default, - const.COLOR.default, - const.COLOR.default, - ) + if not style: + print(f'\n[Error] No Style of name "{style_name}" was found!\n') + return [gr.update()] * 12 - def save_style(self, style_name, latent, bri, con, sat, r, g, b): - if style_name in self.STYLE_SHEET["styles"].keys(): - print( - f'\n[Warning] Duplicated Style Name "{style_name}" Detected! Values were not saved!\n' - ) + return ( + style.get("alt", gr.update()), + style.get("brightness", gr.update()), + style.get("contrast", gr.update()), + style.get("saturation", gr.update()), + *style.get("rgb", (gr.update(), gr.update(), gr.update())), + style.get("hr", gr.update()), + style.get("ad", gr.update()), + style.get("rn", gr.update()), + style.get("noise", gr.update()), + style.get("scaling", gr.update()), + ) + + def save_style( + self, + style_name: str, + latent: bool, + bri: float, + con: float, + sat: float, + r: float, + g: float, + b: float, + hr: bool, + ad: bool, + rn: bool, + noise: str, + scaling: str, + ): + if style_name in self.STYLE_SHEET["styles"]: + print(f'\n[Error] Duplicated Style Name: "{style_name}" Detected!') + print("Values were not saved!\n") return self.list_style() - style = { + new_style = { "alt": latent, "brightness": bri, "contrast": con, "saturation": sat, "rgb": [r, g, b], + "hr": hr, + "ad": ad, + "rn": rn, + "noise": noise, + "scaling": scaling, } - self.STYLE_SHEET["styles"].update({style_name: style}) + self.STYLE_SHEET["styles"].update({style_name: new_style}) with open(STYLE_FILE, "w+") as json_file: - json_file.write(json.dumps(self.STYLE_SHEET)) + json.dump(self.STYLE_SHEET, json_file) print(f'\nStyle of Name "{style_name}" Saved!\n') return self.list_style() - def delete_style(self, style_name): - try: - style = self.STYLE_SHEET["styles"][style_name] - self.STYLE_SHEET["deleted"].update({style_name: style}) - del self.STYLE_SHEET["styles"][style_name] - - except KeyError: - print(f'\n[Warning] No Style of Name "{style_name}" Found!\n') + def delete_style(self, style_name: str): + if style_name not in self.STYLE_SHEET["styles"]: + print(f'\n[Error] No Style of name "{style_name}" was found!\n') return self.list_style() - with open(STYLE_FILE, "w+") as json_file: - json_file.write(json.dumps(self.STYLE_SHEET)) + style: dict = self.STYLE_SHEET["styles"].get(style_name) + self.STYLE_SHEET["deleted"].update({style_name: style}) + del self.STYLE_SHEET["styles"][style_name] - print(f'\nStyle of Name "{style_name}" Deleted!\n') + with open(STYLE_FILE, "w+") as json_file: + json.dump(self.STYLE_SHEET, json_file) + + print(f'\nStyle of name "{style_name}" was deleted!\n') return self.list_style()