Skip to content

Commit

Permalink
Add "gamma8" color correction and note the passing of the shim.
Browse files Browse the repository at this point in the history
  • Loading branch information
EAGrahamJr committed Sep 30, 2024
1 parent 1eb21a4 commit 84df891
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import java.awt.Color

/**
* Pimoroni LED shim - https://shop.pimoroni.com/products/led-shim?variant=3136952467466
*
* **NOTE** Discontinued.
*/
class PimoroniLEDShim(shimDevice: I2CDeviceInterface = DEFAULT_SHIM_DEVICE) : IS31FL3731(shimDevice) {
override val height = 3
Expand Down
34 changes: 30 additions & 4 deletions src/main/kotlin/crackers/kobots/devices/lighting/PixelBuf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import kotlin.math.roundToInt
* `pixelbuf` implementation.
*
* Note that some difference exist: for example, this relies on the base Java classes to represent colors vs the
* Python `ColorUnion` construct that allows for 3 different types of "colors".
* Python `ColorUnion` construct that allows for 3 different types of "colors". Also, unless otherwise configured,
* the usual `Gamma 8` color-correction is applied.
*
* TODO dotStar implementation is probably incorrect - needs device to test with
*/
Expand All @@ -37,6 +38,7 @@ abstract class PixelBuf(
val byteOrder: String = "BGR",
brightness: Float = 1f,
autoWriteEnabled: Boolean = false,
val applyGamma: Boolean = true,
header: ByteArray? = null,
trailer: ByteArray? = null
) : WS2811 {
Expand All @@ -61,6 +63,30 @@ abstract class PixelBuf(
_autoWrite = b
}

private fun Color.corrected() =
let {
Color(gamma8Correction[it.red], gamma8Correction[it.green], gamma8Correction[it.blue])
}

open val gamma8Correction = listOf(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255,
)

/**
* Manage how bright things are
*/
Expand Down Expand Up @@ -205,6 +231,8 @@ abstract class PixelBuf(
sendBuffer(pixelBuffer)
}

protected fun Byte.brightness(b: Float): Byte = (this.toUByte().toFloat() * b).roundToInt().toByte()

/**
* Sets up an individual pixel element, with whiteness stuff and brightness
*/
Expand All @@ -229,7 +257,7 @@ abstract class PixelBuf(
* Dork with white values based on the setup.
*/
protected fun parseColor(pixelColor: PixelColor): PixelBufColor {
val color = pixelColor.color
val color = pixelColor.color.let { if (applyGamma) it.corrected() else it }
val whiteValue = pixelColor.white
val bright = pixelColor.brightness

Expand Down Expand Up @@ -269,7 +297,5 @@ abstract class PixelBuf(
companion object {
const val DOTSTAR_LED_START_FULL_BRIGHT = 0xFF.toByte()
const val DOTSTAR_LED_START = 0b11100000

fun Byte.brightness(b: Float): Byte = (this.toUByte().toFloat() * b).roundToInt().toByte()
}
}

0 comments on commit 84df891

Please sign in to comment.