From ab64fbdd2dab56c6db839f68b466284fb7698d27 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 14 Mar 2022 02:30:07 +0100 Subject: [PATCH] Implemented inverting colors --- .../codinux/banking/epcqrcode/EpcQrCode.kt | 8 ++-- .../epcqrcode/EpcQrCodeStringFormatter.kt | 38 ++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt index ed79c96..6a8dc0b 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -41,12 +41,12 @@ open class EpcQrCode( } - open fun qrCodeAsString(): String { - return EpcQrCodeStringFormatter().asString(this) + open fun qrCodeAsString(invertColors: Boolean = false): String { + return EpcQrCodeStringFormatter().asString(this, invertColors) } - open fun qrCodeAsSmallString(): String { - return EpcQrCodeStringFormatter().asSmallString(this) + open fun qrCodeAsSmallString(invertColors: Boolean = false): String { + return EpcQrCodeStringFormatter().asSmallString(this, invertColors) } diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt index 0b88e94..24afaf3 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt @@ -2,7 +2,21 @@ package net.codinux.banking.epcqrcode open class EpcQrCodeStringFormatter { - open fun asString(epcQrCode: EpcQrCode): String { + companion object { + const val BitIsSetColor = "█" + + const val BitIsNotSetColor = " " + + const val SmallSizeUpperBitSetColor = "▀" + + const val SmallSizeLowerBitSetColor = "▄" + + } + + + open fun asString(epcQrCode: EpcQrCode, invertColors: Boolean = false): String { + val bitIsSetColor = if (invertColors) BitIsNotSetColor + BitIsNotSetColor else BitIsSetColor + BitIsSetColor + val bitIsNotSetColor = if (invertColors) BitIsSetColor + BitIsSetColor else BitIsNotSetColor + BitIsNotSetColor val height = epcQrCode.height val width = epcQrCode.width val string = StringBuilder(height * width) @@ -10,9 +24,9 @@ open class EpcQrCodeStringFormatter { for (y in 1 until height) { for (x in 1 until width) { if (isBitSet(epcQrCode, x, y)) { - string.append("██") + string.append(bitIsSetColor) } else { - string.append(" ") + string.append(bitIsNotSetColor) } } @@ -22,7 +36,11 @@ open class EpcQrCodeStringFormatter { return string.toString() } - open fun asSmallString(epcQrCode: EpcQrCode): String { + open fun asSmallString(epcQrCode: EpcQrCode, invertColors: Boolean = false): String { + val bitIsSetColor = if (invertColors) BitIsNotSetColor else BitIsSetColor + val bitIsNotSetColor = if (invertColors) BitIsSetColor else BitIsNotSetColor + val upperBitSetColor = if (invertColors) SmallSizeLowerBitSetColor else SmallSizeUpperBitSetColor + val lowerBitSetColor = if (invertColors) SmallSizeUpperBitSetColor else SmallSizeLowerBitSetColor val height = epcQrCode.height val width = epcQrCode.width val string = StringBuilder(height * width) @@ -34,15 +52,15 @@ open class EpcQrCodeStringFormatter { if (currentRowBit == nextRowBit) { if (isBitSet(currentRowBit)) { - string.append("█") + string.append(bitIsSetColor) } else { - string.append(" ") + string.append(bitIsNotSetColor) } } else { if (isBitSet(currentRowBit)) { - string.append("▀") + string.append(upperBitSetColor) } else { - string.append("▄") + string.append(lowerBitSetColor) } } } @@ -54,9 +72,9 @@ open class EpcQrCodeStringFormatter { val y = height - 1 for (x in 1 until width) { if (isBitSet(epcQrCode, x, y)) { - string.append("▀") + string.append(upperBitSetColor) } else { - string.append(" ") + string.append(bitIsNotSetColor) } }