dymokomi/luce-ui
Source of release 0.19.4
0.19.4 / src / widgets / histogram.lucb
## The Histogram widget: how a picture's values spread, as photo editors draw it
## above their adjustments: luma as a grey area with red, green and blue laid
## over it, each normalized to the tallest bin away from the ends (so a clipped
## sky does not flatten the rest), heights on a square-root scale. A mark in a
## corner lights when more than half a percent of the pixels clip there.
from luce_gpu import gpu
import interop
from luce_std import math
import memory
import ownership
import layout
import result
from layout import Widget, Layout, Size, Constraints, Event
from painter import Painter
from geometry import Point
## Bins per channel; a histogram takes four channels' worth: red, green, blue, luma.
pub let histogram_bins: usize = 256
pub struct Histogram: Widget:
let box: interop.Reference[Layout]
var bins: i64[1024]
var total: i64
pub func init() -> !:
let box = try layout.make_layout()
errdefer box.release()
try box.value().set_limits(Size(160.0, 70.0), Size(16384.0, 160.0))
try box.value().set_flex(1.0, 0.0)
var empty: i64[1024]
self.box = box
self.bins = empty
self.total = 0
pub func layout() -> interop.Reference[Layout]:
return self.box.clone()
## Show `bins`: 256 counts each of red, green, blue and luma, over 8-bit
## values. Anything else shows an empty histogram.
pub func set_bins(bins: const i64[]):
self.total = 0
if bins.length != histogram_bins * 4:
self.box.value().invalidate()
return
for at in 0..<bins.length:
self.bins[at] = math_max(bins[at], 0)
for at in 0..<histogram_bins:
self.total += self.bins[768 + at]
self.box.value().invalidate()
## Show nothing.
pub func clear():
self.total = 0
self.box.value().invalidate()
pub func measure(available: Constraints) -> interop.Outcome[Size]:
return result.success[Size](Size(256.0, 96.0))
pub func draw(target: interop.View[gpu.RenderTarget]) -> interop.Outcome[unit]:
self.paint(target) catch failure:
return result.failure[unit](failure.code, failure.message)
return result.success[unit](())
func paint(target: interop.View[gpu.RenderTarget]) -> !:
var painter = try Painter(target)
defer painter.close()
let size = try target.get().size()
let theme = self.box.value().theme()
try painter.rounded_rectangle(gpu.Rect(width = size.width, height = size.height), theme.sunken, theme.control_radius)
if self.total <= 0:
return
let inner = gpu.Rect(4.0, 4.0, size.width - 8.0, size.height - 8.0)
try self.area(&painter, 3, inner, gpu.Color(0.35, 0.35, 0.35), 1.0)
try self.area(&painter, 0, inner, gpu.Color(0.8, 0.05, 0.03), 0.5)
try self.area(&painter, 1, inner, gpu.Color(0.05, 0.6, 0.06), 0.5)
try self.area(&painter, 2, inner, gpu.Color(0.04, 0.12, 0.85), 0.5)
# Clipping: shadows at the left, highlights at the right.
let limit = self.total // 200 + 1
if self.bins[768] > limit:
try painter.triangle(Point(inner.x, inner.y), Point(inner.x + 8.0, inner.y), Point(inner.x, inner.y + 8.0), theme.accent)
if self.bins[1023] > limit:
let right = inner.x + inner.width
try painter.triangle(Point(right, inner.y), Point(right - 8.0, inner.y), Point(right, inner.y + 8.0), theme.accent)
## Channel `channel` (0 red, 1 green, 2 blue, 3 luma) as a filled area.
func area(painter: Painter*, channel: usize, room: gpu.Rect, color: gpu.Color, opacity: f64) -> !:
let base = channel * histogram_bins
var tallest: i64 = 1
for at in 1..<histogram_bins - 1:
tallest = math_max(tallest, self.bins[base + at])
let scale = 1.0 / math.sqrt((f64)tallest)
let step = room.width / (f64)histogram_bins
let bottom = room.y + room.height
var points = try new Point[histogram_bins * 6] --- in memory.heap
defer free(points) in memory.heap
var colors = try new gpu.Color[histogram_bins * 6] --- in memory.heap
defer free(colors) in memory.heap
for at in 0..<histogram_bins:
# A little smoothing: a small sample's bins are noisy.
let before = self.bins[base + at - 1] if at > 0 else self.bins[base + at]
let after = self.bins[base + at + 1] if at + 1 < histogram_bins else self.bins[base + at]
let count = (f64)(before + 2 * self.bins[base + at] + after) / 4.0
let height = math.min(1.0, math.sqrt(count) * scale) * room.height
let left = room.x + (f64)at * step
let right = left + step
let top = bottom - height
let corners: Point[6] = [Point(left, bottom), Point(right, bottom), Point(right, top), Point(left, bottom), Point(right, top), Point(left, top)]
for corner in 0..<(usize)6:
points[at * 6 + corner] = corners[corner]
colors[at * 6 + corner] = color
try painter.shaded_mesh(points, colors, opacity)
pub func event(event: Event) -> interop.Outcome[unit]:
return result.success[unit](())
pub func close():
self.box.release()
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
self.box.trace(visit, context)
func math_max(a: i64, b: i64) -> i64:
return a if a > b else b
pub let histogram_type: interop.Type[Histogram] = interop.Type[Histogram]("Histogram", Histogram.close, Histogram.trace, closeable = true, main_thread = true)