Class rtk.Shadow

A utility class that renders a general purpose shadow frame. Useful, for example, to render a shadow around an rtk-based popup menu.

This is meant more for internal widget implementations but you're free to use this directly.

Only the frame is rendered based on the elevation, not the inner part, so it does not affect the coloring of translucent surfaces drawn over top.

A bit janky

The shadow quality isn't all that it might be. Consider this a WIP that will see future refinement.

Example
-- Create a red translucent shadow
local shadow = rtk.Shadow('red#44')
-- Create a crimson colored box that's 50% the window size
local spacer = window:add(rtk.Spacer{w=0.5, h=0.5, bg='crimson'})
-- When the widget reflows, regenerate the shadow
spacer.onreflow = function(self)
    shadow:set_rectangle(self.calc.w, self.calc.h, 40)
end
-- And when the is drawn, also draw the spacer at the widget's
-- position.
spacer.ondraw = function(self, offx, offy, alpha)
    shadow:draw(self.calc.x + offx, self.calc.y + offy, alpha)
end

Shadow Type Constants

Used with the type attribute that indicates the type of shadow.

rtk.Shadow.RECTANGLE

A rectangular shadow

rtk.Shadow.CIRCLE

A circular shadow

Class API

Synopsis

Attributes
type shadowtypeconst

read-only

The type of shadow based on whether set_rectangle() or set_circle() was called

color colortype

read/write

The color of the shadow (defaults to #00000055)

w number or nil

read-only

The width that was last passed to set_rectangle()

h number or nil

read-only

The height that was last passed to set_rectangle()

radius number or nil

read-only

The radius that was last passed to set_circle()`

elevation number or nil

read-only

The computed elevation based on the last call to set_rectangle() or set_circle()`

Methods
rtk.Shadow()

Create a new Shadow instance

set_rectangle()

Sets a rectangular shadow

set_circle()

Sets a circular shadow

draw()

Draws the shadow on the current drawing target

Attributes

rtk.Shadow.type shadowtypeconst read-only

The type of shadow based on whether set_rectangle() or set_circle() was called.

rtk.Shadow.color colortype read/write

The color of the shadow (defaults to #00000055)

rtk.Shadow.w number or nil read-only

The width that was last passed to set_rectangle()

rtk.Shadow.h number or nil read-only

The height that was last passed to set_rectangle()

rtk.Shadow.radius number or nil read-only

The radius that was last passed to set_circle()`

rtk.Shadow.elevation number or nil read-only

The computed elevation based on the last call to set_rectangle() or set_circle()`

Methods

rtk.Shadow(color)

Create a new Shadow instance

rtk.Shadow:set_rectangle(w, h, elevation, t, r, b, l)

Sets a rectangular shadow.

When this is called, the internal shadow image is re-rendered, so it should only be called when the dimensions actually change, or when you want to change elevation.

Parameters
w (number)

the width of the content box the shadow is wrapping, where the shadow starts around the edges and expands outward beyond the given width

h (number)

like w but for height

elevation (number or nil)

affects the apparent height of object the shadow intends to apply to, which roughly corresponds to the number of pixels the shadow expands out to.

t (number or nil)

number of pixels for the top edge, or uses elevation if nil

r (number or nil)

number of pixels for the right edge, or uses elevation if nil

b (number or nil)

number of pixels for the bottom edge, or uses elevation if nil

l (number or nil)

number of pixels for the left edge, or uses elevation if nil

rtk.Shadow:set_circle(radius, elevation)

Sets a circular shadow.

When this is called, the internal shadow image is re-rendered, so it should only be called when the radius changes, or when you want to change elevation.

Parameters
radius (number)

the radius of the inner content area in pixels, which the shadow will wrap and expand outward from

elevation (number or nil)

affects the apparent height of object the shadow intends to apply to, which roughly corresponds to the number of pixels the shadow expands out to. If nil, the elevation defaults to 2/3 of the radius.

rtk.Shadow:draw(x, y, alpha)

Draws the shadow on the current drawing target.

Parameters
x (number)

the x coordinate of the inner content box that the shadow wraps

y (number)

the y coordinate of the inner content box that the shadow wraps

alpha (number or nil)

the opacity of the shadow, which applies a multiplier to the calculated shadow opacity derived from elevation (default 1.0)