arthur/frontend/src/arthur/domain/palette.cljs
Olive Vaughn 1a6d6564c5 One palette per frame, chosen by the shallowest override in the open tab
A frame is drawn in exactly one palette, or one blend of two while a palette
lane tweens. Shapes store slots and the raster index is the slot, so the
palette is only the ramp a frame is expanded through: there is no way for two
palettes' colours to share a frame. The per-palette index banks are gone.

Every symbol drawn says which palette it chooses — its lane, its instance's
palette, its own — and the shallowest wins, so a palette set on the open
symbol beats anything inside it, and a child set to one turns the whole frame
while it is drawn. Nothing chosen is the project's palette.

The palette grid follows the palette the stage is in, per tab, read from the
player rather than worked out again; mid-tween it holds the start palette.
Another can be opened per tab, with a button back to the active one.

The chooser puts the palettes the open symbol uses first, saying where, then
the project's, then palettes made in other projects (with their project, and
searchable by either name), each with the playhead's frame drawn in it;
hovering previews on the stage, `use` sets the symbol to it. The classics are
a folded shelf, now with VGA, Commodore 64, CGA and Workbench 1.3, and a new
project carries only the palette it was made with.

Unused slots are marked; solo shows and lets you click only what is drawn in
the chosen colour; ⌫ empties the open palette's unused slots in place.
The project palette can be changed in the inspector. The palette lane's
label no longer collides with the sidebar grid's styles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 19:47:44 -04:00

254 lines
11 KiB
Clojure

