colour v2
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 v2Every 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(from, toward, amount) | color | from and toward mixed channel by channel: amount = 0 gives from, amount = 1 gives toward (amount is held to 0..1) |
colour.lighten(from, amount) | color | from taken toward white by amount (1 is white) |
colour.darken(from, amount) | color | from taken toward black by amount (1 is black) |
colour.at(ramp, along) | color | the colour a ramp has at along: 0 its first colour, 1 its last, mixed between neighbours (along is held to 0..1; the ramp holds at least one colour) |
colour.shiftHue(from, turn) | color | from with its hue moved by turn round the wheel (0.5 is the opposite hue), saturation and lightness kept |
colour.setLightness(from, level) | color | from with its lightness set to level, 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, along: 0.2), colour.at(inks, along: 0.8)]).
Example
plate(1920, 2400)
use colour v2
random layout
let inks = [#1d2b3a, #3e5c76, #9fb4c7, #ece6d8]
let paper = colour.lighten(inks[3], amount: 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, along: k), turn: 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, 63 lines
// colour v2: 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).
// from and toward mixed channel by channel: amount = 0 gives from, amount = 1 gives toward
// (amount is held to 0..1)
fn mix(from: color, toward: color, amount: num): color {
let m = clamp(amount, 0, 1)
return rgb(round(from.r + (toward.r - from.r) * m), round(from.g + (toward.g - from.g) * m),
round(from.b + (toward.b - from.b) * m))
}
// from taken toward white (amount = 1 is white) or toward black (amount = 1 is black)
fn lighten(from: color, amount: num) = mix(from, toward: #ffffff, amount: amount)
fn darken(from: color, amount: num) = mix(from, toward: #000000, amount: amount)
// 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: q, h: h + 1 / 3), _channel(p, q: q, h: h), _channel(p, q: q, h: h - 1 / 3))
}
// from with its hue moved by turn round the wheel (0.5 is the opposite hue)
fn shiftHue(from: color, turn: num) = hsl(hue(from) + turn, s: saturation(from), l: lightness(from))
// from with its lightness set to level (0 to 1), its hue and saturation kept
fn setLightness(from: color, level: num) = hsl(hue(from), s: saturation(from), l: clamp(level, 0, 1))
// The colour a ramp has at along: 0 its first colour, 1 its last, mixed between
// neighbours on the way (along is held to 0..1). The ramp holds at least one colour.
fn at(ramp: color[], along: num): color {
if ramp.len == 1 { return ramp[0] }
let x = clamp(along, 0, 1) * (ramp.len - 1)
let i = min(floor(x), ramp.len - 2)
return mix(ramp[i], toward: ramp[i + 1], amount: x - i)
}