luminanceAt method

double? luminanceAt(
  1. Rect globalRect
)

Average WCAG relative luminance behind globalRect (global/screen coordinates), read from the most recent backdrop capture.

Answers synchronously from the cached capture rather than awaiting a fresh toImage readback — the underlying content is captured at scroll rate (see GlassContentAwareScope.sampleInterval), so callers get a value that is at most one sample interval stale. Returns null if no GlassContentAwareContent is registered, no capture has completed yet, or globalRect doesn't overlap the sampled content region.

Also requests a fresh sample so repeated queries (e.g. from GlassContrastRule running every frame in a debug overlay) keep the capture warm even without any GlassContentAwareBrightness control registered.

Implementation

double? luminanceAt(Rect globalRect) {
  _luminanceQueryPending = true;
  requestSample();
  final rgba = _lastRgba;
  if (rgba == null) return null;
  final boundary = _contentKey?.currentContext?.findRenderObject();
  if (boundary is! RenderRepaintBoundary || !boundary.attached) return null;
  final origin = boundary.globalToLocal(globalRect.topLeft);
  final localRect =
      (origin & globalRect.size).intersect(Offset.zero & boundary.size);
  if (localRect.isEmpty) return null;
  final background = widget.backgroundColor ??
      (MediaQuery.maybePlatformBrightnessOf(context) == Brightness.dark
          ? const Color(0xFF000000)
          : const Color(0xFFFFFFFF));
  return averageLuminanceOfRegion(
    rgba: rgba,
    width: _lastWidth,
    height: _lastHeight,
    pixelRect: Rect.fromLTRB(
      localRect.left * _lastPixelRatio,
      localRect.top * _lastPixelRatio,
      localRect.right * _lastPixelRatio,
      localRect.bottom * _lastPixelRatio,
    ),
    background: background,
  );
}