Ollin

RojoAzulAmarillo

A gold board ruled in black by a drafting machine, with three flat colors, after Gunther Gerzso's Rojo-azul-amarillo (1970).

Run
swift run --package-path Examples Example-Recreations-GuntherGerzso-RojoAzulAmarillo
Source
Examples/Recreations/GuntherGerzso/RojoAzulAmarillo/Sketch.swift
//  Recreation after Gunther Gerzso - Rojo-azul-amarillo (Red-Blue-Yellow),
//  signed and dated 1970, acrylic and pen on board, 15.6 x 17.8 cm. Shown as
//  plate 94 of Risking the Abstract: Mexican Modernism and the Art of Gunther
//  Gerzso (Santa Barbara Museum of Art, 2003). A homage, not a reproduction,
//  and not affiliated with or endorsed by the artist's estate.
//  https://digitalcommons.chapman.edu/art_books/19
//  https://mamfa.com/exhibitions/beyond-muralism-gerzso-goeritz-and-friedeberg
//
//  An original Ollin interpretation written from a picture of the painting
//  and from the chronology in the retrospective's catalog. The chronology
//  calls it his first painting made with a drafting machine. Nothing was
//  ported: the work is acrylic and pen on board.

import Foundation
import Ollin

/// Rojo-azul-amarillo (Gunther Gerzso, 1970). A board small enough to hold
/// in one hand. Its ground is a mottled gold. Everything else on it is ruled
/// in black with a pen. Two heavy uprights hold a column of level lines, set
/// so close that they read as a tone. A white square is cut into the
/// column. Three bars of flat color stand in one lower corner and a red slab
/// lies in the other. The title is the names of the colors.
///
/// A drafting machine is a pair of rulers fixed at a right angle on a
/// jointed arm. The arm lets them slide anywhere on the board, and they keep
/// their angle as they go. His son Michael, an architect, showed him one in
/// 1966. The catalog of his 2003 retrospective calls this board his first
/// painting made with it. The catalog tells of it under 1969, and the board
/// is signed 1970.
///
/// So the rule here is the machine's. Every line is straight. Nearly all of
/// them run level or upright, the two ways the rulers point. A few are
/// slanted, as if the head had been turned for them. There is no shading:
/// a tone is lines ruled closer together. The measures of the column, the
/// square, the bars, and the slab are read off a picture of the board.
///
/// The seed deals one such board, and no seed is the painting itself. The
/// cycle shows it whole, then starts a fresh board. The flat colors go on,
/// and the lines are ruled one after another, in an order a hand could
/// take. A vector export holds the flats and the ruled lines alone, as
/// strokes a pen can follow.
///
/// Try it: `pitch` is how far apart the hatch lines are, which sets the
/// tone of the column. `heavy` is the weight of the heavy lines, and at 0
/// every line is a hairline. `slants` is how many lines are ruled with the
/// head turned. `colors` names the three colors after other titles of his.
/// `machine` shows the two rulers at work, and a press deals the next board.
@main
final class RojoAzulAmarillo: Sketch {
    /// Three colors named in the title, as he named paintings in those years.
    enum Colors: String, CaseIterable, ParamOption {
        case rojoAzulAmarillo, verdeAzulAmarillo, rojoAzulNaranja, grisVerdeNaranja
    }

    /// How far apart the hatch lines are ruled, in pixels of a board 1080
    /// pixels tall. Closer lines read as a darker tone.
    @Param(3 ... 14, icon: "line.3.horizontal") var pitch = 4.9
    /// How heavy the heavy lines are. At 0 every line is a hairline.
    @Param(0 ... 2, icon: "lineweight") var heavy = 1.0
    /// How many lines are ruled on a slant, with the machine's head turned.
    @Param(0 ... 8, icon: "line.diagonal") var slants = 4
    /// The three colors, each set named after a title of his.
    @Param(icon: "paintpalette") var colors = Colors.rojoAzulAmarillo
    /// How mottled the painted ground is. At 0 the board is flat color.
    @Param(0 ... 1, icon: "circle.dotted") var grain = 0.7
    /// The two rulers of the drafting machine, shown while the lines go on.
    @Param(icon: "ruler") var machine = true
    /// One cycle: the board whole, then a fresh board ruled from the start.
    @Param(8 ... 30, icon: "clock") var seconds = 24.0

