From 9bdb5c9ab164fb1c24b746ce8b58908fa872b884 Mon Sep 17 00:00:00 2001 From: "E. A. Graham Jr." <10370165+EAGrahamJr@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:37:33 -0700 Subject: [PATCH] Fix brightness on colors. Fixes #2 with a couple of minor tweaks and doc updates. --- .../kobots/devices/lighting/PixelBuf.kt | 1 + .../kobots/devices/lighting/WS2811.kt | 40 +++++++++++++++---- version.properties | 6 +-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/crackers/kobots/devices/lighting/PixelBuf.kt b/src/main/kotlin/crackers/kobots/devices/lighting/PixelBuf.kt index ad7298c..2f7207f 100644 --- a/src/main/kotlin/crackers/kobots/devices/lighting/PixelBuf.kt +++ b/src/main/kotlin/crackers/kobots/devices/lighting/PixelBuf.kt @@ -69,6 +69,7 @@ abstract class PixelBuf( get() = _brightness set(b) { if (b < 0f || b > 1f) throw IllegalArgumentException("'brightness' is out of range (0.0-1.0)") + _brightness = b val currentAutoWrite = _autoWrite _autoWrite = false currentColors.clone().forEachIndexed { i, c -> set(i, PixelColor(c.color, c.white, b)) } diff --git a/src/main/kotlin/crackers/kobots/devices/lighting/WS2811.kt b/src/main/kotlin/crackers/kobots/devices/lighting/WS2811.kt index 48fa82d..83ab72b 100644 --- a/src/main/kotlin/crackers/kobots/devices/lighting/WS2811.kt +++ b/src/main/kotlin/crackers/kobots/devices/lighting/WS2811.kt @@ -20,6 +20,14 @@ import java.awt.Color /** * Defines [WS2811](https://cdn-shop.adafruit.com/datasheets/WS2811.pdf) aka **NeoPixels**. + * + * **NOTE:** when using Java `Color` settings, the [brightness] of the device will be applied to the colors. Example: + * ``` + * p.brightness = .1 + * p.color = Color.RED + * ``` + * will result in an "actual" color of 25 (.1 * 255). This _will_ conflict with adjusting the brightness of colors + * "manually" (e.g. if the value is 25 and brightness is .1, the _actual_ value will be 2). */ interface WS2811 { /** @@ -47,28 +55,40 @@ interface WS2811 { infix fun fill(color: PixelColor) /** - * Fill the entire device with this color. If [autoWrite] is enabled, the results are immediately uploaded. + * Fill the entire device with this color. If [autoWrite] is enabled, the results are immediately uploaded. The + * current [brightness] level is also applied. */ infix fun fill(color: Color) { - fill(PixelColor(color)) - } - - operator fun plus(color: Color) { - fill(PixelColor(color)) + fill(PixelColor(color, brightness = brightness)) } + /** + * Fill the entire device with this color. If [autoWrite] is enabled, the results are immediately uploaded. + */ operator fun plus(color: PixelColor) { fill(color) } + /** + * Fill the entire device with this color. If [autoWrite] is enabled, the results are immediately uploaded. The + * current [brightness] level is also applied. + */ + operator fun plus(color: Color) { + fill(PixelColor(color, brightness = brightness)) + } + /** * Set an individual pixel (this is available as an _indexed_ value). If [autoWrite] is enabled, the results are * immediately uploaded. */ operator fun set(index: Int, color: PixelColor) + /** + * Set an individual pixel (this is available as an _indexed_ value). If [autoWrite] is enabled, the results are + * immediately uploaded.The current [brightness] level is also applied. + */ operator fun set(index: Int, color: Color) { - set(index, PixelColor(color)) + set(index, PixelColor(color, brightness = brightness)) } /** @@ -76,8 +96,12 @@ interface WS2811 { */ operator fun set(start: Int, end: Int, color: PixelColor) + /** + * Set a range of pixels to a color. If [autoWrite] is enabled, the results are immediately uploaded. The + * current [brightness] level is also applied. + */ operator fun set(start: Int, end: Int, color: Color) { - set(start, end, PixelColor(color)) + set(start, end, PixelColor(color, brightness = brightness)) } /** diff --git a/version.properties b/version.properties index 7b5897f..e423b21 100644 --- a/version.properties +++ b/version.properties @@ -1,8 +1,8 @@ #Generated by the Semver Plugin for Gradle -#Sun Mar 31 10:06:32 PDT 2024 +#Sun May 19 13:53:02 PDT 2024 version.buildmeta= version.major=0 version.minor=2 -version.patch=7 +version.patch=8 version.prerelease= -version.semver=0.2.7 +version.semver=0.2.8