@@ -18,7 +18,7 @@ using Compat # for @constprop :none
1818export AnnotationText, AnnotationPoint, AnnotationPoints,
1919 AnnotationLine, AnnotationLines, AnnotationBox
2020export CLim, annotate!, annotations, canvasgrid, imshow, imshow!, imshow_gui, imlink,
21- roi, scalebar, slice2d
21+ roi, scalebar, setup_contrast_popup!, slice2d
2222
2323const AbstractGray{T} = Color{T,1 }
2424const GrayLike = Union{AbstractGray,Number}
@@ -733,6 +733,47 @@ function create_contrast_popup(canvas, enabled, hists, clim)
733733 end
734734end
735735
736+ function dummy_histsig (clim:: Observable{CLim{T}} ) where {T<: GrayLike }
737+ Th = float (T)
738+ cl = clim[]
739+ lo, hi = Th (cl. min), Th (cl. max)
740+ if ! (lo < hi)
741+ lo, hi = zero (Th), one (Th)
742+ end
743+ span = hi - lo
744+ rng = LinRange (lo - span/ 2 , hi + span/ 2 , 300 )
745+ Observable (Histogram (rng, zeros (Int, 299 ), :right , false ))
746+ end
747+
748+ dummy_histsigs (clim:: Observable{CLim{T}} ) where {T<: GrayLike } =
749+ [dummy_histsig (clim)]
750+
751+ dummy_histsigs (clim:: Observable{CLim{T}} ) where {T<: AbstractRGB } =
752+ [dummy_histsig (map (x-> channel_clim (red, x), clim)),
753+ dummy_histsig (map (x-> channel_clim (green, x), clim)),
754+ dummy_histsig (map (x-> channel_clim (blue, x), clim))]
755+
756+ """
757+ setup_contrast_popup!(canvas, clim; img=nothing)
758+
759+ Set up a right-click context menu on `canvas` that allows interactive
760+ adjustment of `clim` via a contrast GUI window. If `img` is an
761+ `Observable` wrapping an array compatible with `clim`, a histogram of
762+ pixel intensities is computed and shown whenever the GUI is opened.
763+ Without `img`, the GUI shows sliders only (no histogram).
764+
765+ This is a lower-level complement to [`imshow`](@ref), useful when
766+ contrast is managed internally by the image type but an interactive
767+ right-click popup is still desired.
768+ """
769+ function setup_contrast_popup! (canvas, clim:: Observable{CLim{T}} ;
770+ img:: Union{Nothing,Observable} = nothing ) where T
771+ enabled = Observable (false )
772+ histsigs = img === nothing ? dummy_histsigs (clim) : histsignals (enabled, img, clim)
773+ push! (canvas. preserved, create_contrast_popup (canvas, enabled, histsigs, clim))
774+ return canvas
775+ end
776+
736777function map_image_roi (@nospecialize (img), zr:: Observable{ZoomRegion{T}} , slices... ) where T
737778 map (zr, slices... ) do r, s...
738779 cv = r. currentview
0 commit comments