A RGB pixels: three inputs, eight colours, on the canvas and on a component's screen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 12:51:09 +01:00
parent 33318d84ca
commit ef8f97b1c9
13 changed files with 172 additions and 9 deletions

View File

@ -5801,3 +5801,15 @@ button.tile:hover {
.analyzer-head .hint {
margin: 0;
}
.node .pixel-rgb {
fill: #5a6779;
font-size: 7px;
font-weight: 700;
text-anchor: middle;
pointer-events: none;
}
.node .pixel-rgb.dark {
fill: rgb(0 0 0 / 0.55);
}

View File

@ -243,6 +243,13 @@ function pack() {
</div>
</div>
<div v-if="only('pixel')" class="field">
<span>Colours, for all</span>
<div class="seg">
<button @click="ed.setRgb(ids, false)">One</button>
<button @click="ed.setRgb(ids, true)">RGB</button>
</div>
</div>
<label v-if="only('pixel')" class="field" title="Pixels with the same screen name make one screen on the component's box">
<span>Screen on the box, for all</span>
<input

View File

@ -451,7 +451,14 @@ function setRowHz(row: PanelRow, e: Event) {
</label>
</template>
<div v-if="n.kind === 'lamp' || n.kind === 'hex' || n.kind === 'pixel'" class="field">
<div v-if="n.kind === 'pixel'" class="field">
<span>Colours</span>
<div class="seg">
<button :class="{ active: !n.rgb }" title="One input: lit in one colour at 1" @click="ed.setRgb([n.id], false)">One</button>
<button :class="{ active: n.rgb }" title="Three inputs, red, green and blue: eight colours" @click="ed.setRgb([n.id], true)">RGB</button>
</div>
</div>
<div v-if="n.kind === 'lamp' || n.kind === 'hex' || (n.kind === 'pixel' && !n.rgb)" class="field">
<span>Colour</span>
<div class="swatches">
<button class="swatch lamp-swatch default" :class="{ active: !n.color, hex: n.kind === 'hex' }" title="Default" @click="setColor(undefined)" />

View File

@ -2,7 +2,7 @@
import { gateDrawing, sevenSegment } from '~/lib/draw'
import { LANE, nameTags, RIBBON_MAX } from '~/lib/logic/cables'
import { baseOf, gateFamily, inputCount, MAX_GATE_INPUTS } from '~/lib/logic/wide'
import { faceTransform, GRID as G, noteLines, noteScale, PANEL_CONTROL_W, portLines, shapeTransform, type Shape } from '~/lib/logic/geometry'
import { faceTransform, GRID as G, noteLines, noteScale, PANEL_CONTROL_W, portLines, rgbHex, shapeTransform, type Shape } from '~/lib/logic/geometry'
import type { CircuitNode } from '~/lib/logic/types'
const props = defineProps<{
@ -168,6 +168,9 @@ const v = (pin: string) => (props.ghost ? 0 : ed.val(props.node.id, pin))
/** Value inside this chip, for its lamps and displays. */
const fv = (inner: string, pin: string) => (props.ghost ? 0 : ed.faceVal(props.node.id, inner, pin))
const faceHex = (inner: string) => sevenSegment([0, 1, 2, 3].reduce((n, i) => n + (fv(inner, `b${i}`) << i), 0))
/** The colour of an RGB pixel inside this chip ('' while off). */
const facePx = (inner: string) => rgbHex(fv(inner, 'r'), fv(inner, 'g'), fv(inner, 'b'))
const rgbNow = computed(() => (props.node.rgb ? rgbHex(v('r'), v('g'), v('b')) : ''))
const FACE_SCALE = 0.6
const hexValue = computed(() => [0, 1, 2, 3].reduce((n, i) => n + (v(`b${i}`) << i), 0))
const segments = computed(() => sevenSegment(hexValue.value))
@ -251,7 +254,8 @@ const title = computed(() => {
:width="f.px! * G - 0.5"
:height="f.px! * G - 0.5"
class="screen-px"
:class="[p.color ? `px-${p.color}` : '', { on: fv(p.id, 'a') === 1 }]"
:class="p.rgb ? { on: !!facePx(p.id) } : [p.color ? `px-${p.color}` : '', { on: fv(p.id, 'a') === 1 }]"
:style="p.rgb && facePx(p.id) ? { '--px': facePx(p.id) } : undefined"
/>
</template>
<template v-else-if="f.kind === 'lamp'">
@ -399,7 +403,11 @@ const title = computed(() => {
<!-- Pixel: one square of a screen -->
<template v-else-if="node.kind === 'pixel'">
<rect x="1" :y="b.y * G + 1" :width="G - 2" :height="G - 2" rx="1" class="pixel" :class="[node.color ? `px-${node.color}` : '', { on: v('a') === 1 }]" />
<template v-if="node.rgb">
<rect x="1" :y="b.y * G + 1" :width="G - 2" :height="b.h * G - 2" rx="1" class="pixel" :class="{ on: !!rgbNow }" :style="rgbNow ? { '--px': rgbNow } : undefined" />
<text v-for="(c, i) in 'RGB'" :key="c" :x="G / 2" :y="i * G + 3" class="pixel-rgb" :class="{ dark: !!rgbNow }">{{ c }}</text>
</template>
<rect v-else x="1" :y="b.y * G + 1" :width="G - 2" :height="G - 2" rx="1" class="pixel" :class="[node.color ? `px-${node.color}` : '', { on: v('a') === 1 }]" />
</template>
<!-- Input panel: a switch, button or clock per row -->

View File

@ -801,6 +801,8 @@ function create() {
if (p.kind === 'ref') node.label = def.value.nodes.filter(n => n.kind === 'tag' && n.label?.trim()).at(-1)?.label?.trim() ?? ''
if (p.kind === 'panel' || p.kind === 'outpanel') node.rows = newPanelRows()
if (p.kind === 'bundle' || p.kind === 'split') node.bits = 4
// A new pixel is like the one placed last: building a colour screen, every pixel comes in colour.
if (p.kind === 'pixel' && def.value.nodes.findLast(n => n.kind === 'pixel')?.rgb) node.rgb = true
def.value.nodes.push(node)
rememberPart(p.kind, p.chip)
const swap = (r: PinRef) => (r.node === 'ghost' ? { node: node.id, pin: r.pin } : r)
@ -1297,6 +1299,34 @@ function create() {
commit(false)
}
/**
* Pixels in colour (three inputs, red, green, blue) or one colour (one
* input). The wire into a one-colour pixel feeds all three (white); back,
* the red wire stays.
*/
function setRgb(ids: string[], on: boolean) {
const d = def.value
for (const n of nodesById(ids)) {
if (n.kind !== 'pixel' || !!n.rgb === on) continue
const into = d.wires.filter(w => w.to.node === n.id)
if (on) {
n.rgb = true
delete n.color
const w = into.find(x => x.to.pin === 'a')
if (w) {
w.to = { node: n.id, pin: 'r' }
for (const pin of ['g', 'b']) d.wires.push({ id: uid('w'), from: { ...w.from }, to: { node: n.id, pin } })
}
} else {
delete n.rgb
const keep = into.find(x => x.to.pin === 'r') ?? into[0]
d.wires = d.wires.filter(x => x.to.node !== n.id || x === keep)
if (keep) keep.to = { node: n.id, pin: 'a' }
}
}
commit()
}
/** Every row of an input panel the same kind: all switches, all buttons or all clocks. */
function setRowsType(panel: CircuitNode, type: PanelControl) {
for (const r of panel.rows ?? []) {
@ -2845,6 +2875,7 @@ function create() {
combineSelected,
setRowsType,
setScreen,
setRgb,
screen,
menuPage,
challengesOpen,

View File

@ -193,7 +193,8 @@ export function useMenu() {
})
}
}
if (n.kind === 'lamp' || n.kind === 'hex' || n.kind === 'pixel') {
if (n.kind === 'pixel') out.push(n.rgb ? { label: 'One colour (one input)', icon: 'lucide:square', run: () => ed.setRgb([n.id], false) } : { label: 'RGB (red, green, blue inputs)', icon: 'lucide:palette', run: () => ed.setRgb([n.id], true) })
if (n.kind === 'lamp' || n.kind === 'hex' || (n.kind === 'pixel' && !n.rgb)) {
out.push({
label: 'Colour',
swatches: [undefined, ...LAMP_COLORS].map(c => ({
@ -358,6 +359,12 @@ export function useMenu() {
}
const swaps = swapChoices(nodes)
if (swaps.length) out.push({ label: 'Change all to', icon: 'lucide:replace', children: swaps })
if (nodes.every(n => n.kind === 'pixel')) {
out.push({ label: 'Colours', icon: 'lucide:palette', children: [
{ label: 'One colour', checked: nodes.every(n => !n.rgb), run: () => ed.setRgb(ids, false) },
{ label: 'RGB', checked: nodes.every(n => n.rgb), run: () => ed.setRgb(ids, true) },
] })
}
if (nodes.every(n => n.kind === 'lamp' || n.kind === 'hex' || n.kind === 'pixel')) {
const now = new Set(nodes.map(n => n.color))
out.push({

View File

@ -41,7 +41,7 @@ export const BASIC_GROUPS: { name: string; parts: BasicPart[] }[] = [
{ kind: 'lamp', name: 'Lamp', icon: 'lucide:lightbulb', key: 'L', hint: 'Lights up at 1.', aka: 'led light' },
{ kind: 'outpanel', name: 'Output panel', icon: 'lucide:list-checks', hint: 'A column of named lamps. Inside a component its rows are output pins.', aka: 'lamps leds' },
{ kind: 'hex', name: 'Hex display', icon: 'lucide:binary', key: 'H', hint: 'Shows 4 bits as 0–F, and passes them on.', aka: 'seven segment 7seg digit' },
{ kind: 'pixel', name: 'Pixel', icon: 'lucide:square', key: 'X', hint: 'One square of a screen, lit at 1. Pixels inside a component show on its box as screens.', aka: 'screen lcd' },
{ kind: 'pixel', name: 'Pixel', icon: 'lucide:square', key: 'X', hint: 'One square of a screen, lit at 1, or in colour with red, green and blue inputs. Pixels inside a component show on its box as screens.', aka: 'screen lcd rgb colour color monitor' },
],
},
{

View File

@ -33,6 +33,8 @@ export function openInputs(def: ComponentDef, n: CircuitNode, get: DefLookup): s
if (!CHECKED_INPUTS.has(n.kind)) return []
const linked = linkedPins(def, n)
const wired = new Set(def.wires.filter(w => w.to.node === n.id).map(w => w.to.pin))
// An RGB pixel may use only some of its colours.
if (n.kind === 'pixel' && n.rgb && wired.size) return []
return shapeOf(n, get).inputs.filter(p => !p.bus && !(p.port && linked.has(p.port)) && !wired.has(p.key)).map(p => p.key)
}

View File

@ -44,9 +44,10 @@ export function inputKeys(node: CircuitNode, pins?: ChipPins, get?: DefLookup):
return ['bus', ...(get ? portLines(node, get).filter(l => !l.enters).map(l => l.key) : [])]
case 'nand':
return ['a', 'b']
case 'pixel':
return node.rgb ? ['r', 'g', 'b'] : ['a']
case 'output':
case 'lamp':
case 'pixel':
case 'split':
case 'tag':
return ['a']

View File

@ -36,9 +36,15 @@ export interface Shape {
drawing?: FaceShape[]
}
/** What an RGB pixel shows for its red, green and blue inputs (index r + 2g + 4b); black is off. */
export const RGB_HEX = ['', '#ff4d4d', '#3ddc84', '#ffe04d', '#4d7dff', '#ff5cf0', '#4de8ff', '#f2f6fa'] as const
export const rgbHex = (r: number, g: number, b: number) => RGB_HEX[(r ? 1 : 0) + (g ? 2 : 0) + (b ? 4 : 0)]!
export interface ScreenPixel {
/** Pixel node id inside the component. */
id: string
/** Lit by its red, green and blue inputs. */
rgb?: boolean
col: number
row: number
color?: LampColor
@ -391,7 +397,7 @@ function screenOf(pixels: CircuitNode[], name: string): FaceItem {
w: xs.length * px,
h: ys.length * px,
px,
pixels: pixels.map(n => ({ id: n.id, col: col.get(n.x)!, row: row.get(n.y)!, color: n.color })),
pixels: pixels.map(n => ({ id: n.id, col: col.get(n.x)!, row: row.get(n.y)!, color: n.color, ...(n.rgb ? { rgb: true } : {}) })),
}
}
@ -704,6 +710,16 @@ const PIXEL_SHAPE: Shape = {
outputs: [],
}
const PIXEL_RGB_SHAPE: Shape = {
body: { x: 0, y: -0.5, w: 1, h: 3 },
inputs: [
{ key: 'r', label: '', dx: 0, dy: 0 },
{ key: 'g', label: '', dx: 0, dy: 1 },
{ key: 'b', label: '', dx: 0, dy: 2 },
],
outputs: [],
}
/** Grid offset of every panel row below the first one. */
export function panelOffsets(rows: PanelRow[]): number[] {
const out: number[] = []
@ -799,7 +815,7 @@ export function shapeOf(node: CircuitNode, getDef: DefLookup): Shape {
case 'split':
return cableShape(node)
case 'pixel':
return PIXEL_SHAPE
return node.rgb ? PIXEL_RGB_SHAPE : PIXEL_SHAPE
case 'outpanel':
return outPanelShape(node)
case 'tag':

View File

@ -46,6 +46,7 @@ function cleanNode(n: CircuitNode) {
}
if (n.inner !== undefined && n.inner !== true) delete n.inner
if (n.screen !== undefined && typeof n.screen !== 'string') delete n.screen
if (n.rgb !== undefined && n.rgb !== true) delete n.rgb
}
const MAX_ROWS = 256

View File

@ -74,6 +74,8 @@ export interface CircuitNode {
inner?: boolean
/** Pixel: the screen it belongs to on the component's box; pixels without one make the main screen. */
screen?: string
/** Pixel with three inputs, red, green and blue: eight colours. */
rgb?: boolean
/** Component: its connector ports (port node ids inside) shown split on this box, with a pin per line to wire by hand. */
splitPorts?: string[]
}

69
tests/rgb.test.ts Normal file
View File

@ -0,0 +1,69 @@
import { describe, expect, it } from 'vitest'
import { openInputs } from '../app/lib/logic/check'
import { compile } from '../app/lib/logic/compile'
import { rgbHex, screenLayout, shapeOf } from '../app/lib/logic/geometry'
import { createProject, lookup } from '../app/lib/logic/ops'
import { Simulator } from '../app/lib/logic/sim'
import type { ComponentDef } from '../app/lib/logic/types'
/** A component with one RGB pixel, its red and blue inputs from two input pins; placed in a circuit with two switches. */
function setup() {
const p = createProject({ examples: false })
const inner: ComponentDef = {
id: 'mon',
name: 'Monitor',
symbol: 'box',
nodes: [
{ id: 'R', kind: 'input', x: 0, y: 0, label: 'R' },
{ id: 'B', kind: 'input', x: 0, y: 2, label: 'B' },
{ id: 'px', kind: 'pixel', x: 10, y: 0, rgb: true },
],
wires: [
{ id: 'w1', from: { node: 'R', pin: 'q' }, to: { node: 'px', pin: 'r' } },
{ id: 'w2', from: { node: 'B', pin: 'q' }, to: { node: 'px', pin: 'b' } },
],
}
const root: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 's1', kind: 'input', x: 0, y: 0 },
{ id: 's2', kind: 'input', x: 0, y: 2 },
{ id: 'm', kind: 'chip', chip: 'mon', x: 10, y: 0 },
],
wires: [
{ id: 'a', from: { node: 's1', pin: 'q' }, to: { node: 'm', pin: 'R' } },
{ id: 'b', from: { node: 's2', pin: 'q' }, to: { node: 'm', pin: 'B' } },
],
}
p.components.push(inner, root)
return { p, inner, root }
}
describe('RGB pixels', () => {
it('have red, green and blue inputs and mix eight colours', () => {
const { p, inner } = setup()
expect(shapeOf(inner.nodes[2]!, lookup(p)).inputs.map(i => i.key)).toEqual(['r', 'g', 'b'])
expect(rgbHex(0, 0, 0)).toBe('')
expect(rgbHex(1, 0, 1)).toBe('#ff5cf0')
expect(rgbHex(1, 1, 1)).toBe('#f2f6fa')
// Green left open on purpose: no warning.
expect(openInputs(inner, inner.nodes[2]!, lookup(p))).toEqual([])
})
it('show their colour on the screen of the component they are in', () => {
const { p, inner, root } = setup()
expect(screenLayout(inner)!.pixels).toEqual([{ id: 'px', col: 0, row: 0, color: undefined, rgb: true }])
const s = new Simulator(compile(lookup(p), root.id))
s.setSource('s1', true)
s.setSource('s2', true)
s.run()
expect([s.face('m', 'px', 'r'), s.face('m', 'px', 'g'), s.face('m', 'px', 'b')]).toEqual([1, 0, 1])
})
it('a one-colour pixel keeps its single input', () => {
const { p } = setup()
expect(shapeOf({ id: 'x', kind: 'pixel', x: 0, y: 0 }, lookup(p)).inputs.map(i => i.key)).toEqual(['a'])
})
})