colour v1
Mixing, lightening, darkening and shifting colour values, and reading a ramp (a list of colours) at a position from 0 to 1. It works on colour values, so it works on both targets.
use colour v1Every fn gives a colour with whole channels from 0 to 255, ready to paint (fill, stroke, a mark’s paint)
or to take apart (.r, .g, .b). Hue is counted in turns: 0 red, 1/3 green, 2/3 blue, round to 1.
Lightness and saturation run from 0 to 1.
Names
| call | gives | |
|---|---|---|
colour.mix(a, b, k) | color | a and b mixed channel by channel: k = 0 gives a, k = 1 gives b (k is held to 0..1) |
colour.lighten(c, k) | color | c taken toward white by k (1 is white) |
colour.darken(c, k) | color | c taken toward black by k (1 is black) |
colour.at(ramp, k) | color | the colour at k along a ramp: 0 its first colour, 1 its last, mixed between neighbours (k is held to 0..1; the ramp holds at least one colour) |
colour.shiftHue(c, turns) | color | c with its hue moved round the wheel (0.5 is the opposite hue), saturation and lightness kept |
colour.setLightness(c, l) | color | c with its lightness set to l, hue and saturation kept |
colour.hsl(h, s, l) | color | the colour of hue h (turns, any number: 1.25 is 0.25), saturation s, lightness l |
colour.hue(c) | num | the hue of c in turns (0 for a grey) |
colour.saturation(c) | num | the saturation of c (0 for a grey) |
colour.lightness(c) | num | the lightness of c: 0 black, 1 white |
A ramp is a color[]: a colour list written out ([#1d2b3a, #9fb4c7, #ece6d8]), a let holding one, or a
list of colours worked out ([colour.at(inks, 0.2), colour.at(inks, 0.8)]).
Example
plate(1920, 2400)
use colour v1
random layout
let inks = [#1d2b3a, #3e5c76, #9fb4c7, #ece6d8]
let paper = colour.lighten(inks[3], 0.6)
background(paper)
for i in 0 ..< 12 {
let k = i / 11
// dark to pale down the ramp, each band's hue nudged a little
let band = colour.shiftHue(colour.at(inks, k), layout.float(-0.02 ..< 0.02))
rect(160, 200 + i * 170, 1600, 150)
fill(band)
}Notes
- Mixing is straight per channel. Mixing two far hues passes through a duller colour between them; to keep the
colour strong, move the hue instead (
shiftHue). - A colour list a piece only paints from is kept as text; one handed to
colour.at(or anything that takes it apart) is kept as channels instead. Both are done for you. - What it costs: about 100 bytes for
mixalone, about 230 foratwithmix,lightenanddarken, and about 720 with the hue and lightness fns too. Only what the piece calls is shipped.
The whole library, 61 lines
// colour v1: mixing, lightening, darkening and shifting colour values, and
// reading a ramp (a list of colours) at a position from 0 to 1.
//
// Every fn gives a colour with whole channels from 0 to 255. Hue, saturation and
// lightness are read the usual way (hue in turns, 0 to 1 round the wheel).
// a and b mixed channel by channel: k = 0 gives a, k = 1 gives b (k is held to 0..1)
fn mix(a: color, b: color, k: num): color {
let m = clamp(k, 0, 1)
return rgb(round(a.r + (b.r - a.r) * m), round(a.g + (b.g - a.g) * m), round(a.b + (b.b - a.b) * m))
}
// c taken toward white (k = 1 is white) or toward black (k = 1 is black)
fn lighten(c: color, k: num) = mix(c, #ffffff, k)
fn darken(c: color, k: num) = mix(c, #000000, k)
// lightness from 0 (black) to 1 (white): halfway between the strongest and weakest channel
fn lightness(c: color): num = (max(c.r, c.g, c.b) + min(c.r, c.g, c.b)) / 510
// hue in turns: 0 red, 1/3 green, 2/3 blue (0 for a grey)
fn hue(c: color): num {
let hi = max(c.r, c.g, c.b), lo = min(c.r, c.g, c.b)
if hi == lo { return 0 }
let d = hi - lo
if hi == c.r { return fract((c.g - c.b) / d / 6) }
if hi == c.g { return ((c.b - c.r) / d + 2) / 6 }
return ((c.r - c.g) / d + 4) / 6
}
// saturation from 0 (a grey) to 1 (as strong as the lightness allows)
fn saturation(c: color): num {
let hi = max(c.r, c.g, c.b) / 255, lo = min(c.r, c.g, c.b) / 255
if hi == lo { return 0 }
return (hi - lo) / (hi + lo > 1 ? 2 - hi - lo : hi + lo)
}
// one channel of the colour with hue h (turns), from the two levels p and q
fn _channel(p: num, q: num, h: num) {
let t = fract(h)
var v = p
if t < 1 / 6 {
v = p + (q - p) * 6 * t
} else if t < 1 / 2 {
v = q
} else if t < 2 / 3 {
v = p + (q - p) * (2 / 3 - t) * 6
}
return clamp(round(v * 255), 0, 255)
}
// the colour of hue h (turns, any number: 1.25 is 0.25), saturation s and lightness l (0 to 1)
fn hsl(h: num, s: num, l: num): color {
let q = l < 0.5 ? l * (1 + s) : l + s - l * s
let p = 2 * l - q
return rgb(_channel(p, q, h + 1 / 3), _channel(p, q, h), _channel(p, q, h - 1 / 3))
}
// c with its hue moved by turns round the wheel (0.5 is the opposite hue)
fn shiftHue(c: color, turns: num) = hsl(hue(c) + turns, saturation(c), lightness(c))
// c with its lightness set to l (0 to 1), its hue and saturation kept
fn setLightness(c: color, l: num) = hsl(hue(c), saturation(c), clamp(l, 0, 1))
// The colour at k along a ramp: 0 its first colour, 1 its last, mixed between
// neighbours on the way (k is held to 0..1). The ramp holds at least one colour.
fn at(ramp: color[], k: num): color {
if ramp.len == 1 { return ramp[0] }
let x = clamp(k, 0, 1) * (ramp.len - 1)
let i = min(floor(x), ramp.len - 2)
return mix(ramp[i], ramp[i + 1], x - i)
}