Ollin

InductionChromatique

A panel of blue, black, and white lines built to bring out a yellow that is not on it, after Cruz-Diez's Induction Chromatique (1963).

Run
swift run --package-path Examples Example-Recreations-CarlosCruzDiez-InductionChromatique
Source
Examples/Recreations/CarlosCruzDiez/InductionChromatique/Sketch.swift
//  Recreation after Carlos Cruz-Diez - the Inductions Chromatiques (from
//  1963). They are panels of lines, here blue, black, and white. The
//  foundation that keeps his work says a yellow appears on them that no
//  pigment put there. Two were looked at closely. One is "Induction
//  chromatique n° 54. Bleu + Noir = Jaune" (April 1973; acrylic on canvas
//  mounted on plywood, 120 x 120 cm; Centre Pompidou, Paris). The other is
//  the "Induction du Jaune série Nov 06" (Paris, 2006, 100 x 100 cm). A
//  homage, not a reproduction, and not affiliated with or endorsed by the
//  artist, his family, the Cruz-Diez Foundation, the Atelier Cruz-Diez,
//  Articruz, or the museum that holds the painting.
//  https://cruz-diez.com/works/induction-chromatique/
//  https://www.centrepompidou.fr/en/ressources/oeuvre/cAgjk8M
//
//  An original Ollin interpretation, written from the foundation's account of
//  the series and from pictures of the works. Nothing was ported. The lattice
//  was counted off the foundation's ten pictures of the 2006 series. It has
//  42 colored lines and the 41 gaps between them, each gap as wide as a
//  line. The ways a black line meets a colored one were measured off the
//  same pictures. The layouts are this sketch's own, dealt by seed. The parts the
//  inks play in the orange follow a photograph of a 1975 panel; in the red
//  they are this sketch's guess.

import Foundation
import Ollin

/// An *Induction Chromatique* (Carlos Cruz-Diez, Paris). **Move the pointer
/// up** to step back from the panel, and down to come close.
///
/// Stare at a red plane, look away, and for a moment you see it again in
/// green. That second color is the eye's own, and it fades as you watch.
/// From 1963 he built panels meant to bring it out while you are still
/// looking, and to hold it there. The foundation that keeps his work gives
/// the recipes. The first is "inductive yellow, which is obtained by
/// superimposing black, blue, and white". The others are an orange from
/// blue, yellow, and black, and a red from green, white, and black. A
/// painting of 1973 in the Centre Pompidou carries the sum in its title,
/// *Bleu + Noir = Jaune*: blue plus black equals yellow. Of the color that
/// appears, the foundation says "it is as real as the pigments used."
///
/// The panel is a lattice. `lines` colored lines cross it from edge to edge,
/// with a gap of bare ground as wide as a line between each two. Black lines
/// of the same thickness are laid over them in upright zones. In a zone
/// every black line meets its colored line in the same way. In a block it
/// lies exactly over it, so the block reads black and ground. In a passage
/// it slides across the zone, from over the colored line to over the gap
/// below or above. Or it tapers from nothing to a full line. Where it
/// slides, the three inks trade places a little at a time, and the bare
/// ground narrows to a sliver between color and black.
///
/// What is on the canvas is three flat inks and nothing else: the ground,
/// the colored lines, and the black. A count of the pixels will tell you
/// so. If you see a fourth color, your eye made it. This sketch draws the
/// panel and does not paint that color, model it, or promise it, because
/// screens differ and so do eyes. He said that what happens in these works
/// changes with where the spectator stands and with the light. So the
/// pointer lets you walk.
///
/// While the pointer rests you walk by yourself: back from `distance`
/// centimeters by `walk` more, and in again. The walk goes one pixel of
/// line thickness at a time and stands a moment at each. While you stand,
/// every line falls on whole pixels, so the screen adds no blur of its own.
///
/// Try it: `induce` picks the recipe, yellow, orange, or red. `lines` sets
/// how fine the lattice is: the 2006 series has 42 colored lines, and the
/// 1973 painting has 67. `panelSeed` deals a new layout of zones, and so
/// does a click. `--export-svg` writes every line and every black piece as
/// a polygon, so the lattice and the meetings can be read back from the
/// file.
@main
final class InductionChromatique: Sketch {
    /// The color a panel is built to bring out, by the three recipes the
    /// foundation gives.
    enum Induced: String, CaseIterable, ParamOption { case yellow, orange, red }

