From e8e52a74a7ce10de1df697912cf24daf54baf6a6 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Wed, 24 Jul 2024 10:08:05 -0300 Subject: [PATCH 01/12] add functions for color models and colors based on depth in utils package --- utils/depth.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 utils/depth.go diff --git a/utils/depth.go b/utils/depth.go new file mode 100644 index 0000000..0bb051b --- /dev/null +++ b/utils/depth.go @@ -0,0 +1,36 @@ +package utils + +import "image/color" + +func ColorModel(depth int) color.Model { + switch depth { + case 8: + return color.GrayModel + case 24, 32: + return color.RGBAModel + default: + return color.Gray16Model + } +} + +func WhiteColor(depth int) color.Color { + switch depth { + case 8: + return color.Gray{Y: 255} + case 24, 32: + return color.RGBA{255, 255, 255, 255} + default: + return color.White + } +} + +func BlackColor(depth int) color.Color { + switch depth { + case 8: + return color.Gray{Y: 0} + case 24, 32: + return color.RGBA{0, 0, 0, 255} + default: + return color.Black + } +} From f3630e1cd4bfcb2988c5435943192decf718660e Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Wed, 24 Jul 2024 10:25:04 -0300 Subject: [PATCH 02/12] refactor base1DCode methods to use utility functions for color models and colors --- utils/base1dcode.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/utils/base1dcode.go b/utils/base1dcode.go index a335c0c..bd815f0 100644 --- a/utils/base1dcode.go +++ b/utils/base1dcode.go @@ -12,6 +12,7 @@ type base1DCode struct { *BitList kind string content string + depth int } type base1DCodeIntCS struct { @@ -28,7 +29,7 @@ func (c *base1DCode) Metadata() barcode.Metadata { } func (c *base1DCode) ColorModel() color.Model { - return color.Gray16Model + return ColorModel(c.depth) } func (c *base1DCode) Bounds() image.Rectangle { @@ -37,9 +38,9 @@ func (c *base1DCode) Bounds() image.Rectangle { func (c *base1DCode) At(x, y int) color.Color { if c.GetBit(x) { - return color.Black + return BlackColor(c.depth) } - return color.White + return WhiteColor(c.depth) } func (c *base1DCodeIntCS) CheckSum() int { @@ -48,10 +49,20 @@ func (c *base1DCodeIntCS) CheckSum() int { // New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList func New1DCodeIntCheckSum(codeKind, content string, bars *BitList, checksum int) barcode.BarcodeIntCS { - return &base1DCodeIntCS{base1DCode{bars, codeKind, content}, checksum} + return &base1DCodeIntCS{base1DCode{bars, codeKind, content, 16}, checksum} +} + +// New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList +func New1DCodeIntCheckSumWithDepth(codeKind, content string, bars *BitList, checksum int, depth int) barcode.BarcodeIntCS { + return &base1DCodeIntCS{base1DCode{bars, codeKind, content, depth}, checksum} } // New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList func New1DCode(codeKind, content string, bars *BitList) barcode.Barcode { - return &base1DCode{bars, codeKind, content} + return &base1DCode{bars, codeKind, content, 16} +} + +// New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList +func New1DCodeWithDepth(codeKind, content string, bars *BitList, depth int) barcode.Barcode { + return &base1DCode{bars, codeKind, content, depth} } From 87ab0677e310954939dd6704a8f42b4cd81c4ea0 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Wed, 24 Jul 2024 10:29:23 -0300 Subject: [PATCH 03/12] add support for depth in barcode generations - Introduced depth parameter to EncodeWithDepth function to allow generating barcodes with specified color depth. - Updated Encode function to use EncodeWithDepth with a default depth of 16. --- aztec/azteccode.go | 11 ++++++----- aztec/encoder.go | 9 +++++++-- codabar/encoder.go | 9 +++++++-- code128/encode.go | 9 +++++++-- code39/encoder.go | 10 ++++++++-- code93/encoder.go | 12 ++++++++---- datamatrix/codelayout.go | 6 ++++-- datamatrix/datamatrixcode.go | 13 +++++++++---- datamatrix/encoder.go | 13 +++++++++---- ean/encoder.go | 11 ++++++++--- pdf417/encoder.go | 10 +++++++++- pdf417/pdfcode.go | 7 ++++--- qr/encoder.go | 14 +++++++++----- qr/qrcode.go | 14 ++++++++++---- twooffive/encoder.go | 13 +++++++++---- 15 files changed, 114 insertions(+), 47 deletions(-) diff --git a/aztec/azteccode.go b/aztec/azteccode.go index 8ffa7fd..d347b35 100644 --- a/aztec/azteccode.go +++ b/aztec/azteccode.go @@ -13,10 +13,11 @@ type aztecCode struct { *utils.BitList size int content []byte + depth int } -func newAztecCode(size int) *aztecCode { - return &aztecCode{utils.NewBitList(size * size), size, nil} +func newAztecCode(size int, depth int) *aztecCode { + return &aztecCode{utils.NewBitList(size * size), size, nil, 16} } func (c *aztecCode) Content() string { @@ -28,7 +29,7 @@ func (c *aztecCode) Metadata() barcode.Metadata { } func (c *aztecCode) ColorModel() color.Model { - return color.Gray16Model + return utils.ColorModel(c.depth) } func (c *aztecCode) Bounds() image.Rectangle { @@ -37,9 +38,9 @@ func (c *aztecCode) Bounds() image.Rectangle { func (c *aztecCode) At(x, y int) color.Color { if c.GetBit(x*c.size + y) { - return color.Black + return utils.BlackColor(c.depth) } - return color.White + return utils.WhiteColor(c.depth) } func (c *aztecCode) set(x, y int) { diff --git a/aztec/encoder.go b/aztec/encoder.go index e62f8fb..b598d96 100644 --- a/aztec/encoder.go +++ b/aztec/encoder.go @@ -123,7 +123,12 @@ func drawBullsEye(matrix *aztecCode, center, size int) { } // Encode returns an aztec barcode with the given content -func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error) { +func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error){ + return EncodeWithDepth(data, minECCPercent, userSpecifiedLayers, 16) +} + +// Encode returns an aztec barcode with the given content +func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, depth int) (barcode.Barcode, error) { bits := highlevelEncode(data) eccBits := ((bits.Len() * minECCPercent) / 100) + 11 totalSizeBits := bits.Len() + eccBits @@ -215,7 +220,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba alignmentMap[origCenter+i] = center + newOffset + 1 } } - code := newAztecCode(matrixSize) + code := newAztecCode(matrixSize, depth) code.content = data // draw data bits diff --git a/codabar/encoder.go b/codabar/encoder.go index 8e0f3f6..cb365e0 100644 --- a/codabar/encoder.go +++ b/codabar/encoder.go @@ -33,7 +33,7 @@ var encodingTable = map[rune][]bool{ } // Encode creates a codabar barcode for the given content -func Encode(content string) (barcode.Barcode, error) { +func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`) if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" { return nil, fmt.Errorf("can not encode \"%s\"", content) @@ -45,5 +45,10 @@ func Encode(content string) (barcode.Barcode, error) { } resBits.AddBit(encodingTable[r]...) } - return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil + return utils.New1DCodeWithDepth(barcode.TypeCodabar, content, resBits, depth), nil +} + +// Encode creates a codabar barcode for the given content +func Encode(content string) (barcode.Barcode, error) { + return EncodeWithDepth(content, 16) } diff --git a/code128/encode.go b/code128/encode.go index 3a00f6c..a5c7dc6 100644 --- a/code128/encode.go +++ b/code128/encode.go @@ -156,7 +156,7 @@ func getCodeIndexList(content []rune) *utils.BitList { } // Encode creates a Code 128 barcode for the given content -func Encode(content string) (barcode.BarcodeIntCS, error) { +func EncodeWithDepth(content string, depth int) (barcode.BarcodeIntCS, error) { contentRunes := strToRunes(content) if len(contentRunes) <= 0 || len(contentRunes) > 80 { return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes)) @@ -180,7 +180,12 @@ func Encode(content string) (barcode.BarcodeIntCS, error) { sum = sum % 103 result.AddBit(encodingTable[sum]...) result.AddBit(encodingTable[stopSymbol]...) - return utils.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), nil + return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode128, content, result, sum, depth), nil +} + +// Encode creates a Code 128 barcode for the given content +func Encode(content string) (barcode.BarcodeIntCS, error) { + return EncodeWithDepth(content, 16) } func EncodeWithoutChecksum(content string) (barcode.Barcode, error) { diff --git a/code39/encoder.go b/code39/encoder.go index 63b3e71..2a745e1 100644 --- a/code39/encoder.go +++ b/code39/encoder.go @@ -113,7 +113,7 @@ func prepare(content string) (string, error) { // Encode returns a code39 barcode for the given content // if includeChecksum is set to true, a checksum character is calculated and added to the content -func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) { +func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (barcode.BarcodeIntCS, error) { if fullASCIIMode { var err error content, err = prepare(content) @@ -148,5 +148,11 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B if err != nil { checkSum = 0 } - return utils.New1DCodeIntCheckSum(barcode.TypeCode39, content, result, int(checkSum)), nil + return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode39, content, result, int(checkSum), depth), nil +} + +// Encode returns a code39 barcode for the given content +// if includeChecksum is set to true, a checksum character is calculated and added to the content +func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) { + return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16) } diff --git a/code93/encoder.go b/code93/encoder.go index 87c4395..abe6aab 100644 --- a/code93/encoder.go +++ b/code93/encoder.go @@ -74,9 +74,7 @@ func prepare(content string) (string, error) { return result, nil } -// Encode returns a code93 barcode for the given content -// if includeChecksum is set to true, two checksum characters are calculated and added to the content -func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) { +func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (barcode.Barcode, error) { if fullASCIIMode { var err error content, err = prepare(content) @@ -104,7 +102,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B } result.AddBit(true) - return utils.New1DCode(barcode.TypeCode93, content, result), nil + return utils.New1DCodeWithDepth(barcode.TypeCode93, content, result, depth), nil +} + +// Encode returns a code93 barcode for the given content +// if includeChecksum is set to true, two checksum characters are calculated and added to the content +func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) { + return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16) } func getChecksum(content string, maxWeight int) rune { diff --git a/datamatrix/codelayout.go b/datamatrix/codelayout.go index 923b135..97b9fd9 100644 --- a/datamatrix/codelayout.go +++ b/datamatrix/codelayout.go @@ -11,13 +11,15 @@ type codeLayout struct { matrix *utils.BitList occupy *utils.BitList size *dmCodeSize + depth int } -func newCodeLayout(size *dmCodeSize) *codeLayout { +func newCodeLayout(size *dmCodeSize, depth int) *codeLayout { result := new(codeLayout) result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows()) result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows()) result.size = size + result.depth = depth return result } @@ -159,7 +161,7 @@ func (l *codeLayout) SetValues(data []byte) { } func (l *codeLayout) Merge() *datamatrixCode { - result := newDataMatrixCode(l.size) + result := newDataMatrixCodeWithDepth(l.size, l.depth) //dotted horizontal lines for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) { diff --git a/datamatrix/datamatrixcode.go b/datamatrix/datamatrixcode.go index 8f53021..2ae1cf6 100644 --- a/datamatrix/datamatrixcode.go +++ b/datamatrix/datamatrixcode.go @@ -12,10 +12,15 @@ type datamatrixCode struct { *utils.BitList *dmCodeSize content string + depth int +} + +func newDataMatrixCodeWithDepth(size *dmCodeSize, depth int) *datamatrixCode { + return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", depth} } func newDataMatrixCode(size *dmCodeSize) *datamatrixCode { - return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, ""} + return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", 16} } func (c *datamatrixCode) Content() string { @@ -27,7 +32,7 @@ func (c *datamatrixCode) Metadata() barcode.Metadata { } func (c *datamatrixCode) ColorModel() color.Model { - return color.Gray16Model + return utils.ColorModel(c.depth) } func (c *datamatrixCode) Bounds() image.Rectangle { @@ -36,9 +41,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle { func (c *datamatrixCode) At(x, y int) color.Color { if c.get(x, y) { - return color.Black + return utils.BlackColor(c.depth) } - return color.White + return utils.WhiteColor(c.depth) } func (c *datamatrixCode) get(x, y int) bool { diff --git a/datamatrix/encoder.go b/datamatrix/encoder.go index ea852e8..544e0b0 100644 --- a/datamatrix/encoder.go +++ b/datamatrix/encoder.go @@ -8,7 +8,7 @@ import ( ) // Encode returns a Datamatrix barcode for the given content -func Encode(content string) (barcode.Barcode, error) { +func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { data := encodeText(content) var size *dmCodeSize @@ -23,7 +23,7 @@ func Encode(content string) (barcode.Barcode, error) { } data = addPadding(data, size.DataCodewords()) data = ec.calcECC(data, size) - code := render(data, size) + code := render(data, size, depth) if code != nil { code.content = content return code, nil @@ -31,8 +31,13 @@ func Encode(content string) (barcode.Barcode, error) { return nil, errors.New("unable to render barcode") } -func render(data []byte, size *dmCodeSize) *datamatrixCode { - cl := newCodeLayout(size) +// Encode returns a Datamatrix barcode for the given content +func Encode(content string) (barcode.Barcode, error) { + return EncodeWithDepth(content, 16) +} + +func render(data []byte, size *dmCodeSize, depth int) *datamatrixCode { + cl := newCodeLayout(size, depth) cl.SetValues(data) diff --git a/ean/encoder.go b/ean/encoder.go index a2190f1..0a74a42 100644 --- a/ean/encoder.go +++ b/ean/encoder.go @@ -158,7 +158,7 @@ func encodeEAN13(code string) *utils.BitList { } // Encode returns a EAN 8 or EAN 13 barcode for the given code -func Encode(code string) (barcode.BarcodeIntCS, error) { +func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) { var checkSum int if len(code) == 7 || len(code) == 12 { code += string(calcCheckNum(code)) @@ -175,13 +175,18 @@ func Encode(code string) (barcode.BarcodeIntCS, error) { if len(code) == 8 { result := encodeEAN8(code) if result != nil { - return utils.New1DCodeIntCheckSum(barcode.TypeEAN8, code, result, checkSum), nil + return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN8, code, result, checkSum, depth), nil } } else if len(code) == 13 { result := encodeEAN13(code) if result != nil { - return utils.New1DCodeIntCheckSum(barcode.TypeEAN13, code, result, checkSum), nil + return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN13, code, result, checkSum, depth), nil } } return nil, errors.New("invalid ean code data") } + +// Encode returns a EAN 8 or EAN 13 barcode for the given code +func Encode(code string) (barcode.BarcodeIntCS, error) { + return EncodeWithDepth(code, 16) +} diff --git a/pdf417/encoder.go b/pdf417/encoder.go index f7922d4..d09e7d7 100644 --- a/pdf417/encoder.go +++ b/pdf417/encoder.go @@ -15,7 +15,7 @@ const ( // Encodes the given data as PDF417 barcode. // securityLevel should be between 0 and 8. The higher the number, the more // additional error-correction codes are added. -func Encode(data string, securityLevel byte) (barcode.Barcode, error) { +func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcode, error) { if securityLevel >= 9 { return nil, fmt.Errorf("Invalid security level %d", securityLevel) } @@ -34,6 +34,7 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) { barcode := new(pdfBarcode) barcode.data = data + barcode.depth = depth codeWords, err := encodeData(dataWords, columns, sl) if err != nil { @@ -70,6 +71,13 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) { return barcode, nil } +// Encodes the given data as PDF417 barcode. +// securityLevel should be between 0 and 8. The higher the number, the more +// additional error-correction codes are added. +func Encode(data string, securityLevel byte) (barcode.Barcode, error) { + return EncodeWithDepth(data, securityLevel, 16) +} + func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) { dataCount := len(dataWords) diff --git a/pdf417/pdfcode.go b/pdf417/pdfcode.go index a5c47a9..8dd9d17 100644 --- a/pdf417/pdfcode.go +++ b/pdf417/pdfcode.go @@ -12,6 +12,7 @@ type pdfBarcode struct { data string width int code *utils.BitList + depth int } func (c *pdfBarcode) Metadata() barcode.Metadata { @@ -23,7 +24,7 @@ func (c *pdfBarcode) Content() string { } func (c *pdfBarcode) ColorModel() color.Model { - return color.Gray16Model + return utils.ColorModel(c.depth) } func (c *pdfBarcode) Bounds() image.Rectangle { @@ -34,7 +35,7 @@ func (c *pdfBarcode) Bounds() image.Rectangle { func (c *pdfBarcode) At(x, y int) color.Color { if c.code.GetBit((y/moduleHeight)*c.width + x) { - return color.Black + return utils.BlackColor(c.depth) } - return color.White + return utils.WhiteColor(c.depth) } diff --git a/qr/encoder.go b/qr/encoder.go index 2c6ab21..f712d02 100644 --- a/qr/encoder.go +++ b/qr/encoder.go @@ -55,7 +55,7 @@ func (e Encoding) String() string { } // Encode returns a QR barcode with the given content, error correction level and uses the given encoding -func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) { +func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding, depth int) (barcode.Barcode, error) { bits, vi, err := mode.getEncoder()(content, level) if err != nil { return nil, err @@ -63,19 +63,23 @@ func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode. blocks := splitToBlocks(bits.IterateBytes(), vi) data := blocks.interleave(vi) - result := render(data, vi) + result := render(data, vi, depth) result.content = content return result, nil } -func render(data []byte, vi *versionInfo) *qrcode { +func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) { + return EncodeWithDepth(content, level, mode, 16) +} + +func render(data []byte, vi *versionInfo, depth int) *qrcode { dim := vi.modulWidth() results := make([]*qrcode, 8) for i := 0; i < 8; i++ { - results[i] = newBarcode(dim) + results[i] = newBarCodeWithDepth(dim, depth) } - occupied := newBarcode(dim) + occupied := newBarCodeWithDepth(dim, depth) setAll := func(x int, y int, val bool) { occupied.Set(x, y, true) diff --git a/qr/qrcode.go b/qr/qrcode.go index 1360760..ec44da2 100644 --- a/qr/qrcode.go +++ b/qr/qrcode.go @@ -13,6 +13,7 @@ type qrcode struct { dimension int data *utils.BitList content string + depth int } func (qr *qrcode) Content() string { @@ -24,7 +25,7 @@ func (qr *qrcode) Metadata() barcode.Metadata { } func (qr *qrcode) ColorModel() color.Model { - return color.Gray16Model + return utils.ColorModel(qr.depth) } func (qr *qrcode) Bounds() image.Rectangle { @@ -33,9 +34,9 @@ func (qr *qrcode) Bounds() image.Rectangle { func (qr *qrcode) At(x, y int) color.Color { if qr.Get(x, y) { - return color.Black + return utils.BlackColor(qr.depth) } - return color.White + return utils.WhiteColor(qr.depth) } func (qr *qrcode) Get(x, y int) bool { @@ -158,9 +159,14 @@ func (qr *qrcode) calcPenaltyRule4() uint { return uint(math.Min(floor, ceil) * 10) } -func newBarcode(dim int) *qrcode { +func newBarCodeWithDepth(dim int, depth int) *qrcode { res := new(qrcode) res.dimension = dim res.data = utils.NewBitList(dim * dim) + res.depth = depth return res } + +func newBarcode(dim int) *qrcode { + return newBarCodeWithDepth(dim, 16) +} diff --git a/twooffive/encoder.go b/twooffive/encoder.go index 8ad0946..05895ba 100644 --- a/twooffive/encoder.go +++ b/twooffive/encoder.go @@ -78,8 +78,8 @@ func AddCheckSum(content string) (string, error) { return content + string(utils.IntToRune(sum%10)), nil } -// Encode creates a codabar barcode for the given content -func Encode(content string, interleaved bool) (barcode.Barcode, error) { +// Encode creates a codabar barcode for the given content and depth +func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barcode, error) { if content == "" { return nil, errors.New("content is empty") } @@ -131,8 +131,13 @@ func Encode(content string, interleaved bool) (barcode.Barcode, error) { resBits.AddBit(mode.end...) if interleaved { - return utils.New1DCode(barcode.Type2of5Interleaved, content, resBits), nil + return utils.New1DCodeWithDepth(barcode.Type2of5Interleaved, content, resBits, depth), nil } else { - return utils.New1DCode(barcode.Type2of5, content, resBits), nil + return utils.New1DCodeWithDepth(barcode.Type2of5, content, resBits, depth), nil } } + +// Encode creates a codabar barcode for the given content +func Encode(content string, interleaved bool) (barcode.Barcode, error) { + return EncodeWithDepth(content, interleaved, 16) +} From e966fa71060a600aa393af55f6d6607a93960f4f Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Wed, 31 Jul 2024 16:03:52 -0300 Subject: [PATCH 04/12] Add ColorScheme type and predefined color schemes for barcode rendering --- color_scheme.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 color_scheme.go diff --git a/color_scheme.go b/color_scheme.go new file mode 100644 index 0000000..e8ccc7a --- /dev/null +++ b/color_scheme.go @@ -0,0 +1,39 @@ +package barcode + +import "image/color" + +// ColorScheme defines a structure for color schemes used in barcode rendering. +// It includes the color model, background color, and foreground color. +type ColorScheme struct { + Model color.Model // Color model to be used (e.g., grayscale, RGB, RGBA) + Background color.Color // Color of the background + Foreground color.Color // Color of the foreground (e.g., bars in a barcode) +} + +// ColorScheme8 represents a color scheme with 8-bit grayscale colors. +var ColorScheme8 = ColorScheme{ + Model: color.GrayModel, + Background: color.Gray{Y: 255}, + Foreground: color.Gray{Y: 0}, +} + +// ColorScheme16 represents a color scheme with 16-bit grayscale colors. +var ColorScheme16 = ColorScheme{ + Model: color.Gray16Model, + Background: color.White, + Foreground: color.Black, +} + +// ColorScheme24 represents a color scheme with 24-bit RGB colors. +var ColorScheme24 = ColorScheme{ + Model: color.RGBAModel, + Background: color.RGBA{255, 255, 255, 255}, + Foreground: color.RGBA{0, 0, 0, 255}, +} + +// ColorScheme32 represents a color scheme with 32-bit RGBA colors, which is similar to ColorScheme24 but typically includes alpha for transparency. +var ColorScheme32 = ColorScheme{ + Model: color.RGBAModel, + Background: color.RGBA{255, 255, 255, 255}, + Foreground: color.RGBA{0, 0, 0, 255}, +} From 30509e0dd7a14b514b659db7f39645bc19ff6e17 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Wed, 31 Jul 2024 16:11:52 -0300 Subject: [PATCH 05/12] Refactor to use ColorScheme instead of 'depth' approach --- aztec/azteccode.go | 12 ++++++------ aztec/encoder.go | 8 ++++---- codabar/encoder.go | 6 +++--- code128/encode.go | 6 +++--- code39/encoder.go | 6 +++--- code93/encoder.go | 6 +++--- datamatrix/codelayout.go | 12 +++++++----- datamatrix/datamatrixcode.go | 14 +++++++------- datamatrix/encoder.go | 16 ++++++++-------- ean/encoder.go | 8 ++++---- pdf417/encoder.go | 6 +++--- pdf417/pdfcode.go | 8 ++++---- qr/encoder.go | 12 ++++++------ qr/qrcode.go | 14 +++++++------- twooffive/encoder.go | 10 +++++----- utils/base1dcode.go | 20 ++++++++++---------- 16 files changed, 83 insertions(+), 81 deletions(-) diff --git a/aztec/azteccode.go b/aztec/azteccode.go index d347b35..48de252 100644 --- a/aztec/azteccode.go +++ b/aztec/azteccode.go @@ -13,11 +13,11 @@ type aztecCode struct { *utils.BitList size int content []byte - depth int + color barcode.ColorScheme } -func newAztecCode(size int, depth int) *aztecCode { - return &aztecCode{utils.NewBitList(size * size), size, nil, 16} +func newAztecCode(size int, color barcode.ColorScheme) *aztecCode { + return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16} } func (c *aztecCode) Content() string { @@ -29,7 +29,7 @@ func (c *aztecCode) Metadata() barcode.Metadata { } func (c *aztecCode) ColorModel() color.Model { - return utils.ColorModel(c.depth) + return c.color.Model } func (c *aztecCode) Bounds() image.Rectangle { @@ -38,9 +38,9 @@ func (c *aztecCode) Bounds() image.Rectangle { func (c *aztecCode) At(x, y int) color.Color { if c.GetBit(x*c.size + y) { - return utils.BlackColor(c.depth) + return c.color.Foreground } - return utils.WhiteColor(c.depth) + return c.color.Background } func (c *aztecCode) set(x, y int) { diff --git a/aztec/encoder.go b/aztec/encoder.go index b598d96..a7ebc50 100644 --- a/aztec/encoder.go +++ b/aztec/encoder.go @@ -123,12 +123,12 @@ func drawBullsEye(matrix *aztecCode, center, size int) { } // Encode returns an aztec barcode with the given content -func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error){ - return EncodeWithDepth(data, minECCPercent, userSpecifiedLayers, 16) +func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error) { + return EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16) } // Encode returns an aztec barcode with the given content -func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, depth int) (barcode.Barcode, error) { +func EncodeWithColor(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme) (barcode.Barcode, error) { bits := highlevelEncode(data) eccBits := ((bits.Len() * minECCPercent) / 100) + 11 totalSizeBits := bits.Len() + eccBits @@ -220,7 +220,7 @@ func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, de alignmentMap[origCenter+i] = center + newOffset + 1 } } - code := newAztecCode(matrixSize, depth) + code := newAztecCode(matrixSize, color) code.content = data // draw data bits diff --git a/codabar/encoder.go b/codabar/encoder.go index cb365e0..beb0e0e 100644 --- a/codabar/encoder.go +++ b/codabar/encoder.go @@ -33,7 +33,7 @@ var encodingTable = map[rune][]bool{ } // Encode creates a codabar barcode for the given content -func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { +func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) { checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`) if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" { return nil, fmt.Errorf("can not encode \"%s\"", content) @@ -45,10 +45,10 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { } resBits.AddBit(encodingTable[r]...) } - return utils.New1DCodeWithDepth(barcode.TypeCodabar, content, resBits, depth), nil + return utils.New1DCodeWithColor(barcode.TypeCodabar, content, resBits, color), nil } // Encode creates a codabar barcode for the given content func Encode(content string) (barcode.Barcode, error) { - return EncodeWithDepth(content, 16) + return EncodeWithColor(content, barcode.ColorScheme16) } diff --git a/code128/encode.go b/code128/encode.go index a5c7dc6..7c3a9a3 100644 --- a/code128/encode.go +++ b/code128/encode.go @@ -156,7 +156,7 @@ func getCodeIndexList(content []rune) *utils.BitList { } // Encode creates a Code 128 barcode for the given content -func EncodeWithDepth(content string, depth int) (barcode.BarcodeIntCS, error) { +func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { contentRunes := strToRunes(content) if len(contentRunes) <= 0 || len(contentRunes) > 80 { return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes)) @@ -180,12 +180,12 @@ func EncodeWithDepth(content string, depth int) (barcode.BarcodeIntCS, error) { sum = sum % 103 result.AddBit(encodingTable[sum]...) result.AddBit(encodingTable[stopSymbol]...) - return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode128, content, result, sum, depth), nil + return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode128, content, result, sum, color), nil } // Encode creates a Code 128 barcode for the given content func Encode(content string) (barcode.BarcodeIntCS, error) { - return EncodeWithDepth(content, 16) + return EncodeWithColor(content, barcode.ColorScheme16) } func EncodeWithoutChecksum(content string) (barcode.Barcode, error) { diff --git a/code39/encoder.go b/code39/encoder.go index 2a745e1..5607615 100644 --- a/code39/encoder.go +++ b/code39/encoder.go @@ -113,7 +113,7 @@ func prepare(content string) (string, error) { // Encode returns a code39 barcode for the given content // if includeChecksum is set to true, a checksum character is calculated and added to the content -func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (barcode.BarcodeIntCS, error) { +func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { if fullASCIIMode { var err error content, err = prepare(content) @@ -148,11 +148,11 @@ func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, d if err != nil { checkSum = 0 } - return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode39, content, result, int(checkSum), depth), nil + return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode39, content, result, int(checkSum), color), nil } // Encode returns a code39 barcode for the given content // if includeChecksum is set to true, a checksum character is calculated and added to the content func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) { - return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16) + return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16) } diff --git a/code93/encoder.go b/code93/encoder.go index abe6aab..ff5f4b4 100644 --- a/code93/encoder.go +++ b/code93/encoder.go @@ -74,7 +74,7 @@ func prepare(content string) (string, error) { return result, nil } -func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (barcode.Barcode, error) { +func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.Barcode, error) { if fullASCIIMode { var err error content, err = prepare(content) @@ -102,13 +102,13 @@ func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, d } result.AddBit(true) - return utils.New1DCodeWithDepth(barcode.TypeCode93, content, result, depth), nil + return utils.New1DCodeWithColor(barcode.TypeCode93, content, result, color), nil } // Encode returns a code93 barcode for the given content // if includeChecksum is set to true, two checksum characters are calculated and added to the content func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) { - return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16) + return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16) } func getChecksum(content string, maxWeight int) rune { diff --git a/datamatrix/codelayout.go b/datamatrix/codelayout.go index 97b9fd9..2158a0a 100644 --- a/datamatrix/codelayout.go +++ b/datamatrix/codelayout.go @@ -1,8 +1,10 @@ package datamatrix import ( - "github.com/boombuler/barcode/utils" "strconv" + + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/utils" ) type setValFunc func(byte) @@ -11,15 +13,15 @@ type codeLayout struct { matrix *utils.BitList occupy *utils.BitList size *dmCodeSize - depth int + color barcode.ColorScheme } -func newCodeLayout(size *dmCodeSize, depth int) *codeLayout { +func newCodeLayout(size *dmCodeSize, color barcode.ColorScheme) *codeLayout { result := new(codeLayout) result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows()) result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows()) result.size = size - result.depth = depth + result.color = color return result } @@ -161,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) { } func (l *codeLayout) Merge() *datamatrixCode { - result := newDataMatrixCodeWithDepth(l.size, l.depth) + result := newDataMatrixCodeWithColor(l.size, l.color) //dotted horizontal lines for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) { diff --git a/datamatrix/datamatrixcode.go b/datamatrix/datamatrixcode.go index 2ae1cf6..c467dce 100644 --- a/datamatrix/datamatrixcode.go +++ b/datamatrix/datamatrixcode.go @@ -12,15 +12,15 @@ type datamatrixCode struct { *utils.BitList *dmCodeSize content string - depth int + color barcode.ColorScheme } -func newDataMatrixCodeWithDepth(size *dmCodeSize, depth int) *datamatrixCode { - return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", depth} +func newDataMatrixCodeWithColor(size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode { + return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", color} } func newDataMatrixCode(size *dmCodeSize) *datamatrixCode { - return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", 16} + return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", barcode.ColorScheme16} } func (c *datamatrixCode) Content() string { @@ -32,7 +32,7 @@ func (c *datamatrixCode) Metadata() barcode.Metadata { } func (c *datamatrixCode) ColorModel() color.Model { - return utils.ColorModel(c.depth) + return c.color.Model } func (c *datamatrixCode) Bounds() image.Rectangle { @@ -41,9 +41,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle { func (c *datamatrixCode) At(x, y int) color.Color { if c.get(x, y) { - return utils.BlackColor(c.depth) + return c.color.Foreground } - return utils.WhiteColor(c.depth) + return c.color.Background } func (c *datamatrixCode) get(x, y int) bool { diff --git a/datamatrix/encoder.go b/datamatrix/encoder.go index 544e0b0..f3b1418 100644 --- a/datamatrix/encoder.go +++ b/datamatrix/encoder.go @@ -8,7 +8,7 @@ import ( ) // Encode returns a Datamatrix barcode for the given content -func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { +func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) { data := encodeText(content) var size *dmCodeSize @@ -23,7 +23,7 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { } data = addPadding(data, size.DataCodewords()) data = ec.calcECC(data, size) - code := render(data, size, depth) + code := render(data, size, color) if code != nil { code.content = content return code, nil @@ -33,11 +33,11 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) { // Encode returns a Datamatrix barcode for the given content func Encode(content string) (barcode.Barcode, error) { - return EncodeWithDepth(content, 16) + return EncodeWithColor(content, barcode.ColorScheme16) } -func render(data []byte, size *dmCodeSize, depth int) *datamatrixCode { - cl := newCodeLayout(size, depth) +func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode { + cl := newCodeLayout(size, color) cl.SetValues(data) @@ -74,11 +74,11 @@ func addPadding(data []byte, toCount int) []byte { } for len(data) < toCount { R := ((149 * (len(data) + 1)) % 253) + 1 - tmp := 129 + R; - if (tmp > 254) { + tmp := 129 + R + if tmp > 254 { tmp = tmp - 254 } - + data = append(data, byte(tmp)) } return data diff --git a/ean/encoder.go b/ean/encoder.go index 0a74a42..f3e7af2 100644 --- a/ean/encoder.go +++ b/ean/encoder.go @@ -158,7 +158,7 @@ func encodeEAN13(code string) *utils.BitList { } // Encode returns a EAN 8 or EAN 13 barcode for the given code -func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) { +func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { var checkSum int if len(code) == 7 || len(code) == 12 { code += string(calcCheckNum(code)) @@ -175,12 +175,12 @@ func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) { if len(code) == 8 { result := encodeEAN8(code) if result != nil { - return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN8, code, result, checkSum, depth), nil + return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN8, code, result, checkSum, color), nil } } else if len(code) == 13 { result := encodeEAN13(code) if result != nil { - return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN13, code, result, checkSum, depth), nil + return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN13, code, result, checkSum, color), nil } } return nil, errors.New("invalid ean code data") @@ -188,5 +188,5 @@ func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) { // Encode returns a EAN 8 or EAN 13 barcode for the given code func Encode(code string) (barcode.BarcodeIntCS, error) { - return EncodeWithDepth(code, 16) + return EncodeWithColor(code, barcode.ColorScheme16) } diff --git a/pdf417/encoder.go b/pdf417/encoder.go index d09e7d7..a6c4310 100644 --- a/pdf417/encoder.go +++ b/pdf417/encoder.go @@ -15,7 +15,7 @@ const ( // Encodes the given data as PDF417 barcode. // securityLevel should be between 0 and 8. The higher the number, the more // additional error-correction codes are added. -func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcode, error) { +func EncodeWithColor(data string, securityLevel byte, color barcode.ColorScheme) (barcode.Barcode, error) { if securityLevel >= 9 { return nil, fmt.Errorf("Invalid security level %d", securityLevel) } @@ -34,7 +34,7 @@ func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcod barcode := new(pdfBarcode) barcode.data = data - barcode.depth = depth + barcode.color = color codeWords, err := encodeData(dataWords, columns, sl) if err != nil { @@ -75,7 +75,7 @@ func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcod // securityLevel should be between 0 and 8. The higher the number, the more // additional error-correction codes are added. func Encode(data string, securityLevel byte) (barcode.Barcode, error) { - return EncodeWithDepth(data, securityLevel, 16) + return EncodeWithColor(data, securityLevel, barcode.ColorScheme16) } func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) { diff --git a/pdf417/pdfcode.go b/pdf417/pdfcode.go index 8dd9d17..68c9c3e 100644 --- a/pdf417/pdfcode.go +++ b/pdf417/pdfcode.go @@ -12,7 +12,7 @@ type pdfBarcode struct { data string width int code *utils.BitList - depth int + color barcode.ColorScheme } func (c *pdfBarcode) Metadata() barcode.Metadata { @@ -24,7 +24,7 @@ func (c *pdfBarcode) Content() string { } func (c *pdfBarcode) ColorModel() color.Model { - return utils.ColorModel(c.depth) + return c.color.Model } func (c *pdfBarcode) Bounds() image.Rectangle { @@ -35,7 +35,7 @@ func (c *pdfBarcode) Bounds() image.Rectangle { func (c *pdfBarcode) At(x, y int) color.Color { if c.code.GetBit((y/moduleHeight)*c.width + x) { - return utils.BlackColor(c.depth) + return c.color.Foreground } - return utils.WhiteColor(c.depth) + return c.color.Background } diff --git a/qr/encoder.go b/qr/encoder.go index f712d02..937c46e 100644 --- a/qr/encoder.go +++ b/qr/encoder.go @@ -55,7 +55,7 @@ func (e Encoding) String() string { } // Encode returns a QR barcode with the given content, error correction level and uses the given encoding -func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding, depth int) (barcode.Barcode, error) { +func EncodeWithColor(content string, level ErrorCorrectionLevel, mode Encoding, color barcode.ColorScheme) (barcode.Barcode, error) { bits, vi, err := mode.getEncoder()(content, level) if err != nil { return nil, err @@ -63,23 +63,23 @@ func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding, blocks := splitToBlocks(bits.IterateBytes(), vi) data := blocks.interleave(vi) - result := render(data, vi, depth) + result := render(data, vi, color) result.content = content return result, nil } func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) { - return EncodeWithDepth(content, level, mode, 16) + return EncodeWithColor(content, level, mode, barcode.ColorScheme16) } -func render(data []byte, vi *versionInfo, depth int) *qrcode { +func render(data []byte, vi *versionInfo, color barcode.ColorScheme) *qrcode { dim := vi.modulWidth() results := make([]*qrcode, 8) for i := 0; i < 8; i++ { - results[i] = newBarCodeWithDepth(dim, depth) + results[i] = newBarCodeWithColor(dim, color) } - occupied := newBarCodeWithDepth(dim, depth) + occupied := newBarCodeWithColor(dim, color) setAll := func(x int, y int, val bool) { occupied.Set(x, y, true) diff --git a/qr/qrcode.go b/qr/qrcode.go index ec44da2..37f6f47 100644 --- a/qr/qrcode.go +++ b/qr/qrcode.go @@ -13,7 +13,7 @@ type qrcode struct { dimension int data *utils.BitList content string - depth int + color barcode.ColorScheme } func (qr *qrcode) Content() string { @@ -25,7 +25,7 @@ func (qr *qrcode) Metadata() barcode.Metadata { } func (qr *qrcode) ColorModel() color.Model { - return utils.ColorModel(qr.depth) + return qr.color.Model } func (qr *qrcode) Bounds() image.Rectangle { @@ -34,9 +34,9 @@ func (qr *qrcode) Bounds() image.Rectangle { func (qr *qrcode) At(x, y int) color.Color { if qr.Get(x, y) { - return utils.BlackColor(qr.depth) + return qr.color.Foreground } - return utils.WhiteColor(qr.depth) + return qr.color.Background } func (qr *qrcode) Get(x, y int) bool { @@ -159,14 +159,14 @@ func (qr *qrcode) calcPenaltyRule4() uint { return uint(math.Min(floor, ceil) * 10) } -func newBarCodeWithDepth(dim int, depth int) *qrcode { +func newBarCodeWithColor(dim int, color barcode.ColorScheme) *qrcode { res := new(qrcode) res.dimension = dim res.data = utils.NewBitList(dim * dim) - res.depth = depth + res.color = color return res } func newBarcode(dim int) *qrcode { - return newBarCodeWithDepth(dim, 16) + return newBarCodeWithColor(dim, barcode.ColorScheme16) } diff --git a/twooffive/encoder.go b/twooffive/encoder.go index 05895ba..da40e98 100644 --- a/twooffive/encoder.go +++ b/twooffive/encoder.go @@ -78,8 +78,8 @@ func AddCheckSum(content string) (string, error) { return content + string(utils.IntToRune(sum%10)), nil } -// Encode creates a codabar barcode for the given content and depth -func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barcode, error) { +// Encode creates a codabar barcode for the given content and color scheme +func EncodeWithColor(content string, interleaved bool, color barcode.ColorScheme) (barcode.Barcode, error) { if content == "" { return nil, errors.New("content is empty") } @@ -131,13 +131,13 @@ func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barco resBits.AddBit(mode.end...) if interleaved { - return utils.New1DCodeWithDepth(barcode.Type2of5Interleaved, content, resBits, depth), nil + return utils.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil } else { - return utils.New1DCodeWithDepth(barcode.Type2of5, content, resBits, depth), nil + return utils.New1DCodeWithColor(barcode.Type2of5, content, resBits, color), nil } } // Encode creates a codabar barcode for the given content func Encode(content string, interleaved bool) (barcode.Barcode, error) { - return EncodeWithDepth(content, interleaved, 16) + return EncodeWithColor(content, interleaved, barcode.ColorScheme16) } diff --git a/utils/base1dcode.go b/utils/base1dcode.go index bd815f0..646600a 100644 --- a/utils/base1dcode.go +++ b/utils/base1dcode.go @@ -12,7 +12,7 @@ type base1DCode struct { *BitList kind string content string - depth int + color barcode.ColorScheme } type base1DCodeIntCS struct { @@ -29,7 +29,7 @@ func (c *base1DCode) Metadata() barcode.Metadata { } func (c *base1DCode) ColorModel() color.Model { - return ColorModel(c.depth) + return c.color.Model } func (c *base1DCode) Bounds() image.Rectangle { @@ -38,9 +38,9 @@ func (c *base1DCode) Bounds() image.Rectangle { func (c *base1DCode) At(x, y int) color.Color { if c.GetBit(x) { - return BlackColor(c.depth) + return c.color.Foreground } - return WhiteColor(c.depth) + return c.color.Background } func (c *base1DCodeIntCS) CheckSum() int { @@ -49,20 +49,20 @@ func (c *base1DCodeIntCS) CheckSum() int { // New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList func New1DCodeIntCheckSum(codeKind, content string, bars *BitList, checksum int) barcode.BarcodeIntCS { - return &base1DCodeIntCS{base1DCode{bars, codeKind, content, 16}, checksum} + return &base1DCodeIntCS{base1DCode{bars, codeKind, content, barcode.ColorScheme16}, checksum} } // New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList -func New1DCodeIntCheckSumWithDepth(codeKind, content string, bars *BitList, checksum int, depth int) barcode.BarcodeIntCS { - return &base1DCodeIntCS{base1DCode{bars, codeKind, content, depth}, checksum} +func New1DCodeIntCheckSumWithColor(codeKind, content string, bars *BitList, checksum int, color barcode.ColorScheme) barcode.BarcodeIntCS { + return &base1DCodeIntCS{base1DCode{bars, codeKind, content, color}, checksum} } // New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList func New1DCode(codeKind, content string, bars *BitList) barcode.Barcode { - return &base1DCode{bars, codeKind, content, 16} + return &base1DCode{bars, codeKind, content, barcode.ColorScheme16} } // New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList -func New1DCodeWithDepth(codeKind, content string, bars *BitList, depth int) barcode.Barcode { - return &base1DCode{bars, codeKind, content, depth} +func New1DCodeWithColor(codeKind, content string, bars *BitList, color barcode.ColorScheme) barcode.Barcode { + return &base1DCode{bars, codeKind, content, color} } From 2c9211e1ea81308df619c30356ef0edafdfbca91 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Wed, 31 Jul 2024 16:50:14 -0300 Subject: [PATCH 06/12] Add ScaleWithFill --- scaledbarcode.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/scaledbarcode.go b/scaledbarcode.go index 152b180..1e87a2a 100644 --- a/scaledbarcode.go +++ b/scaledbarcode.go @@ -49,11 +49,16 @@ func (bc *intCSscaledBC) CheckSum() int { // Scale returns a resized barcode with the given width and height. func Scale(bc Barcode, width, height int) (Barcode, error) { + return ScaleWithFill(bc, width, height, color.White) +} + +// Scale returns a resized barcode with the given width, height and fill color. +func ScaleWithFill(bc Barcode, width, height int, fill color.Color) (Barcode, error) { switch bc.Metadata().Dimensions { case 1: - return scale1DCode(bc, width, height) + return scale1DCode(bc, width, height, fill) case 2: - return scale2DCode(bc, width, height) + return scale2DCode(bc, width, height, fill) } return nil, errors.New("unsupported barcode format") @@ -72,7 +77,7 @@ func newScaledBC(wrapped Barcode, wrapperFunc wrapFunc, rect image.Rectangle) Ba return result } -func scale2DCode(bc Barcode, width, height int) (Barcode, error) { +func scale2DCode(bc Barcode, width, height int, fill color.Color) (Barcode, error) { orgBounds := bc.Bounds() orgWidth := orgBounds.Max.X - orgBounds.Min.X orgHeight := orgBounds.Max.Y - orgBounds.Min.Y @@ -87,12 +92,12 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) { wrap := func(x, y int) color.Color { if x < offsetX || y < offsetY { - return color.White + return fill } x = (x - offsetX) / factor y = (y - offsetY) / factor if x >= orgWidth || y >= orgHeight { - return color.White + return fill } return bc.At(x, y) } @@ -104,7 +109,7 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) { ), nil } -func scale1DCode(bc Barcode, width, height int) (Barcode, error) { +func scale1DCode(bc Barcode, width, height int, fill color.Color) (Barcode, error) { orgBounds := bc.Bounds() orgWidth := orgBounds.Max.X - orgBounds.Min.X factor := int(float64(width) / float64(orgWidth)) @@ -116,12 +121,12 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) { wrap := func(x, y int) color.Color { if x < offsetX { - return color.White + return fill } x = (x - offsetX) / factor if x >= orgWidth { - return color.White + return fill } return bc.At(x, 0) } From 1c8224977e1efd6f585a2fb6a20809b6af6b5dd8 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Thu, 1 Aug 2024 09:00:33 -0300 Subject: [PATCH 07/12] Remove unecessary file --- utils/depth.go | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 utils/depth.go diff --git a/utils/depth.go b/utils/depth.go deleted file mode 100644 index 0bb051b..0000000 --- a/utils/depth.go +++ /dev/null @@ -1,36 +0,0 @@ -package utils - -import "image/color" - -func ColorModel(depth int) color.Model { - switch depth { - case 8: - return color.GrayModel - case 24, 32: - return color.RGBAModel - default: - return color.Gray16Model - } -} - -func WhiteColor(depth int) color.Color { - switch depth { - case 8: - return color.Gray{Y: 255} - case 24, 32: - return color.RGBA{255, 255, 255, 255} - default: - return color.White - } -} - -func BlackColor(depth int) color.Color { - switch depth { - case 8: - return color.Gray{Y: 0} - case 24, 32: - return color.RGBA{0, 0, 0, 255} - default: - return color.Black - } -} From b1129f9d8ba88f3accf0e79c0a7a3b5363a7726b Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Thu, 1 Aug 2024 09:02:23 -0300 Subject: [PATCH 08/12] Update godoc comment of EncodeWithColor methods --- aztec/encoder.go | 2 +- codabar/encoder.go | 2 +- code128/encode.go | 2 +- code39/encoder.go | 2 +- code93/encoder.go | 2 ++ datamatrix/encoder.go | 2 +- ean/encoder.go | 2 +- pdf417/encoder.go | 2 +- qr/encoder.go | 2 +- 9 files changed, 10 insertions(+), 8 deletions(-) diff --git a/aztec/encoder.go b/aztec/encoder.go index a7ebc50..a63650d 100644 --- a/aztec/encoder.go +++ b/aztec/encoder.go @@ -127,7 +127,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba return EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16) } -// Encode returns an aztec barcode with the given content +// Encode returns an aztec barcode with the given content and color scheme func EncodeWithColor(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme) (barcode.Barcode, error) { bits := highlevelEncode(data) eccBits := ((bits.Len() * minECCPercent) / 100) + 11 diff --git a/codabar/encoder.go b/codabar/encoder.go index beb0e0e..74689d0 100644 --- a/codabar/encoder.go +++ b/codabar/encoder.go @@ -32,7 +32,7 @@ var encodingTable = map[rune][]bool{ 'D': []bool{true, false, true, false, false, true, true, false, false, true}, } -// Encode creates a codabar barcode for the given content +// Encode creates a codabar barcode for the given content and color scheme func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) { checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`) if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" { diff --git a/code128/encode.go b/code128/encode.go index 7c3a9a3..3e1b646 100644 --- a/code128/encode.go +++ b/code128/encode.go @@ -155,7 +155,7 @@ func getCodeIndexList(content []rune) *utils.BitList { return result } -// Encode creates a Code 128 barcode for the given content +// Encode creates a Code 128 barcode for the given content and color scheme func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { contentRunes := strToRunes(content) if len(contentRunes) <= 0 || len(contentRunes) > 80 { diff --git a/code39/encoder.go b/code39/encoder.go index 5607615..f474173 100644 --- a/code39/encoder.go +++ b/code39/encoder.go @@ -111,7 +111,7 @@ func prepare(content string) (string, error) { return result, nil } -// Encode returns a code39 barcode for the given content +// Encode returns a code39 barcode for the given content and color scheme // if includeChecksum is set to true, a checksum character is calculated and added to the content func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { if fullASCIIMode { diff --git a/code93/encoder.go b/code93/encoder.go index ff5f4b4..27140c3 100644 --- a/code93/encoder.go +++ b/code93/encoder.go @@ -74,6 +74,8 @@ func prepare(content string) (string, error) { return result, nil } +// Encode returns a code93 barcode for the given content and color scheme +// if includeChecksum is set to true, two checksum characters are calculated and added to the content func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.Barcode, error) { if fullASCIIMode { var err error diff --git a/datamatrix/encoder.go b/datamatrix/encoder.go index f3b1418..9501bee 100644 --- a/datamatrix/encoder.go +++ b/datamatrix/encoder.go @@ -7,7 +7,7 @@ import ( "github.com/boombuler/barcode" ) -// Encode returns a Datamatrix barcode for the given content +// Encode returns a Datamatrix barcode for the given content and color scheme func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) { data := encodeText(content) diff --git a/ean/encoder.go b/ean/encoder.go index f3e7af2..4a80706 100644 --- a/ean/encoder.go +++ b/ean/encoder.go @@ -157,7 +157,7 @@ func encodeEAN13(code string) *utils.BitList { return result } -// Encode returns a EAN 8 or EAN 13 barcode for the given code +// Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { var checkSum int if len(code) == 7 || len(code) == 12 { diff --git a/pdf417/encoder.go b/pdf417/encoder.go index a6c4310..3425ac7 100644 --- a/pdf417/encoder.go +++ b/pdf417/encoder.go @@ -12,7 +12,7 @@ const ( padding_codeword = 900 ) -// Encodes the given data as PDF417 barcode. +// Encodes the given data and color scheme as PDF417 barcode. // securityLevel should be between 0 and 8. The higher the number, the more // additional error-correction codes are added. func EncodeWithColor(data string, securityLevel byte, color barcode.ColorScheme) (barcode.Barcode, error) { diff --git a/qr/encoder.go b/qr/encoder.go index 937c46e..81ceb88 100644 --- a/qr/encoder.go +++ b/qr/encoder.go @@ -54,7 +54,7 @@ func (e Encoding) String() string { return "" } -// Encode returns a QR barcode with the given content, error correction level and uses the given encoding +// Encode returns a QR barcode with the given content and color scheme, error correction level and uses the given encoding func EncodeWithColor(content string, level ErrorCorrectionLevel, mode Encoding, color barcode.ColorScheme) (barcode.Barcode, error) { bits, vi, err := mode.getEncoder()(content, level) if err != nil { From a8e67c5d16f5f4de67a62c2dd28d48f9e52216c6 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Fri, 2 Aug 2024 12:30:54 -0300 Subject: [PATCH 09/12] Add EncodeWithChecksum color overload --- code128/encode.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/code128/encode.go b/code128/encode.go index 3e1b646..9a101b6 100644 --- a/code128/encode.go +++ b/code128/encode.go @@ -189,6 +189,10 @@ func Encode(content string) (barcode.BarcodeIntCS, error) { } func EncodeWithoutChecksum(content string) (barcode.Barcode, error) { + return EncodeWithoutChecksumWithColor(content, barcode.ColorScheme16) +} + +func EncodeWithoutChecksumWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) { contentRunes := strToRunes(content) if len(contentRunes) <= 0 || len(contentRunes) > 80 { return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes)) @@ -204,5 +208,5 @@ func EncodeWithoutChecksum(content string) (barcode.Barcode, error) { result.AddBit(encodingTable[idx]...) } result.AddBit(encodingTable[stopSymbol]...) - return utils.New1DCode(barcode.TypeCode128, content, result), nil + return utils.New1DCodeWithColor(barcode.TypeCode128, content, result, color), nil } From 49d4ce8a5b9497985a4d93cb8b31484902ad49ed Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Fri, 2 Aug 2024 12:31:58 -0300 Subject: [PATCH 10/12] Add BarcodeColor interface --- barcode.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/barcode.go b/barcode.go index 25f4a69..9d23881 100644 --- a/barcode.go +++ b/barcode.go @@ -1,6 +1,8 @@ package barcode -import "image" +import ( + "image" +) const ( TypeAztec = "Aztec" @@ -40,3 +42,7 @@ type BarcodeIntCS interface { Barcode CheckSum() int } + +type BarcodeColor interface { + ColorScheme() ColorScheme +} From d5743d95affe3ce2ea90e5e500269a34bdaf8f91 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Fri, 2 Aug 2024 12:33:00 -0300 Subject: [PATCH 11/12] Barcode implementations also implements BarcodeColor interface --- aztec/azteccode.go | 4 ++++ datamatrix/datamatrixcode.go | 4 ++++ pdf417/pdfcode.go | 4 ++++ qr/qrcode.go | 4 ++++ utils/base1dcode.go | 4 ++++ 5 files changed, 20 insertions(+) diff --git a/aztec/azteccode.go b/aztec/azteccode.go index 48de252..c757ae6 100644 --- a/aztec/azteccode.go +++ b/aztec/azteccode.go @@ -32,6 +32,10 @@ func (c *aztecCode) ColorModel() color.Model { return c.color.Model } +func (c *aztecCode) ColorScheme() barcode.ColorScheme { + return c.color +} + func (c *aztecCode) Bounds() image.Rectangle { return image.Rect(0, 0, c.size, c.size) } diff --git a/datamatrix/datamatrixcode.go b/datamatrix/datamatrixcode.go index c467dce..7e7ca26 100644 --- a/datamatrix/datamatrixcode.go +++ b/datamatrix/datamatrixcode.go @@ -35,6 +35,10 @@ func (c *datamatrixCode) ColorModel() color.Model { return c.color.Model } +func (c *datamatrixCode) ColorScheme() barcode.ColorScheme { + return c.color +} + func (c *datamatrixCode) Bounds() image.Rectangle { return image.Rect(0, 0, c.Columns, c.Rows) } diff --git a/pdf417/pdfcode.go b/pdf417/pdfcode.go index 68c9c3e..a594bd2 100644 --- a/pdf417/pdfcode.go +++ b/pdf417/pdfcode.go @@ -27,6 +27,10 @@ func (c *pdfBarcode) ColorModel() color.Model { return c.color.Model } +func (c *pdfBarcode) ColorScheme() barcode.ColorScheme { + return c.color +} + func (c *pdfBarcode) Bounds() image.Rectangle { height := c.code.Len() / c.width diff --git a/qr/qrcode.go b/qr/qrcode.go index 37f6f47..2fb44ab 100644 --- a/qr/qrcode.go +++ b/qr/qrcode.go @@ -28,6 +28,10 @@ func (qr *qrcode) ColorModel() color.Model { return qr.color.Model } +func (c *qrcode) ColorScheme() barcode.ColorScheme { + return c.color +} + func (qr *qrcode) Bounds() image.Rectangle { return image.Rect(0, 0, qr.dimension, qr.dimension) } diff --git a/utils/base1dcode.go b/utils/base1dcode.go index 646600a..1b77e4f 100644 --- a/utils/base1dcode.go +++ b/utils/base1dcode.go @@ -32,6 +32,10 @@ func (c *base1DCode) ColorModel() color.Model { return c.color.Model } +func (c *base1DCode) ColorScheme() barcode.ColorScheme { + return c.color +} + func (c *base1DCode) Bounds() image.Rectangle { return image.Rect(0, 0, c.Len(), 1) } From d79eb8727b08e5b3cb34f14a9633f09fef8108f3 Mon Sep 17 00:00:00 2001 From: zhaori96 Date: Fri, 2 Aug 2024 12:35:17 -0300 Subject: [PATCH 12/12] Use the Background color from ColorScheme if bc implements BarcodeColor --- scaledbarcode.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scaledbarcode.go b/scaledbarcode.go index 1e87a2a..491cc3f 100644 --- a/scaledbarcode.go +++ b/scaledbarcode.go @@ -49,7 +49,13 @@ func (bc *intCSscaledBC) CheckSum() int { // Scale returns a resized barcode with the given width and height. func Scale(bc Barcode, width, height int) (Barcode, error) { - return ScaleWithFill(bc, width, height, color.White) + var fill color.Color + if v, ok := bc.(BarcodeColor); ok { + fill = v.ColorScheme().Background + } else { + fill = color.White + } + return ScaleWithFill(bc, width, height, fill) } // Scale returns a resized barcode with the given width, height and fill color.