Skip to content

Commit

Permalink
Fix brightness on colors.
Browse files Browse the repository at this point in the history
Fixes #2 with a couple of minor tweaks and doc updates.
  • Loading branch information
EAGrahamJr committed Jun 2, 2024
1 parent d4f4c7d commit 9bdb5c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
40 changes: 32 additions & 8 deletions src/main/kotlin/crackers/kobots/devices/lighting/WS2811.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -47,37 +55,53 @@ 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))
}

/**
* Set a range of pixels to a color. If [autoWrite] is enabled, the results are immediately uploaded.
*/
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))
}

/**
Expand Down
6 changes: 3 additions & 3 deletions version.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9bdb5c9

Please sign in to comment.