Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.1

0.5.1 / src / adjustments.luc

## Every color adjustment in one table, which the adjust dialog builds its
## rows from: the sliders in the units Photoshop shows, the pages some show one
## at a time (Levels' channels, Hue/Saturation's ranges, Color Balance's
## tones), the checkboxes, and how each shown value becomes the number
## luce-image's adjust pass takes (adjust.frag lists them). The same settings
## serve the destructive path (Adjust menu: the pixels change once) and
## adjustment layers (live, editable) — Compositor's model, with a table where
## Compositor has a sheet per kind.

## One slider: its label, range, starting value, decimals and suffix, and the
## factor the engine's number is the shown value times.
pub class Knob:
    pub let label: str
    pub let minimum: float
    pub let maximum: float
    pub let value: float
    pub let scale: float
    pub let decimals: int
    pub let suffix: str

    pub func init(self, label: str, minimum: float, maximum: float, value: float, scale: float = 1.0, decimals: int = 0, suffix: str = ""):
        self.label = label
        self.minimum = minimum
        self.maximum = maximum
        self.value = value
        self.scale = scale
        self.decimals = decimals
        self.suffix = suffix

## One checkbox and whether it starts checked; the engine takes 1 or 0.
pub class Toggle:
    pub let label: str
    pub let on: bool

    pub func init(self, label: str, on: bool = false):
        self.label = label
        self.on = on

## One adjustment. Its engine numbers run page by page — every knob of the
## first page, then of the next — then one per toggle.
pub class Adjustment:
    pub let kind: int
    pub let name: str
    pub let knobs: list[Knob]
    pub let pages: list[str]
    pub let toggles: list[Toggle]
    ## Whether it can be an adjustment layer (every kind now; Curves keeps its
    ## curves with the layer).
    pub let layerable: bool
    ## Which menu applies it to the pixels: `in_adjust`, `in_filter` (a look
    ## such as Vignette, which Photoshop lists under Filter) or `layer_only`.
    pub let menu: int

    pub func init(self, kind: int, name: str, knobs: list[Knob], pages: list[str]? = none, toggles: list[Toggle]? = none, layerable: bool = true, menu: int = 0):
        self.kind = kind
        self.name = name
        self.knobs = knobs
        self.pages = []
        self.toggles = []
        if let given = pages:
            self.pages = given
        if let given = toggles:
            self.toggles = given
        self.layerable = layerable
        self.menu = menu

    ## How many pages of knobs it has: 1 when it shows all at once.
    pub func page_count(self) -> int:
        return self.pages.length if self.pages.length > 0 else 1

    ## How many shown values it keeps: every page's knobs, then the toggles.
    pub func value_count(self) -> int:
        return self.page_count() * self.knobs.length + self.toggles.length

    ## The shown values it starts from.
    pub func defaults(self) -> list[float]:
        var values: list[float] = []
        var page = 0
        while page < self.page_count():
            for knob in self.knobs:
                values.append(knob.value)
            page += 1
        for toggle in self.toggles:
            values.append(1.0 if toggle.on else 0.0)
        return values

    ## The engine's numbers for shown `values`: each knob's times its scale,
    ## the toggles as they are.
    pub func engine(self, values: list[float]) -> list[float]:
        var numbers: list[float] = []
        var index = 0
        for value in values:
            let knob = index % self.knobs.length if self.knobs.length > 0 else 0
            let is_knob = index < self.page_count() * self.knobs.length
            numbers.append(value * self.knobs[knob].scale if is_knob else value)
            index += 1
        return numbers

    ## The shown values for the engine's `numbers` (an adjustment layer's,
    ## to edit it): `engine` undone; numbers past its own are left out.
    pub func shown_of(self, numbers: list[float]) -> list[float]:
        var values: list[float] = []
        var index = 0
        while index < self.value_count():
            let knob = index % self.knobs.length if self.knobs.length > 0 else 0
            let is_knob = index < self.page_count() * self.knobs.length
            let number = numbers[index] if index < numbers.length else 0.0
            values.append(number / self.knobs[knob].scale if is_knob else number)
            index += 1
        return values

