ColorExtractor

class ColorExtractor(rootView: View)

Samples background and foreground colors from a composable's rendered bounds.

Strategy:

  • Background: 8 sample points — 4 corners + 4 edge midpoints. Nodes at the edge of a container have consistent background color there; deduplication returns only distinct candidates, so a solid background yields a single-element list.

  • Text/foreground: single pixel at the node's geometric center.

Rect coordinates must be root-relative, matching Compose semantics bounds. rootView must be laid out with non-zero dimensions before calling extractColors.

Note: drawToBitmap captures a software copy of the entire view on every call. When sampling multiple nodes, prefer creating the bitmap once externally and calling sampleEdges/sampleCenter directly with the shared Bitmap.

Parameters

rootView

View whose rendered pixels should be sampled.

Constructors

Link copied to clipboard
constructor(rootView: View)

Types

Link copied to clipboard
data class Result(val backgroundColors: List<Color>, val textColor: Color?)

Colors sampled from a node's rendered bounds.

Functions

Link copied to clipboard

Captures rootView and samples colors inside bounds.

Link copied to clipboard
fun sampleCenter(bitmap: Bitmap, bounds: Rect): Color?

Samples the center pixel from bounds in bitmap.

Link copied to clipboard
fun sampleEdges(bitmap: Bitmap, bounds: Rect): List<Color>

Samples corners and edge midpoints from bounds in bitmap.