@@ -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,49 @@ function create_contrast_popup(canvas, enabled, hists, clim)
733733 end
734734end
735735
736+ function dummy_histsig (clim:: Observable{CLim{T}} ; floor= nothing ) 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+ rnglo = floor === nothing ? lo - span/ 2 : max (Th (floor), lo - span/ 2 )
745+ rng = LinRange (rnglo, hi + span/ 2 , 300 )
746+ Observable (Histogram (rng, zeros (Int, 299 ), :right , false ))
747+ end
748+
749+ dummy_histsigs (clim:: Observable{CLim{T}} ; floor= nothing ) where {T<: GrayLike } =
750+ [dummy_histsig (clim; floor)]
751+
752+ dummy_histsigs (clim:: Observable{CLim{T}} ; floor= nothing ) where {T<: AbstractRGB } =
753+ [dummy_histsig (map (x-> channel_clim (red, x), clim); floor),
754+ dummy_histsig (map (x-> channel_clim (green, x), clim); floor),
755+ dummy_histsig (map (x-> channel_clim (blue, x), clim); floor)]
756+
757+ """
758+ setup_contrast_popup!(canvas, clim; img=nothing)
759+
760+ Set up a right-click context menu on `canvas` that allows interactive
761+ adjustment of `clim` via a contrast GUI window. If `img` is an
762+ `Observable` wrapping an array compatible with `clim`, a histogram of
763+ pixel intensities is computed and shown whenever the GUI is opened.
764+ Without `img`, the GUI shows sliders only (no histogram).
765+
766+ This is a lower-level complement to [`imshow`](@ref), useful when
767+ contrast is managed internally by the image type but an interactive
768+ right-click popup is still desired.
769+ """
770+ function setup_contrast_popup! (canvas, clim:: Observable{CLim{T}} ;
771+ img:: Union{Nothing,Observable} = nothing ,
772+ floor= nothing ) where T
773+ enabled = Observable (false )
774+ histsigs = img === nothing ? dummy_histsigs (clim; floor) : histsignals (enabled, img, clim)
775+ push! (canvas. preserved, create_contrast_popup (canvas, enabled, histsigs, clim))
776+ return canvas
777+ end
778+
736779function map_image_roi (@nospecialize (img), zr:: Observable{ZoomRegion{T}} , slices... ) where T
737780 map (zr, slices... ) do r, s...
738781 cv = r. currentview
0 commit comments