dymokomi/luced-2d
Source of release 0.5.0
0.5.0 / tests / panel_tests.luc
## The window's panels and dialogs: Layers, size dialogs, masks, effects,
## filters, Color Range and Replace Color, Hue/Saturation on the image, the
## panel catalog, the navigator, aligning, dock layouts, Select and Mask,
## Quick Mask and the Color panel.
from luce_ui.ui import Font
from luce_gpu.gpu import Frame
from workspace import Workspace
from panels import Panels
from actions import Actions
from view import CanvasView
from navigator_panel import view_box
from layer_ops import OpacityKeys, set_opacity_all
from size_fields import SizeFields
from magnetic_lasso import MagneticLasso, view_level
from adjustments import adjustments
from settings import Settings
from luce_ui.ui import Event, Application
from layer_effects import EffectRows
from adjustment_controls import range_of
from tools import group_of
from script_commands import parse_line, ScriptCommands
from luce_std import paths, files
from luce_window.input import EventKind, Key
from swatches_panel import swatches_of
from align import align_layers
from dock_layout import parse_layout
from luce_gpu.gpu import Rect
from kit import Kit
from color_panel import ColorPanel
from luce_ui.ui import ColorWheel
from luce_color import transfer
from luce_std import math
from luce_window import window
from test_input import press, drag_to, release, near, window_command, foreground
## The Layers panel's operations: titles fitted to the selection, groups
## duplicated with what they hold, Merge Group, opacity for every selected
## layer and typed as digits.
pub func layer_panel_ops() -> unit!:
let workspace = Workspace(64, 48)
workspace.canvas.bind_headless()
let actions = Actions()
let panels = Panels(workspace, actions, Font())
actions.add_layer.trigger()
actions.add_layer.trigger()
assert(workspace.canvas.layer_count() == 3 and workspace.layers.selected == 2)
workspace.layers.toggle_picked(1)
panels.layers_panel.fit_titles()
assert(actions.merge_down.text() == "Merge Layers" and actions.delete_layer.text() == "Delete Selected Layers" and not actions.rename_layer.is_enabled())
actions.group_layers.trigger()
assert(workspace.canvas.layer_count() == 4 and workspace.canvas.layer_is_group(workspace.layers.selected))
panels.layers_panel.fit_titles()
assert(actions.merge_down.text() == "Merge Group")
# The group's copy holds copies of its two layers.
actions.duplicate_layer.trigger()
assert(workspace.canvas.layer_count() == 7 and workspace.canvas.layer_is_group(workspace.layers.selected))
actions.merge_down.trigger()
assert(workspace.canvas.layer_count() == 5 and not workspace.canvas.layer_is_group(workspace.layers.selected))
assert(workspace.canvas.undo_name() == "Merge Group")
# Every selected layer takes the opacity, in one step.
workspace.layers.select(0)
workspace.layers.toggle_picked(workspace.canvas.layer_count() - 1)
set_opacity_all(workspace, 0.25)
assert(workspace.canvas.layer_opacity(0) == 0.25 and workspace.canvas.layer_opacity(workspace.canvas.layer_count() - 1) == 0.25)
let keys = OpacityKeys()
assert(keys.typed(5, 1000) == 0.5 and keys.typed(5, 1200) == 0.55 and keys.typed(0, 5000) == 1.0 and keys.typed(0, 5100) == 0.0)
panels.close()
## The size dialogs: units and the kept ratio, Canvas Size's anchor and
## extension layer, Trim.
pub func size_dialogs() -> unit!:
let fields = SizeFields(Font(), true, true)
fields.reset(200, 100, 72.0)
assert(fields.pixels(true) == 200 and fields.pixels(false) == 100)
fields.set_pixels(300, 150)
assert(fields.pixels(false) == 150)
fields.close()
let workspace = Workspace(40, 30)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
let dialogs = panels.size_dialogs
workspace.colors.set_color(1.0, 0.0, 0.0)
workspace.painting.fill_selection(false)
# Grown to 60 by 50 about the bottom-right corner, the new canvas white on a layer of its own.
dialogs.canvas_size.resize(60, 50, 8, 4)
assert(workspace.canvas.width() == 60 and workspace.canvas.height() == 50 and workspace.canvas.layer_count() == 2)
assert(workspace.canvas.layer_name(0) == "Canvas Extension" and workspace.canvas.undo_name() == "Canvas Size")
# Trimmed by the top-left color (the white extension): back to the red picture.
assert(dialogs.trim.trim(1, true, true, true, true))
assert(workspace.canvas.width() == 40 and workspace.canvas.height() == 30)
assert(not dialogs.trim.trim(0, true, true, true, true))
workspace.canvas.undo()
assert(workspace.canvas.width() == 60)
# One dialog at a time: Canvas Size waits while New Canvas is open. Escape
# cancels the open one; Enter accepts it (New Canvas creates).
let app = Application(panels.body)
app.layout(1200.0, 800.0)
dialogs.new_canvas.open()
dialogs.canvas_size.open()
assert(dialogs.new_canvas.dialog.is_open() and not dialogs.canvas_size.dialog.is_open())
app.layout(1200.0, 800.0)
app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
assert(not dialogs.new_canvas.dialog.is_open())
let before = workspace.documents.count()
dialogs.new_canvas.open()
app.layout(1200.0, 800.0)
app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(not dialogs.new_canvas.dialog.is_open() and workspace.documents.count() == before + 1)
app.close()
panels.close()
## Masks: a new one is the target, Unlink keeps it put, Copy and Paste give
## another layer the same mask.
pub func mask_actions() -> unit!:
let workspace = Workspace(120, 80)
workspace.canvas.bind_headless()
workspace.edits.add_layer()
let top = workspace.layers.selected
workspace.edits.toggle_mask()
assert(workspace.canvas.layer_masked(top) and workspace.canvas.editing_mask())
workspace.edits.toggle_mask_link()
assert(not workspace.canvas.layer_mask_linked(top))
workspace.edits.copy_mask()
workspace.layers.select(0)
assert(workspace.edits.mask_source_present())
workspace.edits.paste_mask()
assert(workspace.canvas.layer_masked(0) and workspace.canvas.editing_mask())
## A layer's effects listed under it: the eye hides one and keeps it listed,
## the cross deletes it, and one copies to another layer with its settings.
pub func effect_rows() -> unit!:
let workspace = Workspace(40, 30)
workspace.canvas.bind_headless()
workspace.edits.add_layer()
let top = workspace.layers.selected
workspace.previews.set_style_value(0, 1.0)
workspace.previews.set_style_value(3, 9.0)
let rows = EffectRows()
let shadow = 5
assert(rows.listed(workspace.canvas, top) == [shadow])
rows.toggle(workspace, top, shadow)
assert(not rows.showing(workspace.canvas, top, shadow) and rows.listed(workspace.canvas, top) == [shadow])
rows.toggle(workspace, top, shadow)
assert(rows.showing(workspace.canvas, top, shadow))
rows.copy(workspace, top, shadow, 0)
assert(workspace.canvas.style_value(0, 0) == 1.0 and workspace.canvas.style_value(0, 3) == 9.0)
rows.remove(workspace, top, shadow)
assert(rows.listed(workspace.canvas, top).length == 0)
## The Filter menu's dialogs preview on the layer and keep one step each;
## Vignette, Grain and Noise are also live adjustment layers.
pub func filter_dialogs() -> unit!:
let workspace = Workspace(80, 60)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
workspace.colors.set_color(0.5, 0.5, 0.5)
workspace.painting.fill_selection(false)
let names = ["Motion Blur", "Lens Correction", "Bloom", "Tonal Contrast"]
var filter = 0
while filter < 4:
panels.adjust.filter(filter)
assert(workspace.previews.previewing())
panels.adjust.apply()
assert(not workspace.previews.previewing() and workspace.canvas.undo_name() == names[filter])
filter += 1
panels.adjust.open(12)
panels.adjust.apply()
let before = workspace.canvas.layer_count()
for kind in [12, 13, 14, 15]:
let spec = adjustments()[kind]
_ = workspace.previews.add_adjustment_layer(kind, spec.name, spec.engine(spec.defaults()))
assert(workspace.canvas.layer_count() == before + 4 and workspace.canvas.layer_adjustment(workspace.layers.selected) == 15)
panels.close()
## Color Range: a click on the disc samples its color and selects it; Shift
## adds the ground, Cancel puts the old selection back; OK keeps it as one step.
pub func color_range() -> unit!:
let workspace = Workspace(96, 64)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
let view = panels.view
workspace.viewport.fitted = true
workspace.viewport.zoom = 1.0
workspace.viewport.offset_x = 0.0
workspace.viewport.offset_y = 0.0
workspace.colors.set_color(0.0, 0.0, 1.0)
workspace.painting.fill_selection(false)
workspace.canvas.select_ellipse(33, 17, 30, 30, 0)
workspace.colors.set_color(1.0, 0.8, 0.0)
workspace.painting.fill_selection(false)
workspace.canvas.deselect()
workspace.canvas.select_rectangle(0, 0, 8, 8, 0)
panels.color_range.open()
assert(workspace.eyedroppers.range_sample_for != none)
press(view, 48.0, 32.0)
release(view, 48.0, 32.0)
panels.color_range.session.poll()
# Shown live, out of history.
assert(workspace.canvas.undo_name() == "Select" and workspace.canvas.selected_at(48, 32) == 255 and workspace.canvas.selected_at(4, 4) == 0)
press(view, 4.0, 4.0, shift = true)
release(view, 4.0, 4.0)
# Nothing is worked out until the frame; then the latest change, once.
assert(workspace.canvas.selected_at(4, 4) == 0)
panels.color_range.session.poll()
assert(workspace.canvas.selected_at(48, 32) == 255 and workspace.canvas.selected_at(4, 4) == 255)
panels.color_range.cancel()
assert(workspace.eyedroppers.range_sample_for == none and workspace.canvas.undo_name() == "Select" and not workspace.canvas.can_redo() and workspace.canvas.selected_at(48, 32) == 0)
# OK: one step.
panels.color_range.open()
press(view, 48.0, 32.0)
release(view, 48.0, 32.0)
panels.color_range.apply()
assert(workspace.canvas.undo_name() == "Color Range" and workspace.canvas.selected_at(48, 32) == 255 and workspace.canvas.selected_at(4, 4) == 0)
# Headless, from a script: the disc's color and the shadows.
workspace.selection.select_color_range(0, 40.0, false, [1.0, 0.9, 0.0], [false])
assert(workspace.canvas.selected_at(48, 32) == 255 and workspace.canvas.selected_at(4, 4) == 0)
workspace.selection.select_color_range(9, 40.0, false, [], [])
assert(workspace.canvas.selected_at(48, 32) == 0)
panels.close()
## Replace Color: a click on the disc samples it, the hue turns it from
## yellow to red live, the ground stays; Cancel puts it back, Apply keeps one
## step; it is never an adjustment layer.
pub func replace_color() -> unit!:
let workspace = Workspace(96, 64)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
let view = panels.view
workspace.viewport.fitted = true
workspace.viewport.zoom = 1.0
workspace.viewport.offset_x = 0.0
workspace.viewport.offset_y = 0.0
workspace.colors.set_color(0.0, 0.0, 1.0)
workspace.painting.fill_selection(false)
workspace.canvas.select_ellipse(33, 17, 30, 30, 0)
workspace.colors.set_color(1.0, 1.0, 0.0)
workspace.painting.fill_selection(false)
workspace.canvas.deselect()
let before = workspace.canvas.undo_name()
panels.replace_color.open()
assert(workspace.eyedroppers.range_sample_for != none)
press(view, 48.0, 32.0)
release(view, 48.0, 32.0)
panels.replace_color.set_replacement(-60.0, 0.0, 0.0)
panels.replace_color.session.poll()
workspace.canvas.pick(48, 32)
assert(workspace.canvas.picked_red() > 0.9 and workspace.canvas.picked_green() < 0.05)
workspace.canvas.pick(4, 4)
assert(workspace.canvas.picked_blue() > 0.9 and workspace.canvas.picked_red() < 0.05)
panels.replace_color.cancel()
workspace.canvas.pick(48, 32)
assert(workspace.eyedroppers.range_sample_for == none and workspace.canvas.picked_green() > 0.9 and workspace.canvas.undo_name() == before)
panels.replace_color.open()
press(view, 48.0, 32.0)
release(view, 48.0, 32.0)
panels.replace_color.set_replacement(-60.0, 0.0, 0.0)
panels.replace_color.apply()
workspace.canvas.pick(48, 32)
assert(workspace.canvas.undo_name() == "Replace Color" and workspace.canvas.picked_green() < 0.05)
var refused = false
_ = workspace.canvas.add_adjustment_layer("Replace", 17) catch failure:
refused = true
recover 0
assert(refused)
panels.close()
## Hue/Saturation On Image: a press picks the range of the color there; a
## drag right raises its saturation.
pub func on_image_adjustment() -> unit!:
let workspace = Workspace(120, 80)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
let view = panels.view
workspace.viewport.fitted = true
workspace.viewport.zoom = 1.0
workspace.viewport.offset_x = 0.0
workspace.viewport.offset_y = 0.0
workspace.colors.set_color(1.0, 0.0, 0.0)
workspace.painting.fill_selection(false)
panels.adjust.open(1)
panels.adjust.controls.toggle_on_image()
press(view, 30.0, 30.0)
drag_to(view, 70.0, 30.0)
release(view, 70.0, 30.0)
# Reds (page 1) saturation went up by half the 40 points moved.
let shown = panels.adjust.controls.values(1)
assert(shown[3 + 1] > 15.0)
panels.adjust.cancel()
assert(workspace.eyedroppers.drag_for == none)
panels.close()
## Every hue finds its range: Reds 1 … Magentas 6, a grey Master.
pub func hue_ranges() -> unit!:
assert(range_of([0.82, 0.52, 0.82]) == 6 and range_of([0.9, 0.6, 0.91]) == 6 and range_of([0.9, 0.2, 0.2]) == 1)
assert(range_of([0.9, 0.9, 0.1]) == 2 and range_of([0.1, 0.9, 0.1]) == 3 and range_of([0.1, 0.9, 0.9]) == 4)
assert(range_of([0.1, 0.1, 0.9]) == 5 and range_of([0.5, 0.5, 0.5]) == 0 and range_of([0.9, 0.3, 0.8]) == 6)
## The panel catalog: the Window menu opens a panel beside the Layers and
## Properties column, ticked; again, it closes and comes back as it was; with
## that column closed a new panel docks right of the viewport; Brush Settings
## floats. The Swatches text survives its round trip.
pub func panel_catalog() -> unit!:
let workspace = Workspace(300, 200)
let actions = Actions()
let panels = Panels(workspace, actions, Font())
let catalog = panels.catalog
assert(catalog.is_open("layers") and catalog.is_open("properties") and not catalog.is_open("swatches"))
assert(window_command(actions, "layers").is_checked() and not window_command(actions, "swatches").is_checked())
window_command(actions, "swatches").trigger()
let swatches = catalog.panel("swatches") else trap("swatches is a panel")
assert(catalog.is_open("swatches") and panels.dock.same_group(swatches, panels.properties.panel))
assert(window_command(actions, "swatches").is_checked())
# Picking an open one again closes it; once more brings the same panel back.
window_command(actions, "swatches").trigger()
assert(not catalog.is_open("swatches") and not window_command(actions, "swatches").is_checked())
window_command(actions, "swatches").trigger()
let again = catalog.panel("swatches") else trap("swatches is a panel")
assert(again is swatches and catalog.is_open("swatches"))
# Opening one already open selects it rather than adding it twice.
let count = panels.dock.panel_count()
catalog.open("swatches")
assert(panels.dock.panel_count() == count)
# With Layers and Properties closed, a new panel goes right of the viewport.
window_command(actions, "layers").trigger()
window_command(actions, "properties").trigger()
window_command(actions, "swatches").trigger()
assert(not catalog.is_open("layers") and not catalog.is_open("properties"))
let groups = panels.dock.group_count()
window_command(actions, "navigator").trigger()
let navigator = catalog.panel("navigator") else trap("navigator is a panel")
assert(catalog.is_open("navigator") and not panels.dock.same_group(navigator, panels.viewport_panel) and panels.dock.group_count() == groups + 1)
# The others open and show the document without failing, History included.
for id in ["color", "history", "info", "brushes"]:
window_command(actions, id).trigger()
assert(catalog.is_open(id))
actions.add_layer.trigger()
panels.refresh()
catalog.poll()
window_command(actions, "brush_settings").trigger()
assert(panels.dock.is_floating(panels.brush.panel))
# The texts the panels keep in the settings.
assert(swatches_of("#FF0000 nonsense #00ff00") == ["#ff0000", "#00ff00"])
let kept = Settings()
kept.swatches = "#123456"
let reread = Settings(kept.serialize())
assert(reread.swatches == "#123456")
# File › Exit on a changed document asks rather than ending the run.
panels.files.quit()
panels.close()
## Layer › Align and Distribute on three painted layers, each one undo step.
pub func aligning() -> unit!:
let workspace = Workspace(300, 200)
workspace.canvas.bind_headless()
workspace.edits.add_layer()
workspace.edits.add_layer()
let painted = [[10, 20, 30, 30], [100, 50, 20, 40], [200, 120, 40, 10]]
for index in 0..<3:
workspace.layers.select(index)
workspace.canvas.select_rectangle(painted[index][0], painted[index][1], painted[index][2], painted[index][3])
workspace.painting.fill_selection(false)
workspace.selection.deselect()
workspace.layers.select(0)
workspace.layers.toggle_picked(1)
workspace.layers.toggle_picked(2)
assert(workspace.layers.selected_ids().length == 3)
# Several layers line up against the box around them all.
assert(align_layers(workspace, "left"))
assert(left_of(workspace, 0) == 10 and left_of(workspace, 1) == 10 and left_of(workspace, 2) == 10)
assert(workspace.canvas.undo_name() == "Align Left Edges")
workspace.undo()
assert(left_of(workspace, 1) == 100 and left_of(workspace, 2) == 200 and workspace.layers.selected_ids().length == 3)
# Centers meet at the middle of that box, 10..240.
assert(align_layers(workspace, "hcenter"))
assert(left_of(workspace, 0) == 110 and left_of(workspace, 1) == 115 and left_of(workspace, 2) == 105)
workspace.undo()
# Distributing keeps the outermost two and centers the middle one between them.
assert(align_layers(workspace, "distribute_h"))
assert(left_of(workspace, 0) == 10 and left_of(workspace, 1) == 113 and left_of(workspace, 2) == 200)
workspace.undo()
assert(left_of(workspace, 1) == 100)
# Alone, a layer lines up against the canvas; with a selection, against that.
workspace.layers.select(2)
assert(align_layers(workspace, "bottom") and top_of(workspace, 2) == 190)
workspace.canvas.select_rectangle(50, 40, 100, 100)
assert(align_layers(workspace, "right") and left_of(workspace, 2) == 110)
assert(not align_layers(workspace, "right"))
func left_of(workspace: Workspace, index: int) -> int!:
assert(workspace.canvas.measure_content(workspace.canvas.layer_id(index)))
return workspace.canvas.content_left()
func top_of(workspace: Workspace, index: int) -> int!:
assert(workspace.canvas.measure_content(workspace.canvas.layer_id(index)))
return workspace.canvas.content_top()
## The dock's arrangement kept across runs: what it writes reads back into
## the same arrangement, panels it no longer knows are left out, anything
## malformed is refused, and Reset Layout brings back the first one.
pub func dock_layouts(arguments: list[str]) -> unit!:
let folder = arguments[arguments.length - 1]
let path = paths.join(folder, "layout.txt")
for broken in ["", "luce-dock 2\ngroup 0 viewport", "luce-dock 1\nsplit h 2 \ngroup 0 a\ngroup 0 b",
"luce-dock 1\ngroup 3 viewport", "luce-dock 1\nsplit v 0.5\ngroup 0 viewport", "luce-dock 1\ngroup 0 viewport\nfloat 1 2 x 4 info"]:
assert(parse_layout(broken) == none)
let panels = Panels(Workspace(300, 200), Actions(), Font())
let first = panels.dock.layout_text()
# Channels and Vector are tabs beside Layers, which shows, as Photoshop groups Layers, Channels and Paths.
assert(first == "luce-dock 1\nsplit h 0.76\ngroup 0 viewport\nsplit v 0.55\ngroup 0 layers channels vector\ngroup 0 properties\n")
# Reset Layout brings a floating viewport back into the dock.
let floated = Panels(Workspace(300, 200), Actions(), Font())
floated.dock.float(floated.viewport_panel, Rect(10.0, 10.0, 200.0, 150.0))
floated.dock_layout.reset()
assert(floated.dock.layout_text() == first)
panels.catalog.open("swatches")
panels.catalog.open("history")
let navigator = panels.catalog.panel("navigator") else trap("navigator is a panel")
panels.dock.float(navigator, Rect(40.0, 50.0, 260.0, 220.0))
panels.dock_layout.restore(path)
panels.dock_layout.save()
let kept = panels.dock.layout_text()
assert(files.read_text(path, limit = 65536) == kept)
# A new window brings back the same arrangement.
let again = Panels(Workspace(300, 200), Actions(), Font())
again.dock_layout.restore(path)
assert(again.dock.layout_text() == kept and again.dock.is_floating(again.catalog.panel("navigator") else trap("navigator")))
# Unknown panels are left out; a kept file without the viewport is passed over.
files.write_text(path, "luce-dock 1\nsplit v 0.4\ngroup 0 viewport bogus\ngroup 0 color nothing\n")
let pruned = Panels(Workspace(300, 200), Actions(), Font())
pruned.dock_layout.restore(path)
assert(pruned.dock.layout_text() == "luce-dock 1\nsplit v 0.4\ngroup 0 viewport\ngroup 0 color\n")
files.write_text(path, "luce-dock 1\ngroup 0 layers\n")
let missing = Panels(Workspace(300, 200), Actions(), Font())
missing.dock_layout.restore(path)
assert(missing.dock.layout_text() == first)
# Window › Reset Layout.
again.dock_layout.reset()
assert(again.dock.layout_text() == first)
panels.close()
again.close()
pruned.close()
missing.close()
## The Navigator's box is what the view shows of the document, cut to it.
pub func navigator_boxes() -> unit!:
let placed = [0.2, 4.0, 10.0]
# The whole 1280x800 document inside the view at 72%: the whole thumbnail.
let whole = view_box(placed, 1280.0, 800.0, 1200.0, 900.0, 0.72, 139.2, 162.0) else trap("whole box")
assert(near(whole.x, 4.0) and near(whole.y, 10.0) and near(whole.width, 256.0) and near(whole.height, 160.0))
# At 200%, the view's middle part.
let inner = view_box(placed, 1280.0, 800.0, 1000.0, 600.0, 2.0, -500.0, -200.0) else trap("inner box")
assert(near(inner.x, 54.0) and near(inner.y, 30.0) and near(inner.width, 100.0) and near(inner.height, 60.0))
# Past the right edge: cut there.
let edge = view_box(placed, 1280.0, 800.0, 1000.0, 800.0, 1.0, -800.0, 0.0) else trap("edge box")
assert(near(edge.x, 164.0) and near(edge.width, 96.0) and near(edge.height, 160.0))
# A view off the document shows none of it.
assert(view_box(placed, 1280.0, 800.0, 1000.0, 600.0, 2.0, 5000.0, 0.0) == none)
## Select › Modify › Border and Smooth, Grow and Similar, and Transform
## Selection through the workspace, each one undo step.
pub func refining_selections() -> unit!:
let workspace = Workspace(400, 300)
workspace.canvas.bind_headless()
# Red on the left, blue in a column, red again on the right.
foreground(workspace, 1.0, 0.0, 0.0)
workspace.canvas.select_rectangle(0, 0, 150, 300)
workspace.painting.fill_selection(false)
foreground(workspace, 0.0, 0.0, 1.0)
workspace.canvas.select_rectangle(150, 0, 50, 300)
workspace.painting.fill_selection(false)
foreground(workspace, 1.0, 0.0, 0.0)
workspace.canvas.select_rectangle(200, 0, 200, 300)
workspace.painting.fill_selection(false)
workspace.canvas.select_rectangle(10, 10, 5, 5)
workspace.selection.grow_selection(true)
assert(workspace.canvas.undo_name() == "Grow" and workspace.canvas.selected_at(140, 290) == 255 and workspace.canvas.selected_at(300, 150) == 0)
workspace.selection.grow_selection(false)
assert(workspace.canvas.undo_name() == "Similar" and workspace.canvas.selected_at(300, 150) == 255 and workspace.canvas.selected_at(170, 150) == 0)
workspace.canvas.select_rectangle(50, 50, 100, 100)
workspace.selection.border_selection(8)
assert(workspace.canvas.undo_name() == "Border" and workspace.canvas.selected_at(100, 100) == 0 and workspace.canvas.selected_at(50, 100) > 0)
workspace.undo()
workspace.selection.smooth_selection(3)
assert(workspace.canvas.undo_name() == "Smooth" and workspace.canvas.selected_at(100, 100) == 255)
# The box moves the outline; Apply keeps it, the pixels stay.
workspace.canvas.select_rectangle(100, 100, 50, 50)
assert(workspace.transforms.begin_selection_transform())
let session = workspace.transforms.transform else trap("a transform is under way")
assert(session.outline and not session.floats())
session.box.dx = 60.0
workspace.transforms.preview_transform()
workspace.transforms.finish_transform(true)
assert(workspace.transforms.transform == none and workspace.canvas.undo_name() == "Transform Selection")
assert(workspace.canvas.selection_left() == 160 and workspace.canvas.selected_at(120, 120) == 0)
workspace.undo()
assert(workspace.canvas.selection_left() == 100)
# Cancelled, nothing changes.
assert(workspace.transforms.begin_selection_transform())
let again = workspace.transforms.transform else trap("a transform is under way")
again.box.dx = 30.0
workspace.transforms.preview_transform()
workspace.transforms.finish_transform(false)
assert(workspace.canvas.selection_left() == 100)
quick_masking()
## Quick Mask from the Q command, the rail's button marking it, the view's
## tint drawn, a fill on the mask becoming the selection.
func quick_masking() -> unit!:
let workspace = Workspace(300, 200)
let font = Font()
let actions = Actions()
let panels = Panels(workspace, actions, font)
workspace.canvas.bind_headless()
workspace.canvas.select_rectangle(20, 20, 50, 50)
actions.quick_mask.trigger()
assert(workspace.canvas.quick_mask() and workspace.canvas.undo_name() == "Quick Mask")
let view = CanvasView(workspace)
view.layout().set_size(300.0, 200.0)
let frame = Frame(window.Size(300.0, 200.0, 300.0, 200.0, 1.0))
view.draw(frame.target())
frame.close()
# A stroke on the mask is listed after Quick Mask, as Photoshop lists it.
panels.choose_tool("brush")
workspace.viewport.zoom = 1.0
workspace.viewport.offset_x = 0.0
workspace.viewport.offset_y = 0.0
press(view, 30.0, 30.0)
drag_to(view, 60.0, 60.0)
release(view, 60.0, 60.0)
let stroked = workspace.canvas.history_steps()
assert(workspace.canvas.history_step_name(stroked - 2) == "Quick Mask" and workspace.canvas.history_step_name(stroked - 1) == "Brush Stroke")
workspace.undo()
# White fills the whole mask (the selection does not clip meanwhile): all selected.
foreground(workspace, 1.0, 1.0, 1.0)
workspace.painting.fill_selection(false)
actions.quick_mask.trigger()
assert(not workspace.canvas.quick_mask() and workspace.canvas.undo_name() == "Exit Quick Mask")
assert(workspace.canvas.selected_at(200, 150) == 255 and workspace.canvas.layer_count() == 1)
workspace.undo()
# Undone, Exit Quick Mask brings the mode back with the selection it had.
assert(workspace.canvas.quick_mask() and workspace.canvas.selected_at(200, 150) == 0 and workspace.canvas.selected_at(30, 30) == 255)
magnetic_lassoing()
## The Magnetic Lasso from the rail's group, its header options, clicks
## along an edge closing round the white half, and the same from a script.
func magnetic_lassoing() -> unit!:
let workspace = Workspace(300, 200)
let font = Font()
let actions = Actions()
let panels = Panels(workspace, actions, font)
workspace.canvas.bind_headless()
foreground(workspace, 1.0, 1.0, 1.0)
workspace.canvas.select_rectangle(150, 0, 150, 200)
workspace.painting.fill_selection(false)
workspace.canvas.deselect()
workspace.choose_tool("magnetic")
assert(group_of("magnetic") == group_of("lasso"))
workspace.tools.selecting.magnetic_width = 12.0
let lasso = MagneticLasso()
lasso.click(workspace, 150.0, 5.0, false, false, 1.0, 6.0, 0)
lasso.hover(workspace, 144.0, 100.0)
assert(workspace.canvas.magnetic_count() > 50)
lasso.click(workspace, 150.0, 195.0, false, false, 1.0, 6.0, 1000)
lasso.click(workspace, 295.0, 195.0, false, false, 1.0, 6.0, 2000)
lasso.click(workspace, 295.0, 5.0, false, false, 1.0, 6.0, 3000)
lasso.close(workspace)
assert(workspace.canvas.undo_name() == "Magnetic Lasso" and workspace.canvas.selected_at(220, 100) == 255 and workspace.canvas.selected_at(100, 100) == 0)
# Backspace at the first anchor lets it go.
lasso.click(workspace, 20.0, 20.0, false, false, 0.25, 6.0, 9000)
assert(view_level(0.25) == 2 and workspace.canvas.magnetic_active())
lasso.take_back(workspace)
assert(not lasso.active and not workspace.canvas.magnetic_active())
let commands = ScriptCommands(workspace, actions)
workspace.canvas.deselect()
commands.apply(parse_line("@l2d magnetic_lasso\t10\t10\t57\t0\t150\t5\t150\t195\t295\t195\t295\t5") else trap("line"))
assert(workspace.canvas.undo_name() == "Magnetic Lasso" and workspace.canvas.selected_at(220, 100) == 255)
panels.close()
selecting_and_masking()
## Select and Mask from its command: the view's box previewed, the views, OK
## as one step, Cancel putting it back, and the same from a script.
func selecting_and_masking() -> unit!:
let workspace = Workspace(300, 200)
let font = Font()
let actions = Actions()
let panels = Panels(workspace, actions, font)
workspace.canvas.bind_headless()
workspace.canvas.select_rectangle(100, 50, 100, 100)
actions.update(workspace)
assert(actions.select_mask.is_enabled())
actions.select_mask.trigger()
let dialog = panels.select_mask
assert(workspace.canvas.refining())
dialog.feather.set_value(6.0)
dialog.changed(6.0)
dialog.session.poll()
# The setting is recorded; the selection stays as it was until OK.
assert(workspace.canvas.selected_at(98, 100) == 0 and workspace.canvas.refine_preview_level() == -1)
workspace.selection.set_refine_view(3)
# A frame of the view works it out at the view's resolution, in the background.
let view = CanvasView(workspace)
view.layout().set_size(300.0, 200.0)
let frame = Frame(window.Size(300.0, 200.0, 300.0, 200.0, 1.0))
view.draw(frame.target())
frame.close()
workspace.canvas.wait_previews()
let second = Frame(window.Size(300.0, 200.0, 300.0, 200.0, 1.0))
view.draw(second.target())
second.close()
assert(workspace.canvas.refine_preview_level() == 0)
# OK refines in the background; frames keep it once it is in.
dialog.apply()
var frames = 0
while workspace.canvas.refine_keeping() and frames < 1000000:
panels.show_opening()
frames += 1
assert(not workspace.canvas.refining() and workspace.canvas.undo_name() == "Select and Mask" and workspace.canvas.selected_at(98, 100) > 0)
workspace.undo()
assert(workspace.canvas.selected_at(98, 100) == 0)
actions.select_mask.trigger()
dialog.smooth.set_value(10.0)
dialog.changed(10.0)
dialog.session.poll()
dialog.cancel()
assert(not workspace.canvas.refining() and workspace.canvas.selected_at(100, 50) == 255)
let commands = ScriptCommands(workspace, actions)
commands.apply(parse_line("@l2d select_and_mask\t0\t0\t4\t0\t0") else trap("line"))
assert(workspace.canvas.undo_name() == "Select and Mask" and workspace.canvas.selected_at(98, 100) > 0)
panels.close()
func wheel_press(wheel: ColorWheel, x: float, y: float, shift: bool = false) -> unit!:
wheel.event(Event(kind = EventKind.pointer_down, x = x, y = y, button = 0, inside = true, shift = shift))
func wheel_drag(wheel: ColorWheel, x: float, y: float, shift: bool = false) -> unit!:
wheel.event(Event(kind = EventKind.pointer_moved, x = x, y = y, inside = true, shift = shift))
func wheel_release(wheel: ColorWheel, x: float, y: float) -> unit!:
wheel.event(Event(kind = EventKind.pointer_up, x = x, y = y, button = 0, inside = true))
## Whether the foreground is the encoded sRGB (red, green, blue), near enough.
func foreground_is(workspace: Workspace, red: float, green: float, blue: float) -> bool:
return encodes_as(workspace.colors.red, red) and encodes_as(workspace.colors.green, green) and encodes_as(workspace.colors.blue, blue)
func encodes_as(linear: float, encoded: float) -> bool:
return math.abs(transfer.srgb_encode(linear) - encoded) < 0.002
## The Color panel's picker in each mode: presses and drags on the ring and on
## the triangle, square and disc set the foreground every step; a drag keeps
## the part it began on; Shift drags fine; greys and black keep the hue and
## saturation; the mode and the sliders' model are kept in the settings.
pub func color_panel_modes() -> unit!:
let workspace = Workspace(64, 64)
let panel = ColorPanel(workspace, Kit(Font()), "triangle", "rgb")
var kept = ""
panel.saved = func (mode: str, model: str) -> unit!:
kept = mode + " " + model
let app = Application(panel.panel)
app.layout(260.0, 560.0)
let wheel = panel.wheel
# Stretched across the panel, then square to that width on the next arrangement.
wheel.layout().invalidate()
app.layout(260.0, 560.0)
let bounds = wheel.layout().bounds()
assert(bounds.width == bounds.height and bounds.width > 200.0)
workspace.colors.set_color(1.0, 0.0, 0.0)
panel.show()
assert(panel.state().hue == 0.0 and math.abs(panel.state().saturation - 1.0) < 1e-9 and math.abs(panel.state().brightness - 1.0) < 1e-9)
# Hue runs clockwise, as GIMP's and Krita's: the ring's top is 270°, violet.
let top_y = bounds.height * 0.5 - 0.94 * (bounds.width * 0.5 - 3.0)
wheel_press(wheel, bounds.width * 0.5, top_y)
wheel_release(wheel, bounds.width * 0.5, top_y)
assert(math.abs(panel.state().hue - 270.0) < 0.5 and foreground_is(workspace, 0.5, 0.0, 1.0))
assert(wheel.ring_point_y(90.0) > bounds.height * 0.5)
# Triangle: the ring at 120° is green, at once.
wheel_press(wheel, wheel.ring_point_x(120.0), wheel.ring_point_y(120.0))
assert(foreground_is(workspace, 0.0, 1.0, 0.0))
# Dragged on inside the ring's hole, the drag still turns only the hue.
let x = wheel.field_point_x(0.5, 0.5)
let y = wheel.field_point_y(0.5, 0.5)
let cx = bounds.width * 0.5
let cy = bounds.height * 0.5
wheel_drag(wheel, cx + (wheel.ring_point_x(240.0) - cx) * 0.3, cy + (wheel.ring_point_y(240.0) - cy) * 0.3)
assert(math.abs(panel.state().hue - 240.0) < 0.01 and panel.state().saturation == 1.0 and foreground_is(workspace, 0.0, 0.0, 1.0))
wheel_release(wheel, x, y)
wheel.set_hsb(240.0, 1.0, 1.0)
panel.state().assign(240.0, 1.0, 1.0)
# The triangle: its middle is half saturation, half brightness of the hue.
wheel_press(wheel, wheel.field_point_x(0.5, 0.5), wheel.field_point_y(0.5, 0.5))
assert(math.abs(panel.state().saturation - 0.5) < 0.01 and math.abs(panel.state().brightness - 0.5) < 0.01 and panel.state().hue == 240.0)
assert(foreground_is(workspace, 0.25, 0.25, 0.5))
# Dragged to the white corner, then past the black one: the hue and saturation stay.
wheel_drag(wheel, wheel.field_point_x(0.0, 1.0), wheel.field_point_y(0.0, 1.0))
assert(foreground_is(workspace, 1.0, 1.0, 1.0) and panel.state().hue == 240.0)
let saturation = panel.state().saturation
let black_x = wheel.field_point_x(0.0, 0.0)
let black_y = wheel.field_point_y(0.0, 0.0)
wheel_drag(wheel, black_x + (black_x - bounds.width * 0.5) * 0.3, black_y + (black_y - bounds.height * 0.5) * 0.3)
assert(foreground_is(workspace, 0.0, 0.0, 0.0) and panel.state().hue == 240.0 and panel.state().saturation == saturation)
wheel_release(wheel, black_x, black_y)
# A grey or black set elsewhere keeps the hue (the panel refreshes after).
panel.state().assign(240.0, 0.8, 0.8)
workspace.colors.set_color(0.2, 0.2, 0.2)
panel.show()
assert(panel.state().hue == 240.0 and panel.state().saturation == 0.0)
workspace.colors.set_color(0.0, 0.0, 0.0)
panel.show()
assert(panel.state().hue == 240.0 and panel.state().saturation == 0.0 and panel.state().brightness == 0.0)
# Shift drags a quarter as far: 40 points right moves the aim 10.
panel.state().assign(240.0, 1.0, 1.0)
panel.show()
workspace.colors.set_color(0.0, 0.0, 1.0)
panel.choose_mode(1)
app.layout(260.0, 560.0)
assert(kept == "square rgb")
let sx = wheel.field_point_x(0.5, 0.5)
let sy = wheel.field_point_y(0.5, 0.5)
let span = wheel.field_point_x(1.0, 0.5) - wheel.field_point_x(0.0, 0.5)
wheel_press(wheel, sx, sy, shift = true)
wheel_drag(wheel, sx + 40.0, sy, shift = true)
assert(math.abs(panel.state().saturation - (0.5 + 10.0 / span)) < 0.001 and math.abs(panel.state().brightness - 0.5) < 0.001)
wheel_drag(wheel, wheel.field_point_x(1.0, 1.0) + 30.0, wheel.field_point_y(1.0, 1.0) - 30.0)
assert(foreground_is(workspace, 0.0, 0.0, 1.0))
wheel_release(wheel, sx, sy)
# The square's ring: yellow; the arrow keys and the wheel turn the hue.
wheel_press(wheel, wheel.ring_point_x(60.0), wheel.ring_point_y(60.0))
wheel_release(wheel, wheel.ring_point_x(60.0), wheel.ring_point_y(60.0))
assert(foreground_is(workspace, 1.0, 1.0, 0.0))
wheel.event(Event(kind = EventKind.key_down, key = Key.right))
assert(math.abs(panel.state().hue - 61.0) < 0.01)
wheel.event(Event(kind = EventKind.scroll, scroll_y = -1.0, inside = true))
assert(math.abs(panel.state().hue - 59.0) < 0.01)
# The wheel: hue around, saturation out; brightness on the strip under it.
panel.choose_mode(2)
app.layout(260.0, 560.0)
assert(kept == "wheel rgb" and panel.strip.layout().bounds().width > 200.0)
wheel_press(wheel, wheel.ring_point_x(300.0), wheel.ring_point_y(300.0))
wheel_release(wheel, wheel.ring_point_x(300.0), wheel.ring_point_y(300.0))
assert(math.abs(panel.state().hue - 300.0) < 0.01 and math.abs(panel.state().saturation - 0.5) < 0.01 and foreground_is(workspace, 1.0, 0.5, 1.0))
let strip = panel.strip.layout().bounds()
panel.strip.event(Event(kind = EventKind.pointer_down, x = strip.width * 0.25, y = 5.0, button = 0, inside = true))
panel.strip.event(Event(kind = EventKind.pointer_up, x = strip.width * 0.25, y = 5.0, button = 0, inside = true))
assert(math.abs(panel.state().brightness - 0.25) < 0.001 and foreground_is(workspace, 0.25, 0.125, 0.25))
# Sliders: HSB and Lab set the color; a Lab grey keeps the hue.
panel.choose_mode(3)
panel.sliders.choose(1)
assert(kept == "sliders hsb")
panel.sliders.set_channel(2, 100.0)
assert(foreground_is(workspace, 1.0, 0.5, 1.0))
# A grey from HSB, then made lighter in Lab: still hue 300.
panel.sliders.set_channel(1, 0.0)
assert(panel.state().hue == 300.0 and panel.state().saturation == 0.0)
panel.sliders.choose(2)
panel.sliders.set_channel(0, 70.0)
assert(panel.state().hue == 300.0 and panel.state().saturation < 0.01 and panel.state().brightness > 0.6)
panel.sliders.choose(3)
panel.sliders.set_channel(0, 100.0)
assert(foreground_is(workspace, 0.0, 0.0, 0.0) and panel.state().hue == 300.0)
let reread = Settings(Settings().serialize())
assert(reread.color_mode == "triangle" and reread.color_model == "rgb")
app.close()