    // The board is 17.8 by 15.6 centimeters.
    override var canvasSize: CanvasSize { .size(1232, 1080) }
    override var loopDuration: Double? { seconds }

    // MARK: - The parts

    /// One ruled line: its two ends and its weight, from 0 for a hairline to
    /// 1 for the heaviest bar. `begun` is how far the pen has traveled, in
    /// pixels, when it starts this line.
    private struct Ruled {
        var from: Vector2
        var to: Vector2
        var weight: Double
        var begun: Double
        var length: Double { (to - from).length }
    }

    /// One flat of color, or of ink where `inked` is set. The ink flat is
    /// part of the drawing, so it goes on after the last line.
    private struct Flat {
        var outline: [Vector2]
        var color: Color
        var inked = false
    }

    /// A board: its lines in the order they are ruled, its flats, and how
    /// far the pen travels in all.
    private struct Board {
        var lines: [Ruled] = []
        var flats: [Flat] = []
        var travel = 1.0
    }

    /// A ground, three colors for the bars, a color for the slab, and the
    /// pale of the square.
    private struct Scheme {
        var ground: UInt32
        var bars: [UInt32]
        var slab: UInt32
        var pale: UInt32
    }

    /// The first set is read off a picture of the board. The others take
    /// only their names from his titles.
    private static let schemes: [Colors: Scheme] = [
        .rojoAzulAmarillo: Scheme(ground: 0xB8924A, bars: [0x435A88, 0xB5A386, 0xB67A36], slab: 0xB84A44,
                                  pale: 0xDAD6CE),
        .verdeAzulAmarillo: Scheme(ground: 0x8FA387, bars: [0x3F6A8A, 0xB9B79A, 0xC9A63C], slab: 0x4F7A52,
                                   pale: 0xD4D2C2),
        .rojoAzulNaranja: Scheme(ground: 0xC0763A, bars: [0x3E5484, 0xC2A486, 0xD9963C], slab: 0xA83A34,
                                 pale: 0xD8CEC0),
        .grisVerdeNaranja: Scheme(ground: 0x9A9A8E, bars: [0x5E7A5A, 0xC2BEB0, 0xC97F34], slab: 0x55705A,
                                  pale: 0xD6D4CC),
    ]

    private let ink = Color(hex: 0x131718)

    private var board = Board()
    private var built = (seed: Int.min, pitch: 0.0, slants: -1, colors: Colors.rojoAzulAmarillo, width: 0.0,
                         height: 0.0)
    private var pressed = 0

    override func mousePressed() {
        pressed += 1
    }

    // MARK: - Dealing a board

