Implemented inverting colors

This commit is contained in:
dankito 2022-03-14 02:30:07 +01:00
parent 66690c4ec3
commit ab64fbdd2d
2 changed files with 32 additions and 14 deletions

View File

@ -41,12 +41,12 @@ open class EpcQrCode(
} }
open fun qrCodeAsString(): String { open fun qrCodeAsString(invertColors: Boolean = false): String {
return EpcQrCodeStringFormatter().asString(this) return EpcQrCodeStringFormatter().asString(this, invertColors)
} }
open fun qrCodeAsSmallString(): String { open fun qrCodeAsSmallString(invertColors: Boolean = false): String {
return EpcQrCodeStringFormatter().asSmallString(this) return EpcQrCodeStringFormatter().asSmallString(this, invertColors)
} }

View File

@ -2,7 +2,21 @@ package net.codinux.banking.epcqrcode
open class EpcQrCodeStringFormatter { 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 height = epcQrCode.height
val width = epcQrCode.width val width = epcQrCode.width
val string = StringBuilder(height * width) val string = StringBuilder(height * width)
@ -10,9 +24,9 @@ open class EpcQrCodeStringFormatter {
for (y in 1 until height) { for (y in 1 until height) {
for (x in 1 until width) { for (x in 1 until width) {
if (isBitSet(epcQrCode, x, y)) { if (isBitSet(epcQrCode, x, y)) {
string.append("██") string.append(bitIsSetColor)
} else { } else {
string.append(" ") string.append(bitIsNotSetColor)
} }
} }
@ -22,7 +36,11 @@ open class EpcQrCodeStringFormatter {
return string.toString() 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 height = epcQrCode.height
val width = epcQrCode.width val width = epcQrCode.width
val string = StringBuilder(height * width) val string = StringBuilder(height * width)
@ -34,15 +52,15 @@ open class EpcQrCodeStringFormatter {
if (currentRowBit == nextRowBit) { if (currentRowBit == nextRowBit) {
if (isBitSet(currentRowBit)) { if (isBitSet(currentRowBit)) {
string.append("") string.append(bitIsSetColor)
} else { } else {
string.append(" ") string.append(bitIsNotSetColor)
} }
} else { } else {
if (isBitSet(currentRowBit)) { if (isBitSet(currentRowBit)) {
string.append("") string.append(upperBitSetColor)
} else { } else {
string.append("") string.append(lowerBitSetColor)
} }
} }
} }
@ -54,9 +72,9 @@ open class EpcQrCodeStringFormatter {
val y = height - 1 val y = height - 1
for (x in 1 until width) { for (x in 1 until width) {
if (isBitSet(epcQrCode, x, y)) { if (isBitSet(epcQrCode, x, y)) {
string.append("") string.append(upperBitSetColor)
} else { } else {
string.append(" ") string.append(bitIsNotSetColor)
} }
} }