    /// Which recipe the panel follows: yellow from black, blue, and white;
    /// orange from blue, yellow, and black; red from green, white, and
    /// black.
    @Param(icon: "eyedropper") var induce = Induced.yellow
    /// How many colored lines cross the panel.
    @Param(20 ... 70, icon: "line.3.horizontal") var lines = 42
    /// How far from the panel you stand while the pointer rests, in
    /// centimeters. At 150 the panel fills the canvas.
    @Param(150 ... 600, icon: "figure.stand") var distance = 150.0
    /// How much farther back the resting walk takes you, in centimeters; 0
    /// stands still.
    @Param(0 ... 450, icon: "figure.walk") var walk = 300.0
    /// Deals the layout of zones.
    @Param(1 ... 999, icon: "dice") var panelSeed = 6

    override var canvasSize: CanvasSize { .square(1080) }
    override var loopDuration: Double? {
        let (near, far) = thicknesses()
        return near > far ? InductionChromatique.loop : nil
    }

    /// The resting walk comes back to its start after this many seconds.
    nonisolated static let loop = 24.0
    /// The panel's side at the nearest distance, in pixels, before it is
    /// trimmed to a whole number of pixels for every line.
    nonisolated static let nearestSide = 1000.0
    /// The nearest distance, in centimeters: where the panel fills the
    /// canvas.
    nonisolated static let nearestDistance = 150.0
    /// The panel is divided into this many units across; a zone's sides fall
    /// on them.
    nonisolated static let units = 32

    // MARK: - The inks

    /// The three inks of one recipe.
    struct Inks {
        var ground: Color
        var line: Color
        var black: Color
    }

    /// The blue and the white are the ones in the foundation's pictures of
    /// the 2006 series. The yellow and its blue are sampled from a
    /// photograph of a 1975 panel. The green is this sketch's own choice.
    nonisolated static func inks(for induced: Induced) -> Inks {
        switch induced {
        case .yellow:
            return Inks(ground: Color(hex: 0xFFFFFF), line: Color(hex: 0x3C00FE), black: Color(hex: 0x000000))
        case .orange:
            return Inks(ground: Color(hex: 0xF4AF0A), line: Color(hex: 0x212D8D), black: Color(hex: 0x000000))
        case .red:
            return Inks(ground: Color(hex: 0xFFFFFF), line: Color(hex: 0x00A843), black: Color(hex: 0x000000))
        }
    }

    /// The wall is a neutral gray, so nothing around the panel leans toward
    /// any color.
    nonisolated static let wall = Color(hex: 0xD6D6D6)

    // MARK: - The layout

    /// How a black line meets its colored line at one side of a zone.
    /// `offset` is how far its upper edge lies below the colored line's, in
    /// line thicknesses. That is 0 exactly over the line, 1 over the gap
    /// below, and -1 over the gap above. `thickness` is how thick it is
    /// there, from 0 to 1.
    struct Meeting: Equatable {
        var offset: Double
        var thickness: Double

        static let over = Meeting(offset: 0, thickness: 1)
        static let below = Meeting(offset: 1, thickness: 1)
        static let above = Meeting(offset: -1, thickness: 1)
        /// Nothing yet, at the colored line's upper edge and at its lower.
        static let nothingAtTop = Meeting(offset: 0, thickness: 0)
        static let nothingAtBottom = Meeting(offset: 1, thickness: 0)
    }

    /// An upright zone of black lines. It runs from unit `x0` to unit `x1`
    /// across, over the colored lines `first` through `last` counted from
    /// the top. It has one meeting at its left side and one at its right.
    /// Between the two sides each black line runs straight.
    struct Zone: Equatable {
        var x0, x1: Int
        var first, last: Int
        var left, right: Meeting

        /// The same zone on the other side of the panel's middle.
        var mirrored: Zone {
            Zone(x0: InductionChromatique.units - x1, x1: InductionChromatique.units - x0,
                 first: first, last: last, left: right, right: left)
        }
    }

