Colors
Module colors
Core color model, conversions and manipulation primitives.
import colors
| Module | colors |
| Source | colors.zirr |
| Imports | colors.maths — Degree, Fraction |
Contents
- Data —
Color,Theme - Unions —
ColorLike - Constants —
white,black,retroDark - Constructors —
from,hsl,rgb,hex - Manipulation —
complement,tint,shade,tone,mix
Data
Color
`colors.zirr:8`
data Color {
hue: Degree
saturation: Fraction
lightness: Fraction
}
A color in the HSL color space. All three components are normalizing wrappers from colors.maths: Degree wraps around at 360, Fraction clamps to the range 0.0 … 1.0.
Fields
| Field | Type | Description |
|---|---|---|
hue |
Degree |
Position on the color wheel, in degrees. |
saturation |
Fraction |
Color intensity, 0.0 (gray) to 1.0 (full). |
lightness |
Fraction |
Brightness, 0.0 (black) to 1.0 (white). |
Theme
`colors.zirr:121`
data Theme {
name: String
accent: Scheme
highlight: Scheme
surface: Scheme
border: Scheme
text: Scheme
warning: Color
error: Color
info: Color
success: Color
}
A named collection of color schemes and status colors. The five scheme fields generate as many colors as a consumer requests — see Scheme — while the four status fields are fixed single colors.
Fields
| Field | Type | Description |
|---|---|---|
name |
String |
Human readable theme name. |
accent |
Scheme |
Scheme for primary, attention-drawing elements. |
highlight |
Scheme |
Scheme for secondary emphasis and selections. |
surface |
Scheme |
Scheme for backgrounds, layered from base upwards. |
border |
Scheme |
Scheme for separators and outlines. |
text |
Scheme |
Scheme for foreground text, layered from primary downwards. |
warning |
Color |
Status color for warnings. |
error |
Color |
Status color for errors. |
info |
Color |
Status color for informational messages. |
success |
Color |
Status color for successful operations. |
Unions
ColorLike
`colors.zirr:14`
union ColorLike {
Color
Int
Array
}
Everything that can be interpreted as a Color. Use from to normalize a ColorLike into a Color.
Cases
| Case | Interpretation |
|---|---|
Color |
Used as is. |
Int |
A hex color value, e.g. 0xFF3EC0. |
Array |
Three elements. Int elements are read as RGB, Float elements as HSL. |
Constants
white
`colors.zirr:5`
const white = hex(0xFFFFFF)
Pure white.
black
`colors.zirr:6`
const black = hex(0x000000)
Pure black.
retroDark
`colors.zirr:136`
const retroDark = Theme(
"Retro Dark",
Analogous(hex(0xFF3EC0), Degree(30)),
Analogous(hex(0x00FFD0), Degree(30)),
prepend(hex(0x0B0C1F), Tone(hex(0x1F1B2E), 0.1)),
Tone(0x3A1F55, 0.1),
prepend(hex(0xF8F8FF), Tone(hex(0xB0B0FF), 0.1)),
hex(0xFFE600),
hex(0xFF6F3C),
hex(0x1E90FF),
hex(0x39FF14)
)
A built-in dark Theme with neon accents. Accent and highlight use Analogous schemes at 30° spacing, surface and text use a fixed base color prepended to a Tone scheme.
Constructors
from
`colors.zirr:20`
fn from(raw: ColorLike) -> Color
Normalizes any ColorLike value into a Color.
Parameters
| Parameter | Type | Description |
|---|---|---|
raw |
ColorLike |
The value to convert. |
Returns
Color — the normalized color.
Behavior
| Input | Result |
|---|---|
Color |
Returned unchanged. |
Int |
Converted via hex. |
Array of three Int |
Converted via rgb. |
Array of three Float |
Converted via hsl. |
Panics
"Invalid color array type"— the array contains neitherIntnorFloat."invalid color array length"— the array does not contain exactly three elements."invalid color type"— the value is not a member ofColorLike.
hsl
`colors.zirr:38`
fn hsl(h: @Numeric, s: @Numeric, l: @Numeric) -> Color
Creates a Color from hue, saturation and lightness. The arguments are wrapped in Degree and Fraction, so out-of-range values are normalized rather than rejected.
Parameters
| Parameter | Type | Description |
|---|---|---|
h |
@Numeric |
Hue in degrees. Wraps at 360. |
s |
@Numeric |
Saturation. Clamped to 0.0 … 1.0. |
l |
@Numeric |
Lightness. Clamped to 0.0 … 1.0. |
Returns
Color
rgb
`colors.zirr:42`
fn rgb(r: @Numeric, g: @Numeric, b: @Numeric) -> Color
Creates a Color from red, green and blue channels, converting them to HSL. Negative hues are rotated back into the 0 … 360 range.
Parameters
| Parameter | Type | Description |
|---|---|---|
r |
@Numeric |
Red channel, 0 … 255. |
g |
@Numeric |
Green channel, 0 … 255. |
b |
@Numeric |
Blue channel, 0 … 255. |
Returns
Color
hex
`colors.zirr:70`
@Returns(Color)
fn hex(num: Int)
Creates a Color from a packed 24 bit hex value, where the highest byte is red, the middle byte green and the lowest byte blue.
Parameters
| Parameter | Type | Description |
|---|---|---|
num |
Int |
Packed color value, e.g. 0xFF3EC0. |
Returns
Color
The body currently panics with "unimplemented".
Manipulation
complement
`colors.zirr:77`
fn complement(color: Color) -> Color
Generates a complementary color by shifting the hue by 180 degrees. Saturation and lightness are preserved.
Parameters
| Parameter | Type | Description |
|---|---|---|
color |
Color |
The base color. |
Returns
Color — the color opposite on the color wheel.
tint
`colors.zirr:86`
fn tint(color: Color, amount: maths.Fraction) -> Color
Lightens a color by mixing it with white. Hue and saturation are preserved.
Parameters
| Parameter | Type | Description |
|---|---|---|
color |
Color |
The base color. |
amount |
maths.Fraction |
How far to move towards white. 0.0 keeps the color, 1.0 yields white. |
Returns
Color
shade
`colors.zirr:95`
fn shade(color: Color, amount: maths.Fraction) -> Color
Darkens a color by mixing it with black. Hue and saturation are preserved.
Parameters
| Parameter | Type | Description |
|---|---|---|
color |
Color |
The base color. |
amount |
maths.Fraction |
How far to move towards black. 0.0 keeps the color, 1.0 yields black. |
Returns
Color
tone
`colors.zirr:104`
fn tone(color: Color, amount: maths.Fraction) -> Tone
Reduces the saturation of a color, making it more gray. Hue and lightness are preserved.
Parameters
| Parameter | Type | Description |
|---|---|---|
color |
Color |
The base color. |
amount |
maths.Fraction |
How much saturation to remove. 0.0 keeps the color, 1.0 yields gray. |
Returns
Tone — as declared. The body returns a Color.
mix
`colors.zirr:113`
fn mix(base: Color, blend: Color, ratio: maths.Fraction) -> Color
Mixes two colors together by a given ratio. Hue, saturation and lightness are interpolated linearly and independently.
Parameters
| Parameter | Type | Description |
|---|---|---|
base |
Color |
The color at ratio == 0.0. |
blend |
Color |
The color at ratio == 1.0. |
ratio |
maths.Fraction |
Blend position, 0.0 … 1.0. |
Returns
Color