Skip to content

Color scales

Read as Markdown

Import from @pibbl/core/viz. Each continuous provider takes id, domain, colors, and optional children. Domain and colors accept ordinary values or signals.

  • SequentialColorScale: two finite domain values and two colors. useSequentialColorScale(id): Signal<ResolvedSequentialColorScale>.
  • DivergingColorScale: three finite domain values (endpoint, center, endpoint) and three colors. useDivergingColorScale(id): Signal<ResolvedDivergingColorScale>.

A resolved mapping exposes frozen domain, colors, stops, and map(value: number): string. ColorScaleStop is {value: number, color: string}. Use actual resolved stops and map results to compose annotations with core Text and shapes. No inversion, generic scale hook, or automatic legend is provided.

Colors are required #RRGGBB strings (case-insensitive input); output is canonical lowercase #rrggbb. Interpolation is linear per encoded sRGB channel, rounded to the nearest integer, with exact authored endpoint and center colors. It is not linear-light or perceptually uniform interpolation. There is no default palette; choose endpoints that suit the data and provide labels or shapes in addition to color when distinctions matter. Named CSS colors, alpha, gradients, and other color spaces are deliberately outside this mapping contract.

Sequential domains may ascend or descend, preserving endpoint-color association. If both domain values are equal, every finite input maps to the channel midpoint. Diverging domains must be strictly increasing or strictly decreasing around the authored center; no symmetry is inferred. A center input maps to the exact center color. Both families clamp finite out-of-domain inputs to endpoint colors; extrapolation is not supported. Nonfinite inputs reject rather than returning an unknown color. Providers validate lazily when the resolved mapping is read.

Invalid counts, colors, domain values, or diverging order throw TypeError or RangeError. IDs must be nonempty strings or symbols. Each dedicated hook resolves the nearest matching ID and rejects a different color family, absent provider, or crossing a Layer boundary. Color IDs occupy a separate namespace from positional scale IDs. Scopes are restored after traversal/failure; signals and computed mapping lifetimes are mount-owned.

These providers allocate no drawing box, position, clipping, or event targets. Place marks and annotations using ordinary layout and positional scales. Read a mapping with .get() inside a component to subscribe to its domain/color changes; retain the returned hook signal for stable provider identity.

ThresholdColorScale takes id, thresholds, colors, and optional children. useThresholdColorScale(id): Signal<ResolvedThresholdColorScale> returns frozen thresholds, colors, bands, and map(value: number): string. Thresholds and colors accept signals. Boundaries must be finite and strictly increasing with no duplicates; colors must contain exactly one more entry than boundaries.

Each frozen ThresholdColorBand has lower: number | undefined, upper: number | undefined, and canonical color: string. Lower bounds are inclusive and upper bounds exclusive: equality belongs to the next/upper bucket. Undefined edges are unbounded. Empty thresholds with one color define a constant mapping with one unbounded band. Nonfinite map inputs reject. This is threshold classification, not quantization or distribution-derived quantiles. No data is aggregated or sorted on the caller’s behalf. The same context, palette, failure, and ownership rules apply as for the continuous families.

Minimal executable example

Provides an allocation-independent sequential color mapping.

SequentialColorScale: (props: SequentialColorScaleProps) => PibblNode

Related API: SequentialColorScale, SequentialColorScaleProps, PibblNode.

Descendant content.

View source — packages/core/src/features/viz/lib/sequential-color-scale.ts:55

Reads the dedicated ancestor sequential mapping.

useSequentialColorScale: (id: ScaleId) => Signal<ResolvedSequentialColorScale>

Related API: useSequentialColorScale, ScaleId, Signal, ResolvedSequentialColorScale.

  • id — Color-scale identity. See ScaleId.

Shared readonly mapping signal. See ResolvedSequentialColorScale.

View source — packages/core/src/features/viz/lib/sequential-color-scale.ts:65

Provides an allocation-independent color mapping with an authored center.

