struct

TermBuf::Gradient

Inherits Struct < Value < Object

Stability: stable — changes only in a major release.

A colour ramp across a rectangle, handed to a draw call as a Blend.

A gradient is not a field of Style: styles are interned by value, so everything in one has to hash and compare, and a colour that depends on where the cell is cannot. It is a position-aware blend instead — the one thing in the drawing API that already takes a cell's coordinates.

ramp = TermBuf::Gradient.new Color.rgb(0x101820), Color.rgb(0x2060C0),
  screen.bounds, :vertical
screen.clear TermBuf::Style::DEFAULT, blend: ramp.background

A view can carry one instead, in which case the gradient is built against View#bounds and the view translates each cell into its own coordinates before asking. See View#blend.

The colours it answers with are always Color.rgb, whatever the endpoints were: the encoder narrows a 24 bit colour to the 256 colour cube or to the sixteen system colours against the terminal's mask, so one gradient renders everywhere and only the banding differs.

Each cell's colour is a separate Style, and StyleTable only grows. A gradient painted once, or repainted in the same colours, costs a style per cell it covers and no more. One whose endpoints move every frame interns a fresh set each time; an animation wanting that should step through a fixed palette instead.

Constructors

new(from : Color, to : Color, rect : Rect, axis : Axis = Axis::Horizontal)

A ramp from from to to across rect, along axis. Reach for #foreground or #background to get the Blend a draw call takes.

Source

Instance methods

at(x : Int32, y : Int32) : Color

The colour at (x, y): #from at the leading edge of #rect, #to at the trailing one, linearly interpolated per channel in between and clamped to the ends outside.

An endpoint that is not already a 24 bit colour resolves through Color#channels first, so a palette index or the terminal's default is a usable end of a ramp.

Source
axis

Which way the ramp runs.

Source
background

A Blend setting the cell colour of the style being written to #at, leaving everything else in it alone. What a tinted panel wants: fill it through the gradient, then write over it naming only the text colour.

Source
foreground

A Blend setting the text colour of the style being written to #at for the cell, leaving everything else in it alone.

What is already in the cell is not consulted: a gradient decides by position, and a write that also wants what was underneath should compose this with Style::OVER by carrying one on a view and the other on the draw call. See View#blend.

Source
from

Colour at the rectangle's leading edge.

Source
rect

The area the ramp is spread across. Positions outside it clamp to the nearer end, so a gradient does not have to cover everything drawn through it.

Source
to

Colour at the rectangle's trailing edge.

Source
to_s(io : IO) : Nil

Same as #inspect(io).

Source

Nested types