# Cataract3

wavy bands of paired colors that pass from gray into red and turquoise, after Riley's Cataract 3 (1967).

Run it with `swift run --package-path Examples Example-Recreations-BridgetRiley-Cataract3`.

```swift
//  Recreation after Bridget Riley - Cataract 3 (1967), polyvinyl acetate on
//  canvas, 221.9 x 222.9 cm, British Council Collection. Shown at the Venice
//  Biennale in 1968. A homage, not a reproduction, and not affiliated with
//  or endorsed by the artist or the collection.
//  https://bridget-riley.publications.britishart.yale.edu/catalogue/32/
//  https://bridget-riley.publications.britishart.yale.edu/catalogue/33/
//
//  An original Ollin interpretation, written from her own words on the
//  series, quoted in the Yale Center for British Art's catalog, and from the
//  catalog's picture of the painting. She says that colors twisted along a
//  curve keep changing what they lie next to. She also says that she builds
//  clusters of color sequences that flow into each other. The wave, the step
//  from one band to the next, and the widths of the stripes are measured off
//  that picture. So are the eighteen pairs of colors and the order of the
//  bands. No published account lists them. Nothing was ported: the work is
//  paint on canvas.

import Foundation
import Ollin

/// *Cataract 3* (Bridget Riley, 1967): wavy bands of two colors on white
/// that pass from gray into red and turquoise and back.
///
/// Every edge in the painting is the same wave, four waves long from side
/// to side. It is copied down the canvas at a constant step, and each copy
/// is slipped a little to the right of the one above. Because of the slip,
/// two neighboring edges are closest where the wave runs downhill and
/// farthest apart where it climbs. So every stripe swells and pinches along
/// its length, and the pinches line up into diagonals.
///
/// A band is two stripes, a warm one over a cool one, with white below.
/// Her scale study for the series shows such pairs side by side, each a
/// step grayer than the last. In the painting the pairs run from two grays,
/// one a little warm and one a little cool, to a red and a turquoise.
/// Eighteen pairs are sampled here from a picture of the painting.
///
/// The bands go up the scale in clusters of three. The outer two bands of a
/// cluster share one pair. The middle band takes the pair of the cluster
/// next to it on the gray side. Going down the canvas each cluster is one
/// pair further from gray, until two clusters of full color about five
/// eighths of the way down. Below them the color leaves in bigger steps.
///
/// The painting is still. Here the waves stay where they are and the color
/// travels. The flood of red and turquoise moves down through the bands,
/// leaves at the bottom, and comes back in at the top. One lap of `seconds`
/// brings it round to where the painting has it.
///
/// Try it: `color` at 0 leaves only the two grays. `slip` at 0 stacks the
/// waves straight and the stripes stop pinching. `swing` is how tall the
/// wave is. `--export-svg` writes every stripe as a closed outline.
@main
final class Cataract3: Sketch {
    /// How many bands share the height.
    @Param(30 ... 140, icon: "line.3.horizontal") var bands = 87
    /// Whole waves from the left edge to the right edge.
    @Param(1 ... 8, icon: "water.waves") var waves = 4.0
    /// How far the wave rises and falls, in steps from one band to the next.
    @Param(0 ... 1.5, icon: "arrow.up.and.down") var swing = 1.0
    /// How far each band is slipped to the right of the one above, as a
    /// share of one wave. At 0 the stripes keep one thickness.
    @Param(-0.1 ... 0.1, icon: "arrow.right") var slip = 0.079
    /// How far the bands go from gray toward full color: 0 is the two grays.
    @Param(0 ... 1, icon: "paintpalette") var color = 1.0
    /// Seconds for one lap: the color travels down the canvas and comes
    /// back in at the top.
    @Param(6 ... 30, icon: "clock") var seconds = 24.0

    override var loopDuration: Double? { seconds }

    /// The white of the canvas, sampled from the catalog picture.
    private let ground = Color(hex: 0xE9E7E1)

    /// The scale of pairs, from the two grays to the red and the turquoise,
    /// sampled band by band from the catalog picture.
    private let warm: [Color] = [
        0x70777F, 0x747980, 0x7A7A7F, 0x80797E, 0x87787C, 0x91787A,
        0x977273, 0xA06E6B, 0xA86967, 0xAF6765, 0xB46460, 0xBA5F56,
        0xBB5A4D, 0xC15441, 0xC84F38, 0xCF4C35, 0xDA4815, 0xD93413,
    ].map { Color(hex: $0) }
    private let cool: [Color] = [
        0x667982, 0x647982, 0x627D86, 0x597F88, 0x5A7E86, 0x4E7E88,
        0x3C7C88, 0x348491, 0x268391, 0x138795, 0x1B8492, 0x0E8896,
        0x018291, 0x038E9E, 0x018D9E, 0x0090A4, 0x008CA4, 0x008EA2,
    ].map { Color(hex: $0) }

    /// The place on the scale of each cluster of three bands, from the top
    /// of the canvas to the bottom, read off the picture. The color climbs
    /// one pair at a time and comes down faster.
    nonisolated static let clusters: [Double] = [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
        17, 16, 15, 14, 13, 11, 8, 5, 4, 2, 1,
    ]
    /// The share of a step that the warm stripe takes, then the share the
    /// warm and the cool stripe take together. The rest is white.
    nonisolated static let warmShare = 0.265
    nonisolated static let colorShare = 0.54
    /// Where the first band's top edge sits below the top of the canvas, in
    /// steps. Then where the wave stands at the left edge, in radians.
    nonisolated static let firstBand = 0.71
    nonisolated static let phase = -0.47

    /// The place on the scale of a cluster's middle band: the place of
    /// whichever neighboring cluster is nearer to gray.
    nonisolated static func middle(of index: Int) -> Double {
        let count = clusters.count
        let before = clusters[(index + count - 1) % count]
        let after = clusters[(index + 1) % count]
        return min(clusters[index], before, after)
    }

    /// A place on the scale as a pair of colors. A place between two pairs
    /// is a mix of them.
    private func pair(at place: Double) -> (warm: Color, cool: Color) {
        let top = Double(warm.count - 1)
        let p = min(max(place * color, 0), top)
        let i = min(Int(p), warm.count - 2)
        let t = p - Double(i)
        return (Color.mix(warm[i], warm[i + 1], t), Color.mix(cool[i], cool[i + 1], t))
    }

    override func setup() {
        noStroke()
    }

    override func draw() {
        background(ground)

        let step = height / Double(bands)
        let wavelength = width / waves
        let reach = swing * step

        // A point on the edge that sits `below` steps under the first one.
        // That edge is the same wave moved down by that much and slipped
        // sideways in proportion, so the pinch comes from the slip alone.
        func edge(_ below: Double, at x: Double) -> Vector2 {
            let turn = (x / wavelength - slip * below) * .tau + Self.phase
            return Vector2(x, below * step + reach * sin(turn))
        }

        // The outline of one stripe: along its upper edge from left to
        // right, then back along its lower edge. It is drawn a wave wider
        // than the canvas, because each band is moved sideways by its slip.
        func stripe(from upper: Double, to lower: Double) -> Shape {
            let samples = Int(((width + wavelength) / 3).rounded(.up))
            let left = -wavelength
            let across = width + wavelength
            var outline: [Vector2] = []
            outline.reserveCapacity(2 * (samples + 1))
            for i in 0 ... samples {
                outline.append(edge(upper, at: left + across * Double(i) / Double(samples)))
            }
            for i in stride(from: samples, through: 0, by: -1) {
                outline.append(edge(lower, at: left + across * Double(i) / Double(samples)))
            }
            return Shape(outline, closed: true)
        }
        let warmStripe = stripe(from: 0, to: Self.warmShare)
        let coolStripe = stripe(from: Self.warmShare, to: Self.colorShare)

        // The color travels one whole round of the clusters in a lap.
        let count = Self.clusters.count
        let travel = loopProgress(over: seconds) * Double(count)

        for n in -3 ... bands + 2 {
            // Which cluster this band belongs to, and whether it is the
            // middle band of its three.
            let cluster = Int((Double(n) / 3).rounded(.down))
            let isMiddle = n - cluster * 3 == 1

            // The cluster's place on the scale, read a little way back along
            // the list as the lap runs, so the color moves down the canvas.
            let at = Double(cluster) - travel
            let whole = at.rounded(.down)
            let part = at - whole
            let a = ((Int(whole) % count) + count) % count
            let b = (a + 1) % count
            let place = isMiddle
                ? lerp(Self.middle(of: a), Self.middle(of: b), part)
                : lerp(Self.clusters[a], Self.clusters[b], part)
            let colors = pair(at: place)

            // The band is the first band moved down by whole steps and
            // sideways by its slip. A slip of a whole wave changes nothing,
            // so only the part of a wave is kept.
            let below = Double(n) + Self.firstBand
            var aside = (slip * below).truncatingRemainder(dividingBy: 1)
            if aside < 0 { aside += 1 }
            withState {
                translate(aside * wavelength, below * step)
                fill(colors.warm)
                drawShape(warmStripe)
                fill(colors.cool)
                drawShape(coolStripe)
            }
        }
    }
}
```
