From 3e744b7a8d5911187b613f7c474b607e74a41059 Mon Sep 17 00:00:00 2001 From: Guillaume Jenkins Date: Tue, 4 Sep 2018 17:12:01 -0400 Subject: [PATCH] Sim: greyscale display now respects brightness (#1160) * Sim: greyscale display now respects brightness * Don't overwrite image * Don't clamp if brightness is at max --- sim/state/ledmatrix.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/sim/state/ledmatrix.ts b/sim/state/ledmatrix.ts index 577e6a9b..143c81f2 100644 --- a/sim/state/ledmatrix.ts +++ b/sim/state/ledmatrix.ts @@ -134,6 +134,7 @@ namespace pxsim.ImageMethods { let cb = getResume(); let first = true; + leds = clampPixelBrightness(leds); board().ledMatrixState.animationQ.enqueue({ interval, frame: () => { @@ -151,6 +152,7 @@ namespace pxsim.ImageMethods { export function plotImage(leds: Image, offset: number): void { pxtrt.nullCheck(leds) + leds = clampPixelBrightness(leds); board().ledMatrixState.animationQ.enqueue({ interval: 0, frame: () => { @@ -211,6 +213,7 @@ namespace pxsim.ImageMethods { let off = stride > 0 ? 0 : leds.width - 1; let display = board().ledMatrixState.image; + leds = clampPixelBrightness(leds); board().ledMatrixState.animationQ.enqueue({ interval: interval, frame: () => { @@ -230,6 +233,22 @@ namespace pxsim.ImageMethods { whenDone: cb }) } + + function clampPixelBrightness(img: Image): Image { + let res = img; + if (led.displayMode() === DisplayMode.greyscale && led.brightness() < 0xff) { + res = new Image(img.width, img.data); + const b = led.brightness(); + for (let x = 0; x < res.width; ++x) { + for (let y = 0; y < 5; ++y) { + if (pixelBrightness(res, x, y) > b) { + setPixelBrightness(res, x, y, b); + } + } + } + } + return res; + } } namespace pxsim.basic { @@ -280,7 +299,7 @@ namespace pxsim.led { export function plotBrightness(x: number, y: number, brightness: number) { const state = board().ledMatrixState; - brightness = Math.max(0, Math.min(0xff, brightness)); + brightness = Math.max(0, Math.min(led.brightness(), brightness)); if (brightness != 0 && brightness != 0xff && state.displayMode != DisplayMode.greyscale) state.displayMode = DisplayMode.greyscale; state.image.set(x, y, brightness);