Refactor to use ColorScheme instead of 'depth' approach

This commit is contained in:
zhaori96 committed 2024-07-31 16:11:52 -03:00
1 parent e966fa7106
commit 30509e0dd7
16 files changed
+83 -81

No files matched your search

+7 -5
View File
@@ -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) {
+7 -7
View File
@@ -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 {
+8 -8
View File
@@ -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