Maths

Module maths

Numeric wrappers that normalize their value instead of rejecting it.

import colors.maths
Module maths
Source maths/numeric.zirr
Imports none

Both types carry the @Numeric attribute, so they behave like numbers in arithmetic while constraining the range they represent. Color is built entirely from them: a hue is a Degree, saturation and lightness are Fraction. Because normalization happens in the wrapper, callers never have to clamp or wrap values themselves.

Candidate for the standard library

maths is not specific to colors. It may move into Zirric’s standard library as math, in which case this module becomes a re-export or disappears entirely. Prefer importing the types through colors rather than depending on the module path.

Contents


Data

Degree

`maths/numeric.zirr:4`
@Numeric(fn(d: Degree) -> Int { d % 360 })
data Degree {
  value: Float
}

An angle on the color wheel. The @Numeric implementation wraps the value around at 360, so Degree(400.0) and Degree(40.0) describe the same angle, and adding to a hue never leaves the wheel.

Fields

Field Type Description
value Float The raw angle in degrees, before wrapping.

Numeric

Normalization d % 360
Declared result Int
Declared result type

The attribute declares -> Int while value is a Float, so the fractional part of an angle is lost on normalization.


Fraction

`maths/numeric.zirr:17`
@Numeric(fn(f: Fraction) {
  return if f.value < 0 {
    0.0
  } else if f.value > 1 {
    1.0
  } else {
    f.value
  }
})
data Fraction {
  value: Float
}

A ratio between 0.0 and 1.0. The @Numeric implementation clamps out-of-range values to the nearest bound rather than wrapping them, which keeps repeated operations such as tint or shade saturating at white or black instead of jumping back to the other end.

Fields

Field Type Description
value Float The raw ratio, before clamping.

Numeric

Normalization 0.0 below 0, 1.0 above 1, otherwise value
Declared result inferred

See also

  • Colors — the color model built on these types.
  • Schemes — schemes stepping through hues and lightness.