Skip to content

Commit ca3e24f

Browse files
authored
Merge pull request #84 from zhaori96/allow-other-depths
Allow other color schemes and models for barcodes.
2 parents 83789df + d79eb87 commit ca3e24f

File tree

19 files changed

+233
-71
lines changed

19 files changed

+233
-71
lines changed

aztec/azteccode.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ type aztecCode struct {
1313
*utils.BitList
1414
size int
1515
content []byte
16+
color barcode.ColorScheme
1617
}
1718

18-
func newAztecCode(size int) *aztecCode {
19-
return &aztecCode{utils.NewBitList(size * size), size, nil}
19+
func newAztecCode(size int, color barcode.ColorScheme) *aztecCode {
20+
return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16}
2021
}
2122

2223
func (c *aztecCode) Content() string {
@@ -28,7 +29,11 @@ func (c *aztecCode) Metadata() barcode.Metadata {
2829
}
2930

3031
func (c *aztecCode) ColorModel() color.Model {
31-
return color.Gray16Model
32+
return c.color.Model
33+
}
34+
35+
func (c *aztecCode) ColorScheme() barcode.ColorScheme {
36+
return c.color
3237
}
3338

3439
func (c *aztecCode) Bounds() image.Rectangle {
@@ -37,9 +42,9 @@ func (c *aztecCode) Bounds() image.Rectangle {
3742

3843
func (c *aztecCode) At(x, y int) color.Color {
3944
if c.GetBit(x*c.size + y) {
40-
return color.Black
45+
return c.color.Foreground
4146
}
42-
return color.White
47+
return c.color.Background
4348
}
4449

4550
func (c *aztecCode) set(x, y int) {

aztec/encoder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func drawBullsEye(matrix *aztecCode, center, size int) {
124124

125125
// Encode returns an aztec barcode with the given content
126126
func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error) {
127+
return EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16)
128+
}
129+
130+
// Encode returns an aztec barcode with the given content and color scheme
131+
func EncodeWithColor(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme) (barcode.Barcode, error) {
127132
bits := highlevelEncode(data)
128133
eccBits := ((bits.Len() * minECCPercent) / 100) + 11
129134
totalSizeBits := bits.Len() + eccBits
@@ -215,7 +220,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba
215220
alignmentMap[origCenter+i] = center + newOffset + 1
216221
}
217222
}
218-
code := newAztecCode(matrixSize)
223+
code := newAztecCode(matrixSize, color)
219224
code.content = data
220225

221226
// draw data bits

barcode.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package barcode
22

3-
import "image"
3+
import (
4+
"image"
5+
)
46

57
const (
68
TypeAztec = "Aztec"
@@ -40,3 +42,7 @@ type BarcodeIntCS interface {
4042
Barcode
4143
CheckSum() int
4244
}
45+
46+
type BarcodeColor interface {
47+
ColorScheme() ColorScheme
48+
}

codabar/encoder.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ var encodingTable = map[rune][]bool{
3232
'D': []bool{true, false, true, false, false, true, true, false, false, true},
3333
}
3434

35-
// Encode creates a codabar barcode for the given content
36-
func Encode(content string) (barcode.Barcode, error) {
35+
// Encode creates a codabar barcode for the given content and color scheme
36+
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
3737
checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
3838
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
3939
return nil, fmt.Errorf("can not encode \"%s\"", content)
@@ -45,5 +45,10 @@ func Encode(content string) (barcode.Barcode, error) {
4545
}
4646
resBits.AddBit(encodingTable[r]...)
4747
}
48-
return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
48+
return utils.New1DCodeWithColor(barcode.TypeCodabar, content, resBits, color), nil
49+
}
50+
51+
// Encode creates a codabar barcode for the given content
52+
func Encode(content string) (barcode.Barcode, error) {
53+
return EncodeWithColor(content, barcode.ColorScheme16)
4954
}

code128/encode.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ func getCodeIndexList(content []rune) *utils.BitList {
155155
return result
156156
}
157157

158-
// Encode creates a Code 128 barcode for the given content
159-
func Encode(content string) (barcode.BarcodeIntCS, error) {
158+
// Encode creates a Code 128 barcode for the given content and color scheme
159+
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
160160
contentRunes := strToRunes(content)
161161
if len(contentRunes) <= 0 || len(contentRunes) > 80 {
162162
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
@@ -180,10 +180,19 @@ func Encode(content string) (barcode.BarcodeIntCS, error) {
180180
sum = sum % 103
181181
result.AddBit(encodingTable[sum]...)
182182
result.AddBit(encodingTable[stopSymbol]...)
183-
return utils.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), nil
183+
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode128, content, result, sum, color), nil
184+
}
185+
186+
// Encode creates a Code 128 barcode for the given content
187+
func Encode(content string) (barcode.BarcodeIntCS, error) {
188+
return EncodeWithColor(content, barcode.ColorScheme16)
184189
}
185190

186191
func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
192+
return EncodeWithoutChecksumWithColor(content, barcode.ColorScheme16)
193+
}
194+
195+
func EncodeWithoutChecksumWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
187196
contentRunes := strToRunes(content)
188197
if len(contentRunes) <= 0 || len(contentRunes) > 80 {
189198
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
@@ -199,5 +208,5 @@ func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
199208
result.AddBit(encodingTable[idx]...)
200209
}
201210
result.AddBit(encodingTable[stopSymbol]...)
202-
return utils.New1DCode(barcode.TypeCode128, content, result), nil
211+
return utils.New1DCodeWithColor(barcode.TypeCode128, content, result, color), nil
203212
}

code39/encoder.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ func prepare(content string) (string, error) {
111111
return result, nil
112112
}
113113