    /// Deal one board. Places are parts of the width and the height until
    /// `at` turns them into pixels, mirrored for half the seeds. A weight
    /// is 0 for a hairline and grows to 1 for the heaviest bar.
    private func deal(seed: Int) -> Board {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 1970)))
        // The hatching rolls from a generator of its own, so a change of
        // `pitch` leaves every other measure of the board where it was.
        var spacing = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 66)))
        func pick<T>(_ options: [T]) -> T { options[Int.random(in: 0 ..< options.count, using: &rng)] }
        func coin(_ p: Double) -> Bool { Double.random(in: 0 ..< 1, using: &rng) < p }
        let w = width, h = height
        let mirrored = coin(0.5)
        func at(_ u: Double, _ v: Double) -> Vector2 { Vector2((mirrored ? 1 - u : u) * w, v * h) }
        let scheme = RojoAzulAmarillo.schemes[colors] ?? Scheme(ground: 0xB8924A, bars: [0, 0, 0], slab: 0, pale: 0)
        var result = Board()
        // The pen moves at one speed, so a long line takes longer to rule
        // than a short one. Lifting it to the next line costs a little too.
        var traveled = 0.0
        let lift = 0.05 * w
        // The four weights, as parts of the heaviest: hairline, medium,
        // bold, and heavy.
        let hair = 0.0, medium = 0.22, bold = 0.45, heaviest = 1.0
        func rule(_ from: (Double, Double), _ to: (Double, Double), _ weight: Double) {
            let line = Ruled(from: at(from.0, from.1), to: at(to.0, to.1), weight: weight, begun: traveled)
            result.lines.append(line)
            traveled += line.length + lift
        }
        func flat(_ points: [(Double, Double)], _ hex: UInt32) {
            result.flats.append(Flat(outline: points.map { at($0.0, $0.1) }, color: Color(hex: hex)))
        }

        // The measures the drawing hangs on, read off the board and moved a
        // little by the seed.
        let left = pick([0.31, 0.345, 0.358, 0.372, 0.398]), right = pick([0.83, 0.842, 0.856, 0.868, 0.884])
        let lintel = pick([0.2, 0.24, 0.262, 0.282, 0.3, 0.33])
        let bandEnd = lintel + pick([0.052, 0.06, 0.07])
        let fieldTop = bandEnd + pick([0.062, 0.08, 0.095])
        let foot = pick([0.738, 0.75, 0.762])
        let stackTop = pick([0.592, 0.612, 0.63]), stackLeft = right - pick([0.03, 0.036, 0.044])
        let side = 0.11 * w / h
        let squareLeft = pick([0.528, 0.543, 0.558]), squareRight = squareLeft + 0.11
        let squareTop = foot - side * pick([0.24, 0.29, 0.34]), squareBottom = squareTop + side
        let barsRight = pick([0.268, 0.281, 0.292]), barsTop = foot + pick([0.018, 0.022, 0.026])
        let slabLeft = pick([0.618, 0.634, 0.652]), slabTop = pick([0.895, 0.905, 0.915])
        let fieldBottom = slabTop - pick([0.014, 0.02])
        let gap = pitch / 1080

        // The flats: the square, the three bars, and the slab.
        flat([(squareLeft, squareTop), (squareRight, squareTop), (squareRight, squareBottom),
              (squareLeft, squareBottom)], scheme.pale)
        let shares = [0.325, 0.375, 0.3]
        let hues = scheme.bars.shuffled(using: &rng)
        var edge = 0.0
        var dividers: [Double] = []
        for (index, share) in shares.enumerated() {
            let next = index == 2 ? barsRight : edge + barsRight * share
            flat([(edge, barsTop), (next, barsTop), (next, 1), (edge, 1)], hues[index])
            if index < 2 { dividers.append(next) }
            edge = next
        }
        flat([(slabLeft, slabTop), (1, slabTop), (1, 1), (slabLeft, 1)], scheme.slab)

        // The uprights first, each with thin lines beside it.
        rule((left, 0), (left, foot), heaviest)
        rule((left + 0.014, 0), (left + 0.014, lintel), hair)
        rule((left + 0.025, 0), (left + 0.025, lintel), hair)
        rule((right, 0), (right, stackTop), 0.55)
        rule((right + 0.012, 0), (right + 0.012, stackTop - 0.07), hair)
        rule((right + 0.016, 0), (right + 0.016, stackTop - 0.05), hair)
        if coin(0.7) {
            let far = right + pick([0.05, 0.062])
            rule((far, 0), (far, stackTop - 0.1), hair)
            rule((far + 0.005, 0), (far + 0.005, stackTop - 0.06), hair)
        }

        /// Rule a field of level lines from `top` down to `bottom`.
        /// `spans` gives the stretches a line at one height crosses. The
        /// spacing changes every few lines, and a heavier line opens each
        /// change.
        func hatch(from top: Double, to bottom: Double, opens: Bool = true, _ spans: (Double) -> [(Double, Double)]) {
            var v = top + gap
            var remaining = opens ? 0 : Int.random(in: 7 ... 18, using: &spacing)
            var factor = 1.0
            while v < bottom - gap * 0.6 {
                var weight = hair
                if remaining == 0 {
                    remaining = Int.random(in: 7 ... 18, using: &spacing)
                    factor = [0.8, 0.9, 1, 1, 1.15, 1.35][Int.random(in: 0 ..< 6, using: &spacing)]
                    weight = [medium * 0.6, medium, medium, bold * 0.7][Int.random(in: 0 ..< 4, using: &spacing)]
                }
                for span in spans(v) where span.1 - span.0 > 0.004 {
                    rule((span.0, v), (span.1, v), weight)
                }
                remaining -= 1
                v += gap * factor
            }
        }

        // The column: a lintel, a narrow band of hatching, a clear band,
        // and then the long field down to the slab.
        rule((left, lintel), (right, lintel), bold)
        hatch(from: lintel, to: bandEnd, opens: false) { _ in [(left, right)] }
        rule((left, bandEnd), (right, bandEnd), medium)
        rule((left, fieldTop), (right, fieldTop), medium)
        hatch(from: fieldTop, to: fieldBottom, opens: false) { v in
            if v < stackTop { return [(left, right)] }
            if v < squareTop { return [(left, stackLeft)] }
            if v < foot { return [(left, squareLeft), (squareRight, stackLeft)] }
            if v < squareBottom { return [(squareRight, stackLeft)] }
            return [(squareLeft + 0.03, stackLeft)]
        }

        // The foot of the column and the frame of the square.
        rule((left, foot), (squareLeft, foot), heaviest)
        rule((squareLeft, squareTop), (squareRight, squareTop), bold)
        rule((squareRight, squareTop), (squareRight, squareBottom), medium)
        rule((squareRight, squareBottom), (squareLeft, squareBottom), bold)
        rule((squareLeft, squareBottom), (squareLeft, squareTop), bold)

        // The lower zone beside the bars: an open band that the slanted
        // lines cross, then two bands of hatching.
        let zoneRight = squareLeft + 0.03
        let open = barsTop + pick([0.07, 0.082, 0.095])
        let second = open + pick([0.042, 0.05]), last = min(0.955, second + pick([0.04, 0.05]))
        rule((barsRight, barsTop), (squareLeft, barsTop), medium)
        rule((barsRight, open), (zoneRight, open), bold)
        hatch(from: open, to: second) { _ in [(barsRight, zoneRight)] }
        rule((barsRight, second), (slabLeft, second), bold)
        hatch(from: second, to: last) { _ in [(barsRight, slabLeft)] }
        rule((barsRight, last), (slabLeft, last), bold)

        // The slants. The first is the long one, from the foot of the
        // column down across the zone. The rest fan from the bars toward
        // one point beside the square.
        if slants > 0 {
            rule((left, foot + 0.004), (zoneRight + 0.045, second - 0.01), medium)
        }
        let apex = (squareLeft - pick([0.0, 0.012, 0.03]), barsTop + (open - barsTop) * pick([0.45, 0.55, 0.62]))
        if slants > 1 {
            for index in 1 ..< slants {
                let start = barsTop + (open - barsTop) * (Double(index) - 0.3) / Double(slants - 1) * 0.98
                rule((barsRight, start), apex, hair)
            }
        }

        // The stack at the side: level bands under the short upright, one
        // of them a small panel with upright lines and a patch of hatching.
        rule((stackLeft, stackTop), (1, stackTop), bold)
        rule((stackLeft, stackTop), (stackLeft, fieldBottom), bold)
        var level = stackTop
        var panel = Int.random(in: 1 ... 2, using: &rng)
        while level < fieldBottom - 0.07 {
            let tall = pick([0.03, 0.045, 0.056, 0.062])
            let next = min(level + tall, fieldBottom - 0.03)
            if panel == 0 {
                let split = stackLeft + (1 - stackLeft) * pick([0.3, 0.38])
                for index in 0 ..< 4 {
                    let u = stackLeft + 0.012 + 0.011 * Double(index)
                    rule((u, level), (u, next), index == 3 ? medium : hair)
                }
                rule((split, level), (split, next), medium)
                hatch(from: level, to: level + (next - level) * 0.55, opens: false) { _ in [(split, 1)] }
            }
            rule((stackLeft, next), (1, next), pick([medium, bold, bold]))
            panel -= 1
            level = next
        }
        rule((stackLeft - 0.1, fieldBottom), (1, fieldBottom), heaviest * 0.8)

        // The bars and the slab get their edges last.
        rule((0, barsTop), (barsRight, barsTop), bold)
        for divider in dividers {
            rule((divider, barsTop), (divider, 1), bold * 0.8)
        }
        rule((barsRight, barsTop), (barsRight, 1), bold)
        rule((slabLeft - 0.006, slabTop - 0.004), (1, slabTop - 0.004), bold)
        rule((slabLeft - 0.008, fieldBottom), (slabLeft - 0.008, 1), heaviest * 0.75)

        // The one flat of ink: a block at the edge that thins to a point at
        // the foot of the column.
        let wedge = pick([0.09, 0.105, 0.12])
        result.flats.append(Flat(outline: [at(0, foot - 0.03), at(wedge, foot - 0.024), at(left, foot - 0.002),
                                           at(left, foot + 0.006), at(wedge, foot + 0.008), at(0, foot + 0.008)],
                                 color: ink, inked: true))
        // The ink flat takes the last stretch of the pen's travel.
        result.travel = traveled + 0.3 * w
        return result
    }

    // MARK: - The ground

    /// The mottle of the painted ground, laid over the flat color as a thin
    /// coat. It has small pale blots where the paint was dabbed thin, a
    /// slow drift of warmer patches, and a fine speck.
    private static let mottle = """
    float4 shade(float2 uv, ShaderInfo info) {
        float grain = param(info, 0);
        float seed = param(info, 1);
        float3 ground = float3(param(info, 2), param(info, 3), param(info, 4));
        float2 p = info.position;
        float spot = smoothstep(0.5, 0.14, worley(p / 15.0 + seed * 3.7) + 0.5 * (fbm(p / 5.0 + seed) - 0.5));
        float blot = spot * smoothstep(0.24, 0.56, fbm(p / 44.0 + seed * 5.3));
        float warm = smoothstep(0.46, 0.78, fbm(p / 34.0 + seed * 8.1));
        float cool = smoothstep(0.50, 0.80, fbm(p / 90.0 + seed * 1.9));
        float speck = hash12(floor(p) + seed * 17.0);
        float pale = grain * 0.26 * blot * (0.75 + 0.5 * speck);
        float deep = grain * (0.22 * warm + 0.12 * cool + 0.05 * speck) * (1.0 - blot);
        float alpha = pale + deep - pale * deep;
        float3 thin = mix(ground, float3(0.80, 0.84, 0.82), 0.55);
        float3 thick = ground * float3(0.82, 0.66, 0.44);
        float3 color = (thin * pale * (1.0 - deep) + thick * deep) / max(alpha, 1.0e-4);
        return float4(color, alpha);
    }
    """

    // MARK: - Drawing

    override func draw() {
        let seed = variation &+ pressed
        if built.seed != seed || built.pitch != pitch || built.slants != slants || built.colors != colors
            || built.width != width || built.height != height {
            built = (seed, pitch, slants, colors, width, height)
            board = deal(seed: seed)
        }
        let scheme = RojoAzulAmarillo.schemes[colors]
        let cycle = max(8, seconds)
        let turn = (time / cycle).truncatingRemainder(dividingBy: 1)
        func ramp(_ from: Double, _ to: Double) -> Double {
            let t = clamp((turn - from) / (to - from), 0, 1)
            return t * t * (3 - 2 * t)
        }
        // The cycle. The board stands whole until 0.22. By 0.29 it has
        // given way to a fresh one. The flats go on from 0.31 to 0.39, the
        // lines are ruled from 0.40 to 0.95, and the board stands whole
        // again to the end.
        let fresh = turn >= 0.3
        let fade = fresh ? 1 : 1 - ramp(0.22, 0.29)
        let ruled = fresh ? clamp((turn - 0.40) / 0.55, 0, 1) : 1
        // How far the pen has come, in pixels of line and of lifting.
        let traveled = ruled * board.travel
        func amount(_ line: Ruled) -> Double {
            fresh ? clamp((traveled - line.begun) / max(1, line.length), 0, 1) : 1
        }

        let ground = Color(hex: scheme?.ground ?? 0xB8924A)
        background(ground)
        noStroke()
        // A vector export leaves the mottle out. It then holds nothing but
        // the flats and the ruled lines, which a pen can follow.
        if grain > 0, !isVectorExporting {
            fill(Shader(RojoAzulAmarillo.mottle,
                        params: [Float(grain), Float(seed % 97), Float(ground.red), Float(ground.green),
                                 Float(ground.blue)],
                        using: [.noise, .hash]))
            drawRect(0, 0, width, height)
        }
        // The flats are a thin coat, so a little of the ground's mottle
        // comes through them.
        let paints = board.flats.filter { !$0.inked }
        let thin = isVectorExporting ? 1 : 1 - 0.1 * grain
        for (index, flat) in paints.enumerated() {
            let begun = 0.31 + 0.06 * Double(index) / Double(max(1, paints.count))
            let coat = fresh ? ramp(begun, begun + 0.025) : fade
            guard coat > 0 else { continue }
            fill(flat.color.withAlpha(coat * thin))
            drawPolygon(flat.outline)
        }

        let unit = height / 1080
        strokeCap(.butt)
        // The pen is at the last line it has begun.
        var pen: Ruled?
        for line in board.lines {
            let grown = amount(line)
            guard grown > 0 else { continue }
            pen = line
            stroke(ink.withAlpha(fade))
            strokeWeight((1.5 + 17.5 * line.weight * heavy) * unit)
            drawLine(line.from, line.from + (line.to - line.from) * grown)
        }
        noStroke()
        let last = (board.lines.last?.begun ?? 0) + (board.lines.last?.length ?? 0)
        for flat in board.flats where flat.inked {
            let coat = fresh ? fade * clamp((traveled - last) / max(1, board.travel - last), 0, 1) : fade
            guard coat > 0 else { continue }
            fill(ink.withAlpha(coat))
            drawPolygon(flat.outline)
        }
        if machine, !isVectorExporting, fresh, ruled > 0, traveled < last, let pen {
            drawMachine(ruling: pen, reach: amount(pen))
        }
    }

    /// The drafting machine at the line it is ruling. A long ruler lies
    /// along the line, a second one stands square to it, and the round head
    /// is where they meet. For a slanted line the whole head is turned.
    /// `reach` is how far along the line the pen has come.
    private func drawMachine(ruling line: Ruled, reach: Double) {
        let unit = height / 1080
        var along = (line.to - line.from).normalized
        let upright = abs(along.y) > abs(along.x)
        // The long ruler points right, or up for an upright line, whichever
        // way the line itself was ruled.
        if upright ? along.y > 0 : along.x < 0 { along = -along }
        let start = (line.to - line.from).dot(along) >= 0 ? line.from : line.to
        // `across` is the side the ruler's body lies on: under a level
        // line, and to the left of an upright one.
        let across = upright ? Vector2(along.y, -along.x) : Vector2(-along.y, along.x)
        let head = start - along * (46 * unit)
        let long = 0.6 * width, short = 0.42 * width, broad = 34 * unit
        let glass = Color(hex: 0xF4F1E6)
        // The second ruler leaves the head on the side away from the first
        // one's body.
        let square = -across
        withState {
            fill(glass.withAlpha(0.13))
            stroke(glass.withAlpha(0.6))
            strokeWeight(1.2 * unit)
            // The ruler the pen runs along, and the one square to it.
            drawPolygon([head, head + along * long, head + along * long + across * broad, head + across * broad])
            drawPolygon([head, head + square * short, head + square * short - along * broad, head - along * broad])
            // The marks of the scale, one every hundredth of the width.
            let mark = width / 100
            var place = mark
            while place < long {
                let tall = Int((place / mark).rounded()) % 5 == 0 ? 0.5 : 0.28
                drawLine(head + along * place, head + along * place + across * (broad * tall))
                place += mark
            }
            fill(glass.withAlpha(0.3))
            drawCircle(center: head + (across - along) * (broad * 0.5), radius: broad * 0.62)
            // The pen, where it has come to.
            noStroke()
            fill(ink)
            drawCircle(center: line.from + (line.to - line.from) * reach, radius: 3.2 * unit)
        }
    }
}

↑↓ move ↩ open