## Where an adjustment applies to pixels: the Adjust menu, the Filter menu, or
## nowhere (it is only ever a layer).
pub let in_adjust: int = 0
pub let in_filter: int = 1
pub let layer_only: int = 2

## The adjustments, each at its engine kind's index.
pub func adjustments() -> list[Adjustment]:
    let percent = 0.01
    let level = 1.0 / 255.0
    let turn = 1.0 / 360.0
    return [Adjustment(0, "Brightness / Contrast", [Knob("Brightness", -100.0, 100.0, 0.0, percent), Knob("Contrast", -100.0, 100.0, 0.0, percent)]),
            Adjustment(1, "Hue / Saturation", [Knob("Hue", -180.0, 180.0, 0.0, turn, 0, "°"), Knob("Saturation", -100.0, 100.0, 0.0, percent),
                                               Knob("Lightness", -100.0, 100.0, 0.0, percent)],
                       pages = ["Master", "Reds", "Yellows", "Greens", "Cyans", "Blues", "Magentas"], toggles = [Toggle("Colorize")]),
            Adjustment(2, "Invert", []),
            Adjustment(3, "Levels", [Knob("In Black", 0.0, 255.0, 0.0, level), Knob("In White", 0.0, 255.0, 255.0, level),
                                     Knob("Gamma", 0.1, 9.99, 1.0, 1.0, 2), Knob("Out Black", 0.0, 255.0, 0.0, level),
                                     Knob("Out White", 0.0, 255.0, 255.0, level)],
                       pages = ["RGB", "Red", "Green", "Blue"]),
            Adjustment(4, "Desaturate", []),
            Adjustment(5, "Threshold", [Knob("Level", 0.0, 255.0, 128.0, level)]),
            Adjustment(6, "Posterize", [Knob("Levels", 2.0, 32.0, 4.0)]),
            Adjustment(7, "Curves", []),
            Adjustment(8, "Exposure", [Knob("Exposure", -20.0, 20.0, 0.0, 1.0, 2), Knob("Offset", -0.5, 0.5, 0.0, 1.0, 2),
                                       Knob("Gamma", 0.01, 9.99, 1.0, 1.0, 2)]),
            Adjustment(9, "Black & White", [Knob("Reds", -200.0, 300.0, 40.0, percent, 0, "%"), Knob("Yellows", -200.0, 300.0, 60.0, percent, 0, "%"),
                                            Knob("Greens", -200.0, 300.0, 40.0, percent, 0, "%"), Knob("Cyans", -200.0, 300.0, 60.0, percent, 0, "%"),
                                            Knob("Blues", -200.0, 300.0, 20.0, percent, 0, "%"), Knob("Magentas", -200.0, 300.0, 80.0, percent, 0, "%"),
                                            Knob("Tint Hue", 0.0, 360.0, 40.0, turn, 0, "°"), Knob("Tint Sat.", 0.0, 100.0, 20.0, percent, 0, "%")],
                       toggles = [Toggle("Tint")]),
            Adjustment(10, "Color Balance", [Knob("Cyan–Red", -100.0, 100.0, 0.0, percent), Knob("Magenta–Green", -100.0, 100.0, 0.0, percent),
                                             Knob("Yellow–Blue", -100.0, 100.0, 0.0, percent)],
                       pages = ["Shadows", "Midtones", "Highlights"], toggles = [Toggle("Preserve Luminosity", true)]),
            Adjustment(11, "Gradient Map", [], toggles = [Toggle("Reverse")]),
            Adjustment(12, "Vignette", [Knob("Amount", -100.0, 100.0, -50.0, percent, 0, "%"), Knob("Midpoint", 0.0, 100.0, 50.0, percent, 0, "%"),
                                        Knob("Roundness", -100.0, 100.0, 0.0, percent), Knob("Feather", 0.0, 100.0, 50.0, percent, 0, "%")], menu = in_filter),
            Adjustment(13, "Grain", [Knob("Amount", 0.0, 100.0, 25.0, percent, 0, "%"), Knob("Size", 1.0, 20.0, 2.0, 1.0, 1, " px"),
                                     Knob("Roughness", 0.0, 100.0, 50.0, percent, 0, "%")], menu = in_filter),
            # Add Noise (the Filter menu's) makes its grain once; this one is live, on a layer.
            Adjustment(14, "Noise", [Knob("Amount", 0.0, 300.0, 12.0, 1.0 / 300.0, 1, "%")], toggles = [Toggle("Monochromatic"), Toggle("Uniform")], menu = layer_only),
            # Gaussian Blur (the Filter menu's) blurs the pixels once; this one blurs what is below, live.
            Adjustment(15, "Blur", [Knob("Radius", 0.5, 250.0, 8.0, 1.0, 1, " px")], menu = layer_only),
            Adjustment(16, "Camera Raw", [Knob("Temperature", -100.0, 100.0, 0.0, percent), Knob("Tint", -100.0, 100.0, 0.0, percent),
                                          Knob("Exposure", -5.0, 5.0, 0.0, 1.0, 2), Knob("Contrast", -100.0, 100.0, 0.0, percent),
                                          Knob("Highlights", -100.0, 100.0, 0.0, percent), Knob("Shadows", -100.0, 100.0, 0.0, percent),
                                          Knob("Whites", -100.0, 100.0, 0.0, percent), Knob("Blacks", -100.0, 100.0, 0.0, percent),
                                          Knob("Vibrance", -100.0, 100.0, 0.0, percent), Knob("Saturation", -100.0, 100.0, 0.0, percent)], menu = in_filter)]