114-
// Encode returns a code39 barcode for the given content
114+
// Encode returns a code39 barcode for the given content and color scheme
115115
// if includeChecksum is set to true, a checksum character is calculated and added to the content
116-
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
116+
func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
117117
if fullASCIIMode {
118118
var err error
119119
content, err = prepare(content)
@@ -148,5 +148,11 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
148148
if err != nil {
149149
checkSum = 0
150150
}
151-
return utils.New1DCodeIntCheckSum(barcode.TypeCode39, content, result, int(checkSum)), nil
151+
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode39, content, result, int(checkSum), color), nil
152+
}
153+
154+
// Encode returns a code39 barcode for the given content
155+
// if includeChecksum is set to true, a checksum character is calculated and added to the content
156+
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
157+
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
152158
}

code93/encoder.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func prepare(content string) (string, error) {
7474
return result, nil
7575
}
7676

77-
// Encode returns a code93 barcode for the given content
77+
// Encode returns a code93 barcode for the given content and color scheme
7878
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
79-
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
79+
func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.Barcode, error) {
8080
if fullASCIIMode {
8181
var err error
8282
content, err = prepare(content)
@@ -104,7 +104,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
104104
}
105105
result.AddBit(true)
106106

107-
return utils.New1DCode(barcode.TypeCode93, content, result), nil
107+
return utils.New1DCodeWithColor(barcode.TypeCode93, content, result, color), nil
108+
}
109+
110+
// Encode returns a code93 barcode for the given content
111+
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
112+
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
113+
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
108114
}
109115

110116
func getChecksum(content string, maxWeight int) rune {

color_scheme.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package barcode
2+
3+
import "image/color"
4+
5+
// ColorScheme defines a structure for color schemes used in barcode rendering.
6+
// It includes the color model, background color, and foreground color.
7+
type ColorScheme struct {
8+
Model color.Model // Color model to be used (e.g., grayscale, RGB, RGBA)
9+
Background color.Color // Color of the background
10+
Foreground color.Color // Color of the foreground (e.g., bars in a barcode)
11+
}
12+
13+
// ColorScheme8 represents a color scheme with 8-bit grayscale colors.
14+
var ColorScheme8 = ColorScheme{
15+
Model: color.GrayModel,
16+
Background: color.Gray{Y: 255},
17+
Foreground: color.Gray{Y: 0},
18+
}
19+
20+
// ColorScheme16 represents a color scheme with 16-bit grayscale colors.
21+
var ColorScheme16 = ColorScheme{
22+
Model: color.Gray16Model,
23+
Background: color.White,
24+
Foreground: color.Black,
25+
}
26+
27+
// ColorScheme24 represents a color scheme with 24-bit RGB colors.
28+
var ColorScheme24 = ColorScheme{
29+
Model: color.RGBAModel,
30+
Background: color.RGBA{255, 255, 255, 255},
31+
Foreground: color.RGBA{0, 0, 0, 255},
32+
}
33+
34+
// ColorScheme32 represents a color scheme with 32-bit RGBA colors, which is similar to ColorScheme24 but typically includes alpha for transparency.
35+
var ColorScheme32 = ColorScheme{
36+
Model: color.RGBAModel,
37+
Background: color.RGBA{255, 255, 255, 255},
38+
Foreground: color.RGBA{0, 0, 0, 255},
39+
}

datamatrix/codelayout.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package datamatrix
22

33
import (
4-
"github.com/boombuler/barcode/utils"
54
"strconv"
5+
6+
"github.com/boombuler/barcode"
7+
"github.com/boombuler/barcode/utils"
68
)
79

810
type setValFunc func(byte)
@@ -11,13 +13,15 @@ type codeLayout struct {
1113
matrix *utils.BitList
1214
occupy *utils.BitList
1315
size *dmCodeSize
16+
color barcode.ColorScheme
1417
}
1518

16-
func newCodeLayout(size *dmCodeSize) *codeLayout {
19+
func newCodeLayout(size *dmCodeSize, color barcode.ColorScheme) *codeLayout {
1720
result := new(codeLayout)
1821
result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
1922
result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
2023
result.size = size
24+
result.color = color
2125
return result
2226
}
2327

@@ -159,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) {
159163
}
160164

161165
func (l *codeLayout) Merge() *datamatrixCode {
162-
result := newDataMatrixCode(l.size)
166+
result := newDataMatrixCodeWithColor(l.size, l.color)
163167

164168
//dotted horizontal lines
165169
for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) {

datamatrix/datamatrixcode.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ type datamatrixCode struct {
1212
*utils.BitList
1313
*dmCodeSize
1414
content string
15+
color barcode.ColorScheme
16+
}
17+
18+
func newDataMatrixCodeWithColor(size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
19+
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", color}
1520
}
1621

1722
func newDataMatrixCode(size *dmCodeSize) *datamatrixCode {
18-
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, ""}
23+
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", barcode.ColorScheme16}
1924
}
2025

2126
func (c *datamatrixCode) Content() string {
@@ -27,7 +32,11 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
2732
}
2833

2934
func (c *datamatrixCode) ColorModel() color.Model {
30-
return color.Gray16Model
35+
return c.color.Model
36+
}
37+
38+
func (c *datamatrixCode) ColorScheme() barcode.ColorScheme {
39+
return c.color
3140
}
3241

3342
func (c *datamatrixCode) Bounds() image.Rectangle {
@@ -36,9 +45,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle {
3645

3746
func (c *datamatrixCode) At(x, y int) color.Color {
3847
if c.get(x, y) {
39-
return color.Black
48+
return c.color.Foreground
4049
}
41-
return color.White
50+
return c.color.Background
4251
}
4352

4453
func (c *datamatrixCode) get(x, y int) bool {

0 commit comments

Comments
 (0)