Skip to content

Style function library

Keenan Crane edited this page May 20, 2022 · 5 revisions

For the most up-to-date list of objectives, constraints, and functions, see these autogenerated doc pages:

  • Constraints — can be used in Style via the ensure keyword.
  • Objectives — can be used in Style via the encourage keyword.

as well as

  • Autodiff Functions — low-level differentiable functions (needed only if you are trying to add new Style constraints or objectives)

Note that constraints/objectives may be exact for common shapes, and use approximations for less common shape combinations. For instance, the statement ensure disjoint( shape1, shape2 ) will exactly prevent overlap for the following pairs (as can be seen here):

  • Line-Line
  • Circle-Circle
  • Polygon-Polygon
  • Rectangle-Circle
  • Circle-Line

where "Rectangle" really means any rectangle-like shape (e.g., a text box), and similarly for "Line." For other shape combinations (such as complex Bézier paths), Penrose currently just enforces that their bounding boxes do not overlap. This list is constantly changing/improving—the source linked to above provides the most updated reference.

Colors

Colors have type color, and include an alpha (i.e., opacity) channel. Colors can be specified via two different color models:

  • color c = rgba(r,g,b,a) — defines a color via the RGB color model, with red, green, blue, and alpha values in the range [0,1].
  • color c = hsva(h,s,v,a) — defines a color via the HSV color model, with hue in the range [0,360], saturation and value in the range [0,100], and alpha in the range [0,1].

To specify that a color should be omitted altogether, you can also use none(). E.g.,

fillColor: none()
strokeColor: none()

Note that none() is different from using a 100% transparent color: it really prevents the fill or stroke from being drawn altogether.

Math functions

Most of the functions found in the JavaScript Math object can also be used in Style. Some minor differences noted below.

JavaScript Style Notes
abs(x) abs(x)
acos(x) acos(x)
acosh(x) acosh(x)
asin(x) asin(x)
asinh(x) asinh(x)
atan(x) atan(x)
atan2(y,x) atan2(y,x)
atanh(x) atanh(x)
cbrt(x) cbrt(x)
ceil(x) ceil(x) Derivative is zero almost everywhere.
cos(x) cos(x)
cosh(x) cosh(x)
exp(x) exp(x)
expm1(x) expm1(x)
floor(x) floor(x) Derivative is zero almost everywhere.
hypot(v) norm(v) Operates on a vector v. Not implemented using JavaScript Math.hypot().
log(x) log(x) Named ln in Autodiff.ts (to avoid a name conflict), but is still named log in Style.
log2(x) log2(x)
log10(x) log10(x)
log1p(x) log1p(x)
max(x,...) max(x,y)
min(x,...) min(x,y)
pow(x, y) pow(x, y)
random() NA Can't differentiate, and semantics are unclear for a Style program.
round(x) round(x) Derivative is zero almost everywhere.
sign(x) sign(x) Derivative is zero almost everywhere.
sin(x) sin(x)
sinh(x) sinh(x)
sqrt(x) sqrt(x)
tan(x) tan(x)
tanh(x) tanh(x)
trunc(x) trunc(x) Derivative is zero almost everywhere.

JavaScript Math also provides some standard mathematical constants; in Style math constants are provided by static functions.

JavaScript Style Notes
E MathE()
LN2 NA Can be computed in Style via log(2.).
LN10 NA Can be computed in Style via log(10.).
LOG2E NA Can be computed in Style via log2(MathE()).
LOG10E NA Can be computed in Style via log10(MathE()).
PI MathPI()
SQRT1_2 NA Can be computed in Style via sqrt(.5).
SQRT2 NA Can be computed in Style via sqrt(2.).
Clone this wiki locally