(ns arthur.domain.palette
"Project palette assets and their render-time index banks.
Drawing data stores a dumb LOCAL SLOT NUMBER. A symbol or instance supplies
the palette context. `compile` gives project palettes disjoint ranges in the
raster index space, so differently-paletted subtrees can coexist. The ranges
are derived, never persisted: adding a palette never rewrites drawing data.")
(def default-id :arthur/default)
(def inherit :arthur.palette/inherit)
(def entries
[{:name :bg :hex "#12141c"}
{:name :skin-base :hex "#b07a5a"}
{:name :skin-dark :hex "#7a4f3a"}
{:name :mouth-dark :hex "#24161a"}
{:name :teeth :hex "#d9cfc2"}
;; Sclera is not white, and that is authored, not measured. A true white at
;; 320x200 next to a warm skin ramp reads as a hole punched in the face; the
;; eye sits in a socket, in shadow, so it is a dimmer and cooler tone than the
;; teeth, which catch the light.
{:name :eye-white :hex "#c9c3b4"}
;; Three tones for the eye - sclera, iris, pupil - which is the "two or three
;; tones per part" budget, spent where it buys the most: an eye with no tonal
;; step inside it reads as a hole.
{:name :iris :hex "#4a5468"}
{:name :pupil :hex "#171a22"}
;; Brows get their own entry rather than sharing skin-dark with the lash line.
;; They are hair, not shadow: when hair plates exist they want to match those,
;; and tying them to the lash means you cannot change one without the other.
{:name :brow :hex "#3a2a22"}])
(def hexes (mapv :hex entries))
(def index-of
"Palette keyword -> the index the raster writes. Derived, so the vector above
is the single place an ordering is declared."
(into {} (map-indexed (fn [i e] [(:name e) i]) entries)))
(defn hex->rgb [hex]
(let [s (.replace hex "#" "")]
[(js/parseInt (.slice s 0 2) 16)
(js/parseInt (.slice s 2 4) 16)
(js/parseInt (.slice s 4 6) 16)]))
(def rgb
"Index -> [r g b], precomputed."
(mapv hex->rgb hexes))
(def default-palette
{:id default-id :name "Arthur"
:slots (into entries (repeat 7 {:hex "#000000"}))})
(defn- preset
"A palette from nine face tones in `entries` order and seven free ones. The
order is the point: a measured mouth draws in slot 3 and an iris in slot 6
whatever palette is on, so a preset that moved them would recolour a face
into nonsense rather than restyle it."
[id label [bg skin-base skin-dark mouth-dark teeth eye-white iris pupil brow] free]
{:id id :name label
:slots (into (mapv (fn [e hex] {:name (:name e) :hex hex})
entries [bg skin-base skin-dark mouth-dark teeth eye-white iris pupil brow])
(map (fn [hex] {:hex hex}))
free)})
(def presets
"Palettes borrowed from worlds that already work in sixteen colours or fewer:
a shelf to start a project from or add to one, kept out of the project until
then. A sixteen-colour machine's palette is all sixteen of its colours, each
once, the face tones picked from them; a four-colour one repeats, and a face
in it reads by value alone, as on a Game Boy."
[default-palette
;; The sixteen colours of the IBM PC's text modes, which VGA kept from EGA
;; and CGA: the DOS blue behind, the light red and brown every EGA face was.
(preset :arthur/vga "VGA"
["#0000aa" "#ff5555" "#aa5500" "#aa0000" "#ffffff" "#aaaaaa" "#00aaaa" "#000000" "#555555"]
["#00aa00" "#aa00aa" "#55ff55" "#55ffff" "#ff55ff" "#ffff55" "#5555ff"])
;; The Commodore 64's VIC-II as Pepto measured it, on its own blue screen.
(preset :arthur/c64 "Commodore 64"
["#352879" "#9a6759" "#6f4f25" "#68372b" "#ffffff" "#959595" "#70a4b2" "#000000" "#433900"]
["#6f3d86" "#588d43" "#b8c76f" "#444444" "#6c6c6c" "#9ad284" "#6c5eb5"])
;; CGA's four-colour mode, palette 1 at high intensity: magenta skin shaded
;; in cyan, as it was.
(preset :arthur/cga "CGA"
["#000000" "#ff55ff" "#55ffff" "#000000" "#ffffff" "#ffffff" "#55ffff" "#000000" "#000000"]
["#ff55ff" "#55ffff" "#ffffff" "#000000" "#ff55ff" "#55ffff" "#ffffff"])
;; The Amiga's Workbench 1.3: four colours, blue, white, black and orange.
(preset :arthur/workbench "Workbench 1.3"
["#0055aa" "#ff8800" "#000022" "#000022" "#ffffff" "#ffffff" "#0055aa" "#000022" "#000022"]
["#0055aa" "#ffffff" "#000022" "#ff8800" "#0055aa" "#ffffff" "#ff8800"])
;; Lexaloffle's PICO-8, all sixteen, its peach and brown as the skin ramp.
(preset :arthur/pico-8 "PICO-8"
["#1d2b53" "#ffccaa" "#ab5236" "#7e2553" "#fff1e8" "#c2c3c7" "#29adff" "#000000" "#5f574f"]
["#ff004d" "#ffa300" "#ffec27" "#00e436" "#008751" "#ff77a8" "#83769c"])
;; GrafxKid's Sweetie 16.
(preset :arthur/sweetie-16 "Sweetie 16"
["#1a1c2c" "#ffcd75" "#ef7d57" "#5d275d" "#f4f4f4" "#94b0c2" "#3b5dc9" "#333c57" "#566c86"]
["#b13e53" "#a7f070" "#38b764" "#257179" "#29366f" "#41a6f6" "#73eff7"])
;; The original Game Boy's four greens, lightest behind. Everything is one of
;; four, so the face reads by value alone.
(preset :arthur/dmg "Game Boy"
["#9bbc0f" "#8bac0f" "#306230" "#0f380f" "#9bbc0f" "#9bbc0f" "#306230" "#0f380f" "#0f380f"]
["#0f380f" "#306230" "#8bac0f" "#9bbc0f" "#0f380f" "#306230" "#8bac0f"])
;; Rubber-hose cartoons, on paper gone yellow.
(preset :arthur/sepia "Sepia"
["#f2e6c9" "#e8c39e" "#b98b63" "#4a2c1d" "#fbf4e2" "#f3ead6" "#6b4a2f" "#241610" "#4a2c1d"]
["#8c2f1f" "#c8792c" "#d9b44a" "#6f7d3c" "#3e5a6b" "#a39b8b" "#2b2420"])
;; Ink and a red, for line tests and noir.
(preset :arthur/ink "Ink"
["#101010" "#d8d2c4" "#8a857a" "#2a2a2a" "#f0ece2" "#c8c3b6" "#4a4a4a" "#0a0a0a" "#2a2a2a"]
["#b3261e" "#e4e0d6" "#bdb8ad" "#9e998f" "#6d6961" "#4f4c46" "#1c1c1c"])])
(defn palettes [clip]
(if (seq (:palettes clip)) (:palettes clip) {default-id default-palette}))
(defn default-palette-id [clip]
(let [ps (palettes clip)]
(or (:default-palette clip)
(when (contains? ps default-id) default-id)
(first (sort-by str (keys ps))))))
;; ONE PALETTE PER FRAME
;;
;; A frame is drawn in exactly one palette, or one blend of two while a palette
;; lane tweens. Drawing data stores slot numbers, so the raster index IS the
;; slot, and the palette is only the ramp the raster is expanded through. Which
;; palette a frame is in is decided by the open symbol's tree; see
;; `clip/resolver`.
(def bg-slot
"Slot zero is what a frame is cleared to, in whichever palette it is in."
0)
(defn slot
"A shape's colour as a raster index: its slot number, or a legacy tone
keyword's place in `entries`. Anything else is 255, the conspicuous
bad-data sentinel, which the ramp has no colour for and so shows MAGENTA."
[value]
(cond
(and (integer? value) (<= 0 value) (< value 255)) value
;; Compatibility for existing documents. New drawing data is numeric.
(keyword? value) (get index-of value 255)
:else 255))
(defn choices
"The palette ids a palette setting can name: the one it holds, or each its
keys do. `inherit` is not one."
[x]
(remove #(= inherit %)
(cond
(nil? x) []
(and (map? x) (:keys x)) (vals (:keys x))
(and (map? x) (contains? x :value)) [(:value x)]
:else [x])))
(defn compile
"The project's palettes and the one a frame falls back to, for a renderer."
[clip]
{:palettes (palettes clip)
:default (default-palette-id clip)})
(defn ramp-of
"A strip of hex colours as the ramp a raster is expanded through."
[hexes]
(mapv hex->rgb hexes))
(defn effective-ramp
"The ramp of the active palette: an id, or `{:from :to :t}` while a palette
lane blends, which is one pseudopalette mixed slot by slot. A slot only one
end has keeps its colour."
[{:keys [palettes default]} active]
(let [ramp #(ramp-of (map :hex (:slots (get palettes %))))]
(if-let [{:keys [from to t]} (when (map? active) active)]
(let [a (ramp from) b (ramp to)]
(if (number? t)
(into (mapv (fn [x y] (mapv #(js/Math.round (+ %1 (* t (- %2 %1)))) x y)) a b)
(drop (count b) a))
a))
(ramp (if (contains? palettes active) active default)))))
(defn solo-ramp
"`ramp` with every slot but `shown` faded most of the way to the background,
so what is drawn in `shown` is all that stands out."
[ramp shown]
(let [bg (get ramp bg-slot [0 0 0])
fade (fn [c] (mapv (fn [x b] (js/Math.round (+ b (* 0.2 (- x b))))) c bg))]
(into [] (map-indexed (fn [i c] (if (= i shown) c (fade c)))) ramp)))
;; ---------------------------------------------------------------------------
;; which slots drawing uses
;;
;; A slot number means the same place in EVERY palette — that is what lets one
;; drawing be shown in any of them — so a slot is in use when any shape names
;; it, whichever palette it is seen in.
(defn- colour-slots
"The slots a colour channel names at any key: a slot, a legacy tone, or both
sides of a remap's map. A knockout names none."
[ch]
(let [values (if (:keys ch) (vals (:keys ch)) [(:value ch)])]
(into #{}
(comp (mapcat (fn [v]
(cond
(and (vector? v) (= :remap (first v))) (mapcat identity (second v))
(= :clear v) []
:else [v])))
(map slot)
(remove #{255}))
values)))
(defn slots-used
"Every slot a shape in `symbols` names."
[symbols]
(into #{}
(mapcat #(some-> (get-in % [:channels [:style :color]]) colour-slots))
(mapcat (comp vals :nodes) symbols)))
(def empty-slot
"A slot with nothing in it: black, unnamed, as a new palette's free ones are."
{:hex "#000000"})
(defn emptyable
"The slots of palette `p` that can be emptied: none of `used` names them, and
they are not already empty. Slot zero is the background, and a NAMED slot is
a face tone measurement draws in whether or not anything uses it yet."
[p used]
(into []
(keep-indexed (fn [i s] (when-not (or (contains? used i) (= bg-slot i) (:name s)
(= empty-slot (select-keys s [:hex :name])))
i)))
(:slots p)))
(defn empty-slots
"Palette `p` with slots `is` emptied. They stay where they are, so no
drawing's slot number changes and the palette keeps its size."
[p is]
(reduce #(assoc-in %1 [:slots %2] empty-slot) p is))
(defn hexes-of [p] (mapv :hex (:slots p)))
(defn from-hexes
"A palette asset from a strip of colours, its slots named as `entries` are so
a legacy tone keyword still finds its slot."
[id label hexes]
{:id id :name label
:slots (into (mapv (fn [e hex] {:name (:name e) :hex hex}) entries hexes)
(map (fn [hex] {:hex hex}))
(drop (count entries) hexes))})
(defn valid-palette? [{:keys [id name slots]}]
(and id (string? name) (seq name) (vector? slots) (pos? (count slots))
(<= (count slots) 255)
(every? #(and (string? (:hex %))
(boolean (re-matches #"#[0-9a-fA-F]{6}" (:hex %)))) slots)))