## The Gradient Map's kind: its colors, the foreground to the background, go after its toggle.
## The kinds some code treats apart: Curves keeps curves, Gradient Map colors.
pub let curves_adjustment: int = 7
pub let gradient_map: int = 11
## The kinds the dialog shows no knobs for and the Adjust menu applies at once.
pub func applies_at_once(kind: int) -> bool:
    return kind == 2 or kind == 4

## The rows past the adjustments' (0..16): the filters, in the dialog.
pub let blur_kind: int = 17
pub let noise_kind: int = 18
## The filters luce-image numbers 0..3 (`Canvas.filter_layer`) from here:
## Motion Blur, Lens Correction, Bloom, Tonal Contrast.
pub let first_filter_kind: int = 19

## The Filter menu's dialogs, from `blur_kind` on, in the table's terms.
pub func filter_table() -> list[Adjustment]:
    return [Adjustment(blur_kind, "Gaussian Blur", [Knob("Radius", 0.5, 250.0, 8.0, 1.0, 1, " px")], layerable = false),
            # Photoshop's Add Noise amount: at 300% the grain's deviation is full scale.
            Adjustment(noise_kind, "Add Noise", [Knob("Amount", 0.0, 300.0, 12.0, 1.0 / 300.0, 1, "%")], toggles = [Toggle("Monochromatic"), Toggle("Uniform")], layerable = false),
            Adjustment(first_filter_kind, "Motion Blur", [Knob("Angle", -180.0, 180.0, 0.0, 1.0, 0, "°"), Knob("Distance", 1.0, 400.0, 10.0, 1.0, 0, " px")], layerable = false),
            Adjustment(first_filter_kind + 1, "Lens Correction", [Knob("Distortion", -100.0, 100.0, 0.0, 0.01), Knob("Red–Cyan Fringe", -100.0, 100.0, 0.0, 0.01),
                                                                  Knob("Blue–Yellow Fringe", -100.0, 100.0, 0.0, 0.01)], layerable = false),
            Adjustment(first_filter_kind + 2, "Bloom", [Knob("Threshold", 0.0, 100.0, 70.0, 0.01, 0, "%"), Knob("Radius", 1.0, 250.0, 20.0, 1.0, 0, " px"),
                                                        Knob("Strength", 0.0, 200.0, 60.0, 0.01, 0, "%")], layerable = false),
            Adjustment(first_filter_kind + 3, "Tonal Contrast", [Knob("Amount", 0.0, 200.0, 60.0, 0.01, 0, "%"), Knob("Radius", 1.0, 250.0, 30.0, 1.0, 0, " px")], layerable = false)]