Visualize.Interpolate (Visualize v0.2.35)

Copy Markdown View Source

Interpolation functions for smooth transitions between values.

Provides various interpolation methods for numbers, colors, strings, arrays, and other data types.

Examples

# Number interpolation
interp = Visualize.Interpolate.number(0, 100)
interp.(0.5)  # => 50.0

# Color interpolation (RGB)
interp = Visualize.Interpolate.rgb("#ff0000", "#0000ff")
interp.(0.5)  # => "#800080" (purple)

# String interpolation with embedded numbers
interp = Visualize.Interpolate.string("10px solid red", "20px solid blue")
interp.(0.5)  # => "15px solid ..."

Summary

Functions

Creates an interpolator between two lists of numbers.

Creates a basis spline interpolator through a series of values.

Creates an interpolator between two Date values.

Creates an interpolator between two DateTime values.

Creates a discrete interpolator that returns one of n values.

Creates an HSL color interpolator.

Creates an HSL interpolator that takes the long path around the hue circle.

Creates a linear interpolator between two numbers.

Creates an RGB color interpolator.

Interpolates between two numbers, rounding to the nearest integer.

Creates an interpolator for strings containing numbers.

Creates an interpolator for SVG transform strings.

Creates a smooth zoom interpolator between two views.

Functions

array(a, b)

@spec array([number()], [number()]) :: (number() -> [float()])

Creates an interpolator between two lists of numbers.

Lists must be the same length.

Examples

interp = Visualize.Interpolate.array([0, 0], [100, 200])
interp.(0.5)  # => [50.0, 100.0]

basis(values)

@spec basis([number()]) :: (number() -> float())

Creates a basis spline interpolator through a series of values.

Returns smooth values that pass near (but not through) control points.

Examples

interp = Visualize.Interpolate.basis([0, 10, 5, 20, 15])
interp.(0.5)  # Smooth value around middle

date(a, b)

@spec date(Date.t(), Date.t()) :: (number() -> Date.t())

Creates an interpolator between two Date values.

datetime(a, b)

@spec datetime(DateTime.t(), DateTime.t()) :: (number() -> DateTime.t())

Creates an interpolator between two DateTime values.

Examples

start = ~U[2024-01-01 00:00:00Z]
finish = ~U[2024-12-31 00:00:00Z]
interp = Visualize.Interpolate.datetime(start, finish)
interp.(0.5)  # => ~U[2024-07-01 ...]

discrete(values)

@spec discrete([any()]) :: (number() -> any())

Creates a discrete interpolator that returns one of n values.

Useful for stepped animations or categorical transitions.

Examples

interp = Visualize.Interpolate.discrete(["a", "b", "c", "d"])
interp.(0.0)   # => "a"
interp.(0.25)  # => "b"
interp.(0.99)  # => "d"

hsl(a, b)

@spec hsl(any(), any()) :: (number() -> String.t())

Creates an HSL color interpolator.

Interpolating in HSL space often produces more pleasing transitions than RGB, especially for colors that are far apart on the color wheel.

Examples

interp = Visualize.Interpolate.hsl("#ff0000", "#00ff00")
interp.(0.5)  # Interpolates through yellow

hsl_long(a, b)

@spec hsl_long(any(), any()) :: (number() -> String.t())

Creates an HSL interpolator that takes the long path around the hue circle.

number(a, b)

@spec number(number(), number()) :: (number() -> float())

Creates a linear interpolator between two numbers.

Examples

interp = Visualize.Interpolate.number(0, 100)
interp.(0.0)  # => 0.0
interp.(0.5)  # => 50.0
interp.(1.0)  # => 100.0

rgb(a, b)

@spec rgb(any(), any()) :: (number() -> String.t())

Creates an RGB color interpolator.

Accepts hex strings, RGB tuples, or Color structs.

Examples

interp = Visualize.Interpolate.rgb("#ff0000", "#0000ff")
interp.(0.5)  # => "#800080"

round(a, b)

@spec round(number(), number()) :: (number() -> integer())

Interpolates between two numbers, rounding to the nearest integer.

string(a, b)

@spec string(String.t(), String.t()) :: (number() -> String.t())

Creates an interpolator for strings containing numbers.

Numbers in the string are interpolated while other characters are preserved from the ending string.

Examples

interp = Visualize.Interpolate.string("10px", "20px")
interp.(0.5)  # => "15px"

interp = Visualize.Interpolate.string("rotate(0)", "rotate(180)")
interp.(0.5)  # => "rotate(90)"

transform(a, b)

@spec transform(String.t(), String.t()) :: (number() -> String.t())

Creates an interpolator for SVG transform strings.

Handles translate, scale, and rotate transformations.

Examples

interp = Visualize.Interpolate.transform(
  "translate(0, 0) rotate(0)",
  "translate(100, 50) rotate(90)"
)
interp.(0.5)  # => "translate(50, 25) rotate(45)"

zoom(list1, list2)

@spec zoom([number()], [number()]) :: (number() -> [float()])

Creates a smooth zoom interpolator between two views.

Views are specified as [cx, cy, width] representing the center point and visible width. Uses van Wijk and Nuij's algorithm.

Examples

interp = Visualize.Interpolate.zoom(
  [0, 0, 100],      # Start: centered at origin, width 100
  [500, 300, 50]    # End: centered at (500,300), width 50
)
[cx, cy, w] = interp.(0.5)