DivergingColorScale: (props: DivergingColorScaleProps) => PibblNode

Related API: DivergingColorScale, DivergingColorScaleProps, PibblNode.

Descendant content.

View source — packages/core/src/features/viz/lib/diverging-color-scale.ts:58

Reads the dedicated ancestor diverging mapping.

useDivergingColorScale: (id: ScaleId) => Signal<ResolvedDivergingColorScale>

Related API: useDivergingColorScale, ScaleId, Signal, ResolvedDivergingColorScale.

  • id — Color-scale identity. See ScaleId.

Shared readonly mapping signal. See ResolvedDivergingColorScale.

View source — packages/core/src/features/viz/lib/diverging-color-scale.ts:68

One authored domain/color association.

interface ColorScaleStop

Related API: ColorScaleStop.

ResolvedSequentialColorScale

View source — packages/core/src/features/viz/lib/color-types.ts:4

value
readonly value: number

Numeric data value at the color stop.

View source — packages/core/src/features/viz/lib/color-types.ts:5

color
readonly color: string

Canonical lowercase six-digit hexadecimal sRGB color.

View source — packages/core/src/features/viz/lib/color-types.ts:6

Inputs for a dedicated sequential color provider.

interface SequentialColorScaleProps

Related API: SequentialColorScaleProps.

ResolvedSequentialColorScale

View source — packages/core/src/features/viz/lib/color-types.ts:41

id
readonly id: ScaleId

Related API: ScaleId.

Identity in the color-scale namespace. See ScaleId.

View source — packages/core/src/features/viz/lib/color-types.ts:42

domain
readonly domain: SignalValue<readonly [number, number]>

Related API: SignalValue.

Finite endpoints; equal endpoints map all finite inputs to the color midpoint.

View source — packages/core/src/features/viz/lib/color-types.ts:43

colors
readonly colors: SignalValue<readonly [string, string]>

Related API: SignalValue.

Required #RRGGBB endpoint colors. No default palette is chosen.

View source — packages/core/src/features/viz/lib/color-types.ts:46

children (optional)
readonly children?: PibblNode

Related API: PibblNode.

Descendants consuming the mapping. See PibblNode.

View source — packages/core/src/features/viz/lib/color-types.ts:49

Inputs for a dedicated diverging color provider.

interface DivergingColorScaleProps

Related API: DivergingColorScaleProps.

ResolvedDivergingColorScale

View source — packages/core/src/features/viz/lib/color-types.ts:52

id
readonly id: ScaleId

Related API: ScaleId.

Identity in the color-scale namespace. See ScaleId.

View source — packages/core/src/features/viz/lib/color-types.ts:53

domain
readonly domain: SignalValue<readonly [number, number, number]>

Related API: SignalValue.

Strictly increasing or decreasing finite endpoints and center.

View source — packages/core/src/features/viz/lib/color-types.ts:54

colors
readonly colors: SignalValue<readonly [string, string, string]>

Related API: SignalValue.

Required #RRGGBB endpoint and center colors.

View source — packages/core/src/features/viz/lib/color-types.ts:57

children (optional)
readonly children?: PibblNode

Related API: PibblNode.

Descendants consuming the mapping. See PibblNode.

View source — packages/core/src/features/viz/lib/color-types.ts:60

Readonly continuous two-stop color mapping.

interface ResolvedSequentialColorScale

Related API: ResolvedSequentialColorScale.

SequentialColorScaleProps

View source — packages/core/src/features/viz/lib/color-types.ts:9

domain
readonly domain: readonly [number, number]

Authored finite endpoints, preserving ascending or descending order.

View source — packages/core/src/features/viz/lib/color-types.ts:10

colors
readonly colors: readonly [string, string]

Canonical endpoint colors, in authored order.

View source — packages/core/src/features/viz/lib/color-types.ts:14

stops
readonly stops: readonly ColorScaleStop[]

Related API: ColorScaleStop.

Frozen endpoint/value pairs for scale-derived annotations.

