dymokomi/luced-2d
Source of release 0.5.1
0.5.1 / src / document_panel.luc
## Image › Document Settings…: the dialog for the document's size, resolution,
## display unit, color profile and how its colors blend.
from luce_ui.ui import Widget, Font, Text, Button, HStack, Spacer, Size, Dialog, NumberField, Checkbox, Menu, Command
from interop import Connection
from workspace import Workspace
from luce_std import math
from units import unit_ids, unit_label, unit_suffix, pixels_per
## Image › Document Settings…: the document's size in the chosen unit, its
## resolution, the unit itself and its color profile, after Photoshop's Image
## Size. With Resample the pixels are scaled to the new size; without it the
## canvas grows or shrinks around the picture, centred. The profile is assigned,
## not converted: the pixels keep their numbers and are read in the new space.
## "Blend RGB Colors Using Gamma 1.0" is Photoshop's Color Settings option:
## colors mix in linear light instead of as their sRGB-encoded values.
pub class DocumentSettings:
pub let dialog: Dialog
let workspace: Workspace
let font: Font
let width_field: NumberField
let height_field: NumberField
let resolution_field: NumberField
let units_menu: Menu
let profile_menu: Menu
let resample: Checkbox
pub let gamma: Checkbox
let connections: list[Connection]
## The choices while the dialog is open.
var units: str = "px"
var profile: str = "sRGB"
pub var applied: func() -> unit!? = none
pub func init(self, workspace: Workspace, font: Font) -> unit!:
self.workspace = workspace
self.font = font
self.connections = []
self.width_field = NumberField(1.0, 1.0, 65536.0, 1.0, font = font, cells = 11.0)
self.height_field = NumberField(1.0, 1.0, 65536.0, 1.0, font = font, cells = 11.0)
self.resolution_field = NumberField(72.0, 1.0, 9600.0, 1.0, suffix = " px/in", font = font, cells = 11.0)
var unit_commands: list[Command] = []
var index = 0
for unit_name in unit_ids():
let chosen = unit_name
let command = Command(unit_label(unit_name), id = f"document_units_{unit_name}")
self.connections.append(command.on_trigger(func () -> unit!:
self.choose_units(chosen)))
unit_commands.append(command)
index += 1
self.units_menu = Menu("Pixels ▾", unit_commands, font = font)
var profile_commands: list[Command] = []
for name in profile_names():
let chosen = name
let command = Command(name, id = "document_profile_" + name)
self.connections.append(command.on_trigger(func () -> unit!:
self.profile = chosen
self.profile_menu.set_text(chosen + " ▾")))
profile_commands.append(command)
self.profile_menu = Menu("sRGB ▾", profile_commands, font = font)
self.resample = Checkbox("Resample pixels", true, font = font)
self.gamma = Checkbox("Blend RGB Colors Using Gamma 1.0", false, font = font)
let cancel = Button("Cancel", font = font)
let apply = Button("Apply", font = font)
self.connections.append(cancel.on_click(self.cancel))
self.connections.append(apply.on_click(self.apply))
self.dialog = Dialog([Text("Document Settings", font = font),
self.row("Width", self.width_field), self.row("Height", self.height_field),
self.row("Units", self.units_menu), self.row("Resolution", self.resolution_field),
self.row("Color profile", self.profile_menu), self.row("", self.resample),
Text("Unchecked, the canvas resizes around the picture.", font = font, muted = true),
self.row("Blending", self.gamma),
Text("Checked, colors mix in linear light rather than as sRGB values.", font = font, muted = true),
HStack([Spacer(), cancel, apply], spacing = 8.0)], width = 460.0, spacing = 10.0)
func row(self, label: str, control: Widget) -> HStack!:
let caption = Text(label, font = self.font, muted = true)
caption.layout().set_limits(Size(120.0, 0.0), Size(120.0, 16384.0))
return HStack([caption, control, Spacer()], spacing = 8.0)
pub func is_open(self) -> bool:
return self.dialog.is_open()
## Open on the current document, in the view's unit.
pub func open(self) -> unit!:
let canvas = self.workspace.canvas
self.profile = canvas.profile()
self.profile_menu.set_text(self.profile + " ▾")
self.resolution_field.set_value(canvas.resolution())
self.gamma.set_checked(canvas.linear_blend())
self.choose_units(self.workspace.viewport.units)
self.dialog.open()
## Show the size in `units`, converting what the fields hold.
func choose_units(self, units: str) -> unit!:
self.units = units
self.units_menu.set_text(unit_label(units) + " ▾")
let per_unit = self.pixels_per_unit()
let decimals = 0 if units == "px" else 2
let step = 1.0 if units == "px" else 0.01
for field in [self.width_field, self.height_field]:
field.set_decimals(decimals)
field.set_step(step)
self.width_field.set_range(1.0 / per_unit, 65536.0 / per_unit)
self.height_field.set_range(1.0 / per_unit, 65536.0 / per_unit)
self.width_field.set_value(float(self.workspace.canvas.width()) / per_unit)
self.height_field.set_value(float(self.workspace.canvas.height()) / per_unit)
let suffix = unit_suffix(units)
self.width_field.set_suffix(suffix)
self.height_field.set_suffix(suffix)
func pixels_per_unit(self) -> float:
return pixels_per(self.units, self.resolution_field.value())
## Apply: the resolution and profile, then the size, resampled or not.
pub func cancel(self) -> unit!:
self.dialog.hide()
pub func apply(self) -> unit!:
let canvas = self.workspace.canvas
canvas.set_resolution(self.resolution_field.value())
canvas.set_profile(self.profile)
if self.gamma.is_checked() != canvas.linear_blend():
self.workspace.image.set_linear_blend(self.gamma.is_checked())
let per_unit = self.pixels_per_unit()
let width = int(math.floor(self.width_field.value() * per_unit + 0.5))
let height = int(math.floor(self.height_field.value() * per_unit + 0.5))
let old_width = self.workspace.canvas.width()
let old_height = self.workspace.canvas.height()
if width != old_width or height != old_height:
if self.resample.is_checked():
self.workspace.image.resize_image(width, height)
else:
self.workspace.image.resize_canvas(width, height, (width - old_width) // 2, (height - old_height) // 2)
self.workspace.viewport.units = self.units
self.workspace.touch()
self.dialog.hide()
if let done = self.applied:
done()
pub func close(self):
for connection in self.connections:
connection.disconnect()
self.connections.clear()
## The profiles a document can be in, as luce-color names its spaces.
pub func profile_names() -> list[str]:
return ["sRGB", "Display P3", "Adobe RGB (1998)", "Rec. 2020", "ProPhoto RGB", "ACEScg", "Linear sRGB"]