    /// The zones of one panel, dealt by `seed`. Blocks, where the black lies
    /// over the colored lines, take turns across the panel with passages,
    /// where it slides or tapers. Now and then a bar of bare lattice stands
    /// between them, or a zone is cut in two with a band of lattice across
    /// it. Two panels in three are the same on both sides of the middle, as
    /// most of the 2006 series is.
    nonisolated static func deal(seed: Int, lines: Int) -> [Zone] {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 41)))
        func oneIn(_ n: Int) -> Bool { Int.random(in: 0 ..< n, using: &rng) == 0 }

        // How many lines a zone may stop short of the panel's top or bottom.
        let margins = [0, lines / 14, lines / 8, lines / 6, lines / 4]
        func span() -> (Int, Int) {
            let top = margins[Int.random(in: 0 ..< margins.count, using: &rng)]
            let bottom = margins[Int.random(in: 0 ..< margins.count, using: &rng)]
            return (top, lines - 1 - bottom)
        }

        // The meetings of a passage, at its outer side and at the inner side
        // that touches a block. All five were measured off the series.
        func passage() -> (outer: Meeting, inner: Meeting) {
            switch Int.random(in: 0 ..< 9, using: &rng) {
            case 0, 1, 2:
                // A slide: over the colored line outside, over a gap inside.
                return (.over, oneIn(2) ? .below : .above)
            case 3, 4:
                // A taper on the colored line, growing down from its upper
                // edge.
                return (.nothingAtTop, .over)
            case 5, 6:
                // A taper on the colored line, growing up from its lower
                // edge.
                return (.nothingAtBottom, .over)
            case 7:
                // A taper on the gap below, growing down from the line.
                return (.nothingAtBottom, .below)
            default:
                // A line that grows as it climbs from the colored line's
                // lower edge to the gap above.
                return (.nothingAtBottom, .above)
            }
        }

        // One zone's pieces: the lines it may cover, and now and then two
        // stacked pieces with a band of bare lattice between them.
        func pieces(x0: Int, x1: Int, extent: (Int, Int), left: Meeting, right: Meeting) -> [Zone] {
            var (first, last) = extent
            // A line cannot lie over a gap the panel does not have.
            if left == .above || right == .above { first = max(first, 1) }
            if left == .below || right == .below { last = min(last, lines - 2) }
            let band = max(1, lines / 10)
            if oneIn(4), last - first > 4 * band + 6 {
                let middle = (first + last) / 2
                return [Zone(x0: x0, x1: x1, first: first, last: middle - band, left: left, right: right),
                        Zone(x0: x0, x1: x1, first: middle + band, last: last, left: left, right: right)]
            }
            return [Zone(x0: x0, x1: x1, first: first, last: last, left: left, right: right)]
        }

        // Blocks and passages by turns from unit `from` to unit `to`.
        func lay(from: Int, to: Int, endsBesideBlock: Bool) -> [Zone] {
            var out: [Zone] = []
            var x = from
            var isBlock = oneIn(2)
            var besideBlock = false
            var extent = span()
            while x < to {
                var width = isBlock ? Int.random(in: 4 ... 9, using: &rng) : Int.random(in: 3 ... 7, using: &rng)
                if to - x - width < 3 { width = to - x }
                if oneIn(2) { extent = span() }
                let isLast = x + width >= to
                var left = Meeting.over, right = Meeting.over
                if !isBlock {
                    let blockAfter = isLast ? endsBesideBlock : true
                    let innerIsRight = blockAfter && besideBlock ? oneIn(2) : blockAfter || !besideBlock
                    let meetings = passage()
                    left = innerIsRight ? meetings.outer : meetings.inner
                    right = innerIsRight ? meetings.inner : meetings.outer
                }
                out += pieces(x0: x, x1: x + width, extent: extent, left: left, right: right)
                x += width
                // Now and then a bar of bare lattice, one unit wide, after a
                // block.
                if isBlock, to - x > 5, oneIn(4) {
                    x += 1
                    besideBlock = false
                } else {
                    besideBlock = isBlock
                }
                isBlock.toggle()
            }
            return out
        }

        func panel() -> [Zone] {
            let middle = units / 2
            if oneIn(3) {
                // One run across the whole panel.
                let from = Int.random(in: 0 ... 4, using: &rng)
                return lay(from: from, to: units - Int.random(in: 0 ... 4, using: &rng), endsBesideBlock: false)
            }
            // A middle zone, a run on its left, and the same run turned
            // around on its right.
            let half = Int.random(in: 2 ... 6, using: &rng)
            let hasMiddle = !oneIn(4)
            let side = lay(from: Int.random(in: 0 ... 3, using: &rng), to: middle - half, endsBesideBlock: hasMiddle)
            var zones = side
            if hasMiddle {
                zones += pieces(x0: middle - half, x1: middle + half, extent: span(), left: .over, right: .over)
            }
            return zones + side.map(\.mirrored)
        }

        // A panel needs a passage and a block: the passages are where the
        // three inks meet, and a block gives them something to lean on.
        var zones = panel()
        for _ in 0 ..< 40 {
            if zones.contains(where: { $0.left != $0.right }), zones.contains(where: { $0.left == $0.right }) { break }
            zones = panel()
        }
        return zones
    }

    private var zones: [Zone] = []
    private var dealtFor: [Int] = []
    private var shown = 0.0
    private var shownReady = false
    private var lastMoved = -100.0

    /// A click deals the next layout.
    override func mousePressed() {
        panelSeed = panelSeed >= 999 ? 1 : panelSeed + 1
    }

    // MARK: - The walk

    /// The thickness of one line, in whole pixels, where the resting walk is
    /// nearest and where it is farthest.
    func thicknesses() -> (near: Int, far: Int) {
        let nearest = (InductionChromatique.nearestSide / Double(2 * lines - 1)).rounded(.down)
        func at(_ centimeters: Double) -> Int {
            max(2, Int((nearest * InductionChromatique.nearestDistance / centimeters).rounded()))
        }
        let near = min(Int(nearest), at(distance))
        return (near, min(near, at(distance + walk)))
    }

    /// The thickness of one line at `time` while the pointer rests. The
    /// walk goes back a pixel at a time and in again. Each step stands for
    /// three quarters of its time and strides for the last quarter.
    func restingThickness(at time: Double) -> Double {
        let (near, far) = thicknesses()
        let steps = 2 * (near - far)
        guard steps > 0 else { return Double(near) }
        let lap = (time / InductionChromatique.loop).truncatingRemainder(dividingBy: 1)
        let place = max(0, lap) * Double(steps)
        let index = min(steps - 1, Int(place))
        let within = place - Double(index)
        func size(_ i: Int) -> Int {
            let j = i % steps
            return j <= near - far ? near - j : far + (j - (near - far))
        }
        let from = size(index), to = size(index + 1)
        let stride = max(0, (within - 0.75) / 0.25)
        return Double(from) + Double(to - from) * stride * stride * (3 - 2 * stride)
    }

    /// The pointer stands for you while it moves: up the canvas is farther
    /// away. When it has rested a while, the walk takes over.
    private func thickness() -> Double {
        if mouse != previousMouse { lastMoved = time }
        let resting = restingThickness(at: time)
        let sincePointer = time - lastMoved
        // Long at rest, the thickness is a function of the clock alone, so
        // an exported frame does not depend on the frames before it.
        guard sincePointer < 5 else {
            shown = resting
            shownReady = true
            return resting
        }
        let target: Double
        if sincePointer < 4 {
            let nearest = (InductionChromatique.nearestSide / Double(2 * lines - 1)).rounded(.down)
            let away = InductionChromatique.nearestDistance + (1 - mouseY / height) * 450
            target = max(2, min(nearest, (nearest * InductionChromatique.nearestDistance / away).rounded()))
        } else {
            target = resting
        }
        if !shownReady {
            shown = target
            shownReady = true
        } else {
            shown += (target - shown) * (1 - exp(-deltaTime * 12))
            if abs(target - shown) < 0.02 { shown = target }
        }
        return shown
    }

    // MARK: - Drawing

    override func draw() {
        let key = [panelSeed, lines]
        if key != dealtFor {
            zones = InductionChromatique.deal(seed: panelSeed, lines: lines)
            dealtFor = key
        }
        let inks = InductionChromatique.inks(for: induce)
        let line = thickness()

        background(InductionChromatique.wall)
        noStroke()

        // The panel: a square of 2 * lines - 1 stripes, colored lines and
        // the gaps between them, centered, its corner on a whole pixel.
        let side = Double(2 * lines - 1) * line
        let left = ((width - side) / 2).rounded()
        let top = ((height - side) / 2).rounded()

        // It stands a little off the wall; its shadow falls down and right,
        // in neutral gray.
        for (i, spread) in [9.0, 5.0, 2.0].enumerated() {
            fill(Color(white: 0, alpha: 0.04 + 0.03 * Double(i)))
            drawRect(left + 4 - spread, top + 6 - spread, side + 2 * spread, side + 2 * spread)
        }

        fill(inks.ground)
        drawPolygon([Vector2(left, top), Vector2(left + side, top),
                     Vector2(left + side, top + side), Vector2(left, top + side)])

        fill(inks.line)
        for k in 0 ..< lines {
            let y = top + Double(2 * k) * line
            drawPolygon([Vector2(left, y), Vector2(left + side, y),
                         Vector2(left + side, y + line), Vector2(left, y + line)])
        }

        // The black. In each zone there is one piece for every colored line
        // it covers. It runs straight from the meeting at the left side to
        // the meeting at the right. A side where the black is nothing makes
        // the piece a triangle.
        fill(inks.black)
        for zone in zones {
            let x0 = left + (side * Double(zone.x0) / Double(InductionChromatique.units)).rounded()
            let x1 = left + (side * Double(zone.x1) / Double(InductionChromatique.units)).rounded()
            guard zone.last >= zone.first else { continue }
            for k in zone.first ... zone.last {
                let y = top + Double(2 * k) * line
                let la = y + zone.left.offset * line, lb = la + zone.left.thickness * line
                let ra = y + zone.right.offset * line, rb = ra + zone.right.thickness * line
                var piece = [Vector2(x0, la), Vector2(x1, ra)]
                if rb > ra { piece.append(Vector2(x1, rb)) }
                if lb > la { piece.append(Vector2(x0, lb)) }
                if piece.count >= 3 { drawPolygon(piece) }
            }
        }

        if !isExporting {
            drawText("move the pointer up to step back, click for another panel", width / 2, height - 12, size: 14,
                     color: Color(white: 0, alpha: 0.4), align: .center, .bottom)
        }
    }
}

↑↓ move ↩ open