View source — packages/core/src/features/viz/lib/color-types.ts:18

map
map: (value: number) => string

Maps finite data to a clamped color.

  • value — Numeric input.

Canonical #rrggbb color.

View source — packages/core/src/features/viz/lib/color-types.ts:21

Readonly continuous mapping with an explicit center.

interface ResolvedDivergingColorScale

Related API: ResolvedDivergingColorScale.

DivergingColorScaleProps

View source — packages/core/src/features/viz/lib/color-types.ts:24

domain
readonly domain: readonly [number, number, number]

Strictly monotonic finite endpoints and center, in authored order.

View source — packages/core/src/features/viz/lib/color-types.ts:25

colors
readonly colors: readonly [string, string, string]

Canonical endpoint and center colors.

View source — packages/core/src/features/viz/lib/color-types.ts:30

stops
readonly stops: readonly ColorScaleStop[]

Related API: ColorScaleStop.

Frozen endpoint/center pairs for scale-derived annotations.

View source — packages/core/src/features/viz/lib/color-types.ts:35

map
map: (value: number) => string

Maps finite data through the authored center.

  • value — Numeric input.

Canonical #rrggbb color.

View source — packages/core/src/features/viz/lib/color-types.ts:38

Provides a lazy, discrete threshold-to-color mapping for descendants.

ThresholdColorScale: (props: ThresholdColorScaleProps) => PibblNode

Related API: ThresholdColorScale, ThresholdColorScaleProps, PibblNode.

Descendant content.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:90

Reads the matching ancestor threshold color mapping.

useThresholdColorScale: (id: ScaleId) => Signal<ResolvedThresholdColorScale>

Related API: useThresholdColorScale, ScaleId, Signal, ResolvedThresholdColorScale.

  • id — Provider identity. See ScaleId.

Shared readonly mapping signal. See ResolvedThresholdColorScale.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:101

One lower-inclusive, upper-exclusive threshold color bucket.

interface ThresholdColorBand

Related API: ThresholdColorBand.

ResolvedThresholdColorScale

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:12

lower
readonly lower: number | undefined

Lower inclusive boundary, omitted for the first bucket.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:14

upper
readonly upper: number | undefined

Upper exclusive boundary, omitted for the last bucket.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:16

color
readonly color: string

Validated #RRGGBB paint for this bucket.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:18

Authored threshold boundaries, bucket colors, identity, and descendant content.

interface ThresholdColorScaleProps

Related API: ThresholdColorScaleProps.

ResolvedThresholdColorScale

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:36

id
readonly id: ScaleId

Related API: ScaleId.

Stable provider identity.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:38

thresholds
readonly thresholds: SignalValue<readonly number[]>

Related API: SignalValue.

Strictly ascending finite boundaries; each equality belongs to the upper bucket.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:40

colors
readonly colors: SignalValue<readonly string[]>

Related API: SignalValue.

Exactly one #RRGGBB color per bucket.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:42

children (optional)
readonly children?: PibblNode

Related API: PibblNode.

Descendant content using this dedicated color context.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:44

Immutable resolved threshold mapping shared with descendant marks.

interface ResolvedThresholdColorScale

Related API: ResolvedThresholdColorScale.

ThresholdColorScaleProps

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:22

thresholds
readonly thresholds: readonly number[]

Strictly ascending finite authored bucket boundaries.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:24

colors
readonly colors: readonly string[]

One validated color for every bucket.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:26

bands
readonly bands: readonly ThresholdColorBand[]

Related API: ThresholdColorBand.

Readable bucket descriptions in authored order.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:28

map
readonly map: (value: number) => string

Maps a finite numeric value to its containing bucket paint.

  • value — Finite input.

Canonical lowercase #rrggbb color.

View source — packages/core/src/features/viz/lib/threshold-color-scale.ts:32

Read the Data visualization companion for ownership, adaptation, failure modes, and verification. Agent start provides the version-selection workflow.

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.