Colour
diff --git a/app/components/editor/NodeView.vue b/app/components/editor/NodeView.vue
index 99b1dae..fe2ed29 100644
--- a/app/components/editor/NodeView.vue
+++ b/app/components/editor/NodeView.vue
@@ -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"
/>
@@ -399,7 +403,11 @@ const title = computed(() => {
-
+
+
+ {{ c }}
+
+
diff --git a/app/composables/useEditor.ts b/app/composables/useEditor.ts
index ea3fc68..e3b9726 100644
--- a/app/composables/useEditor.ts
+++ b/app/composables/useEditor.ts
@@ -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,
diff --git a/app/composables/useMenu.ts b/app/composables/useMenu.ts
index 12e1317..a1ad8ee 100644
--- a/app/composables/useMenu.ts
+++ b/app/composables/useMenu.ts
@@ -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({
diff --git a/app/composables/useParts.ts b/app/composables/useParts.ts
index 5eccee9..d1a97a1 100644
--- a/app/composables/useParts.ts
+++ b/app/composables/useParts.ts
@@ -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' },
],
},
{
diff --git a/app/lib/logic/check.ts b/app/lib/logic/check.ts
index 72c6550..3d6dc13 100644
--- a/app/lib/logic/check.ts
+++ b/app/lib/logic/check.ts
@@ -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)
}
diff --git a/app/lib/logic/compile.ts b/app/lib/logic/compile.ts
index bff6404..271c18e 100644
--- a/app/lib/logic/compile.ts
+++ b/app/lib/logic/compile.ts
@@ -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']
diff --git a/app/lib/logic/geometry.ts b/app/lib/logic/geometry.ts
index 5ae2cc4..e65a777 100644
--- a/app/lib/logic/geometry.ts
+++ b/app/lib/logic/geometry.ts
@@ -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':
diff --git a/app/lib/logic/project.ts b/app/lib/logic/project.ts
index b2cec6f..1b9a3da 100644
--- a/app/lib/logic/project.ts
+++ b/app/lib/logic/project.ts
@@ -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
diff --git a/app/lib/logic/types.ts b/app/lib/logic/types.ts
index ac59f23..cfe050e 100644
--- a/app/lib/logic/types.ts
+++ b/app/lib/logic/types.ts
@@ -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[]
}
diff --git a/tests/rgb.test.ts b/tests/rgb.test.ts
new file mode 100644
index 0000000..b98d6e8
--- /dev/null
+++ b/tests/rgb.test.ts
@@ -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'])
+ })
+})