using Chronos.App.Infrastructure;
using Chronos.Core.Advice;
using Chronos.Core.Imaging.Color;
using Chronos.Core.Localization;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Ui.Visuals;
namespace Chronos.App.Editor;
/// The measuring tools of the Exposure tab (BRIEF §12.3 "luminance/waveform charts").
public enum ExposureChart
{
/// Measured luminance against the target curve, sky region and exposure steps.
Luminance,
/// Frame-to-frame luminance change before and after the correction.
Flicker,
/// Luma waveform of the corrected frame on screen.
Waveform,
}
///
/// Exposure (BRIEF §12.3): deflicker, regions, holy-grail transitions, white balance and the look. The luminance chart shows the
/// measured luminance (Sun) against the target curve the corrections produce (Accent), the sky region (dashed) and the
/// exposure steps as received; the flicker chart the frame-to-frame change before and after correction; the waveform
/// the luma distribution of the corrected frame on screen.
///
public sealed class ExposureTab : EditorTab
{
private IReadOnlyList _series = [];
private IReadOnlyList _legend = [];
private IReadOnlyList _markers = [];
private string? _summary;
private ExposureChart _chartMode;
public ExposureTab(EditorViewModel editor)
: base(editor, "Exposure", AdvisorSection.Exposure)
{
var s = editor.Session;
// The same rule as the Edit path's (EditorSettings.Applies): a row shown n/a is never asked for.
string? NoDeflicker(ProjectSettings p) =>
EditorSettings.Applies(EditorSettings.DeflickerWindow, p, s.Content.Deep) ? null : Loc.L("Tooltips.Editor.NotApplicable.DeflickerOff");
string? NoHolyGrail(ProjectSettings p) =>
EditorSettings.Applies(EditorSettings.TransitionLength, p, s.Content.Deep) ? null : Loc.L("Tooltips.Editor.NotApplicable.HolyGrailOff");
Rows =
[
new AutoSwitchRow(s, EditorSettings.Deflicker, "Editor.Exposure.Deflicker", "Tooltips.Editor.Exposure.Deflicker"),
new AutoNumberRow(s, EditorSettings.DeflickerWindow, "Editor.Exposure.Window", "Tooltips.Editor.Exposure.Window", 5, 151, 2, "0", NoDeflicker),
new AutoNumberRow(s, EditorSettings.DeflickerStrength, "Editor.Exposure.Strength", "Tooltips.Editor.Exposure.Strength", 0, 1, 0.05, "0.00", NoDeflicker),
new AutoChoiceRow(s, EditorSettings.Regions, "Editor.Exposure.Regions", "Tooltips.Editor.Exposure.Regions",
[
new Choice(RegionMode.Off, "Editor.Value.Off"), new Choice(RegionMode.Auto, "Editor.Regions.Auto"),
new Choice(RegionMode.Horizon, "Editor.Regions.Horizon"), new Choice(RegionMode.Luminance, "Editor.Regions.Luminance"),
], NoDeflicker),
new AutoSwitchRow(s, EditorSettings.HolyGrail, "Editor.Exposure.HolyGrail", "Tooltips.Editor.Exposure.HolyGrail"),
new AutoNumberRow(s, EditorSettings.TransitionLength, "Editor.Exposure.Transitions", "Tooltips.Editor.Exposure.Transitions", 0, 120, 1, "0", NoHolyGrail),
new AutoSwitchRow(s, EditorSettings.WhiteBalance, "Editor.Exposure.WhiteBalance", "Tooltips.Editor.Exposure.WhiteBalance"),
new InfoRow("Editor.Exposure.Temperature", "Tooltips.Editor.Exposure.Temperature", Temperature),
new NumberRow(s, "Editor.Exposure.Offset", "Tooltips.Editor.Exposure.Offset", -3, 3, 0.1, p => p.Exposure.ExposureOffsetEv,
(p, v) => p with { Exposure = p.Exposure with { ExposureOffsetEv = v } }, v => Loc.L("Editor.Value.Ev", EditorText.Ev(v))),
.. LookRows(s),
];
Refresh();
BuildChart();
}
///
/// The Look group at the end of the step (owner review 1, R6): the filter (None, Mono, Sepia, Warm, Cool, Vivid, Muted,
/// Fade) and its strength, "n/a" without a filter. The look is a project setting outside Exposure, so moving these never
/// measures again ().
///
public static IReadOnlyList LookRows(EditorSession session)
{
ArgumentNullException.ThrowIfNull(session);
string? NoLook(ProjectSettings p) => HasFilter(p) ? null : Loc.L("Tooltips.Editor.NotApplicable.NoLook");
return
[
new GroupRow("Editor.Look.Group", "Tooltips.Editor.Look.Group"),
new ChoiceRow(session, "Editor.Look.Filter", "Tooltips.Editor.Look.Filter",
[.. LookFilter.Names.Select(n => new Choice(n, "Editor.Look." + char.ToUpperInvariant(n[0]) + n[1..]))],
p => p.Look?.Filter ?? LookFilter.None, (p, v) => WithLook(p, (string)v, p.Look?.Strength)),
new NumberRow(session, "Editor.Look.Strength", "Tooltips.Editor.Look.Strength", MinimumStrength, 1, 0.01, p => HasFilter(p) ? p.Look!.Strength ?? 1 : 1,
(p, v) => WithLook(p, p.Look?.Filter, double.Round(v, 2)), v => Formatting.Percent(v), NoLook),
];
}
///
/// The Strength row's lowest value: None is the one way to a look that changes nothing, so the editor never stores a
/// filter at 0 % (an identity look that would still change the project's text and hash).
///
public const double MinimumStrength = 0.01;
///
/// with the look at : none when the filter
/// is None or the strength 0 (so a project without a look keeps settings format 1 and its text: the editor writes
/// Look = null whenever the look is the identity), the strength omitted at 100 %. Values of a newer Chronos
/// inside the look are kept.
///
public static ProjectSettings WithLook(ProjectSettings project, string? filter, double? strength)
{
ArgumentNullException.ThrowIfNull(project);
if (filter is null || filter == LookFilter.None || strength is <= 0) return project with { Look = null };
var look = (project.Look ?? new LookSettings()) with { Filter = filter, Strength = strength is >= 1 ? null : strength };
return project with { Look = look };
}
private static bool HasFilter(ProjectSettings project) => project.Look?.Filter is { } filter && filter != LookFilter.None && LookFilter.IsKnown(filter);
/// Luminance, flicker or waveform (the switch beside the chart).
public ExposureChart ChartMode
{
get => _chartMode;
set
{
if (!Set(ref _chartMode, value)) return;
OnPropertyChanged(nameof(IsLuminance));
OnPropertyChanged(nameof(IsFlicker));
OnPropertyChanged(nameof(IsWaveform));
BuildChart();
Editor.OnWaveformNeeded();
}
}
// The switch's radio buttons: checking one chooses its chart; the group's unchecking of the others is ignored.
public bool IsLuminance { get => _chartMode == ExposureChart.Luminance; set => Choose(value, ExposureChart.Luminance); }
public bool IsFlicker { get => _chartMode == ExposureChart.Flicker; set => Choose(value, ExposureChart.Flicker); }
public bool IsWaveform { get => _chartMode == ExposureChart.Waveform; set => Choose(value, ExposureChart.Waveform); }
/// "luma of frame 0413".
public string WaveformTitle => Loc.L("Editor.Chart.WaveformOf", (Editor.CurrentIndex + 1).ToString("D4", Loc.Culture));
public IReadOnlyList Series { get => _series; private set => Set(ref _series, value); }
public IReadOnlyList Legend { get => _legend; private set => Set(ref _legend, value); }
public IReadOnlyList Markers { get => _markers; private set => Set(ref _markers, value); }
/// "flicker −67 % · max step 0.21 EV".
public string? Summary { get => _summary; private set => Set(ref _summary, value); }
public Func YFormatter => _chartMode == ExposureChart.Flicker ? v => Loc.L("Editor.Value.Ev", EditorText.Ev(v)) : v => v.ToString("0.0", Loc.Culture);
public override void OnContentChanged()
{
base.OnContentChanged();
BuildChart();
}
public override void OnCorrectionsChanged() => BuildChart();
public override void OnCurrentFrameChanged() => OnPropertyChanged(nameof(WaveformTitle));
private void Choose(bool chosen, ExposureChart chart)
{
if (chosen) ChartMode = chart;
}
private (string?, string?) Temperature()
{
if (Editor.Session.Content.Deep is not { } deep) return (null, Loc.L("Tooltips.Editor.NotApplicable.NeedsAnalysis"));
if (deep.WhiteBalance is { KelvinLow: { } low, KelvinHigh: { } high })
return (Loc.L("Editor.Value.Kelvin", RoundToTens(low).ToString("N0", Loc.Culture), RoundToTens(high).ToString("N0", Loc.Culture)), null);
return (null, Loc.L("Tooltips.Editor.NotApplicable.Kelvin"));
}
/// Kelvin shown to the nearest ten (Math.Round takes no negative digit count).
internal static double RoundToTens(double kelvin) => double.Round(kelvin / 10) * 10;
private void BuildChart()
{
var content = Editor.Session.Content;
var n = content.Count;
var corrections = Editor.Corrections is { } c && c.FrameCount == n ? c : null;
double[] measured = corrections?.MeasuredLogLuminance is { } m && m.Any(double.IsFinite)
? m
: content.Deep is { } deep ? [.. deep.Frames.LogLuminance.Select(v => v ?? double.NaN)] : [.. content.Frames.Select(f => f.MeanLogLuminance ?? double.NaN)];
var markers = EditorViewModel.StepMarkers(content);
var series = new List();
var legend = new List();
if (_chartMode == ExposureChart.Flicker)
{
series.Add(new ChartSeries(Loc.L("Editor.Chart.FlickerBefore"), Steps(measured), "Brush.Sun"));
legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.FlickerBefore"), "Brush.Sun"));
if (corrections is not null && measured.Any(double.IsFinite))
{
series.Add(new ChartSeries(Loc.L("Editor.Chart.FlickerAfter"), Steps(corrections.TargetLogLuminance()), "Brush.Accent") { Thickness = 2 });
legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.FlickerAfter"), "Brush.Accent"));
}
}
else
{
series.Add(new ChartSeries(Loc.L("Editor.Chart.Measured"), measured, "Brush.Sun"));
legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.Measured"), "Brush.Sun"));
if (corrections is not null && measured.Any(double.IsFinite))
{
series.Add(new ChartSeries(Loc.L("Editor.Chart.Target"), corrections.TargetLogLuminance(), "Brush.Accent") { Thickness = 2 });
legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.Target"), "Brush.Accent"));
}
if (content.Deep is { Regions: not null } withSky && withSky.Frames.SkyLogLuminance.Any(v => v is not null))
{
series.Add(new ChartSeries(Loc.L("Editor.Chart.Sky"), withSky.Frames.SkyLogLuminance.Select(v => v ?? double.NaN).ToArray(), "Brush.Region")
{ Dash = LineDash.Dashed });
legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.Sky"), "Brush.Region", LegendSwatch.DashedLine));
}
}
if (markers.Count > 0) legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.Steps"), "Brush.Sun", LegendSwatch.Triangle));
Series = series;
Legend = legend;
Markers = markers;
Summary = SummaryOf(corrections);
OnPropertyChanged(nameof(YFormatter));
}
/// Change from the previous usable frame, in EV (NaN where either is unusable).
private static double[] Steps(double[] logLuminance)
{
var steps = new double[logLuminance.Length];
var previous = double.NaN;
for (var i = 0; i < steps.Length; i++)
{
steps[i] = logLuminance[i] - previous;
if (double.IsFinite(logLuminance[i])) previous = logLuminance[i];
}
return steps;
}
private static string? SummaryOf(RenderCorrections? corrections)
{
if (corrections is null) return null;
var parts = new List();
if (corrections.Report.Deflicker is { FlickerBefore: > 0 } d)
parts.Add(Loc.L("Editor.Exposure.FlickerChange", double.Round((d.FlickerAfter / d.FlickerBefore - 1) * 100).ToString("+0;−0;0", Loc.Culture)));
if (corrections.Report.MaxTargetStepEv is { } step) parts.Add(Loc.L("Editor.Exposure.MaxStep", step.ToString("0.00", Loc.Culture)));
return parts.Count == 0 ? null : string.Join(Loc.Separator, parts);
}
}