Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.1

0.5.1 / src / replace_color_dialog.luc

## Image › Adjustments › Replace Color…, as Photoshop's: while it is up, a
## click on the canvas samples the color to replace (Shift-click adds one,
## Alt-click takes one back out), Fuzziness sets how near a color must be,
## and Hue, Saturation and Lightness say what it becomes. It is destructive
## only, never an adjustment layer: the layer's pixels show the change live
## and Apply keeps it as one undo step. The work is luce-image's adjust pass
## (adjustment 17), a pixel at a time on the GPU, so it costs no read of the
## document on any size.
from luce_ui.ui import Text, HStack, Spacer, Dialog, Checkbox
from workspace import Workspace
from kit import Kit, SliderField
from preview_session import PreviewDialogSession
from luce_color import transfer

## luce-image's Replace Color adjustment, and how many samples it takes.
let replace_kind: int = 17
let most_samples: int = 6

pub class ReplaceColorDialog:
    pub let dialog: Dialog
    ## The layer's preview's lifecycle (preview_session.luc).
    pub let session: PreviewDialogSession
    let fuzziness: SliderField
    let hue: SliderField
    let saturation: SliderField
    let lightness: SliderField
    let preview_box: Checkbox
    let hint: Text
    ## The samples, linear-light triples, and which of them are taken back out.
    var colors: list[float]
    var subtract: list[bool]
    let workspace: Workspace

    pub func init(self, workspace: Workspace, kit: Kit) -> unit!:
        self.workspace = workspace
        self.colors = []
        self.subtract = []
        let font = kit.font
        self.fuzziness = kit.slider_field(0.0, 200.0, 40.0, 0, "", self.moved)
        self.hue = kit.slider_field(-180.0, 180.0, 0.0, 0, "°", self.moved)
        self.saturation = kit.slider_field(-100.0, 100.0, 0.0, 0, "", self.moved)
        self.lightness = kit.slider_field(-100.0, 100.0, 0.0, 0, "", self.moved)
        self.preview_box = kit.checkbox("Preview", self.show)
        self.preview_box.set_checked(true)
        self.hint = Text("", font = font, muted = true)
        self.dialog = Dialog([Text("Replace Color", font = font), kit.dialog_row("Fuzziness", self.fuzziness.row, 110.0), self.hint,
                              Text("Replacement", font = font), kit.dialog_row("Hue", self.hue.row, 110.0),
                              kit.dialog_row("Saturation", self.saturation.row, 110.0), kit.dialog_row("Lightness", self.lightness.row, 110.0),
                              HStack([self.preview_box, Spacer(), kit.button("Cancel", self.cancel), kit.button("Apply", self.apply)], spacing = 8.0)],
                             width = 460.0, spacing = 10.0)
        self.session = PreviewDialogSession(self.dialog, kit, "Replace Color")
        self.session.begin = workspace.previews.begin_preview
        self.session.preview = self.preview
        self.session.finish = self.finish
        kit.dialog_keys(self.dialog, self.cancel, self.apply)

    pub func open(self) -> unit!:
        if not self.session.open():
            return
        self.colors = []
        self.subtract = []
        self.preview_box.set_checked(true)
        self.workspace.eyedroppers.range_sample_for = self.sampled
        self.dialog.open()
        self.show()

    ## Set the replacement from code, as if the sliders moved: hue in degrees,
    ## saturation and lightness -100..100.
    pub func set_replacement(self, hue: float, saturation: float, lightness: float) -> unit!:
        self.hue.set_value(hue)
        self.saturation.set_value(saturation)
        self.lightness.set_value(lightness)
        self.show()

    func moved(self, value: float) -> unit!:
        _ = value
        self.show()

    ## A color from the canvas (sRGB): 0 samples anew, 1 adds, 2 takes back out.
    pub func sampled(self, color: list[float], how: int) -> unit!:
        if how == 0:
            self.colors = []
            self.subtract = []
        if self.subtract.length >= most_samples:
            return
        for value in color:
            self.colors.append(transfer.srgb_decode(value))
        self.subtract.append(how == 2)
        self.show()

    ## Show the change at the next frame.
    func show(self) -> unit!:
        var added = 0
        for taken in self.subtract:
            if not taken:
                added += 1
        let removed = self.subtract.length - added
        self.hint.set_text("Click a color to replace · Shift adds · Alt takes out." if self.subtract.length == 0 else
                           (f"{added} added, {removed} taken out (at most {most_samples})." if removed > 0 else f"{added} sampled (at most {most_samples})."))
        self.session.request()
        # Wake the loop for the frame that shows it.
        self.hint.layout().invalidate()

    ## The engine's numbers: hue in turns, saturation and lightness -1..1,
    ## fuzziness, the sample count, then each sample, a subtracted one's red
    ## as -1 - red (adjust.frag).
    pub func numbers(self) -> list[float]:
        var numbers = [self.hue.value() / 360.0, self.saturation.value() / 100.0, self.lightness.value() / 100.0,
                       self.fuzziness.value(), float(self.subtract.length)]
        var index = 0
        while index < self.subtract.length:
            let red = self.colors[index * 3]
            numbers = numbers + [(-1.0 - red) if self.subtract[index] else red, self.colors[index * 3 + 1], self.colors[index * 3 + 2]]
            index += 1
        return numbers

    ## The change on the layer's preview, or the layer as it was with Preview off.
    func preview(self) -> unit!:
        self.show_change() catch failure:
            self.session.fail(failure.message)
            return
        self.session.refresh()

    func show_change(self) -> unit!:
        if not self.preview_box.is_checked():
            self.workspace.canvas.show_preview_original()
            self.workspace.touch()
            return
        self.workspace.previews.preview_adjust(replace_kind, self.numbers())

    pub func cancel(self) -> unit!:
        self.session.close(false)

    pub func apply(self) -> unit!:
        self.session.close(true)

    ## The session's end: the eyedropper disarmed; Apply (`keep`, with a
    ## sample) keeps the change as one step.
    func finish(self, keep: bool) -> unit!:
        self.workspace.eyedroppers.range_sample_for = none
        let kept = keep and self.subtract.length > 0
        if kept:
            # What the controls say is kept, the preview shown or not.
            self.workspace.previews.preview_adjust(replace_kind, self.numbers()) catch failure:
                self.session.fail(failure.message)
        self.workspace.previews.end_preview(kept)