Refactor to use ColorScheme instead of 'depth' approach

This commit is contained in:
zhaori96 2024-07-31 16:11:52 -03:00
parent e966fa7106
commit 30509e0dd7
16 changed files with 83 additions and 81 deletions

View File

@ -13,11 +13,11 @@ type aztecCode struct {
*utils.BitList *utils.BitList
size int size int
content []byte content []byte
depth int color barcode.ColorScheme
} }
func newAztecCode(size int, depth int) *aztecCode { func newAztecCode(size int, color barcode.ColorScheme) *aztecCode {
return &aztecCode{utils.NewBitList(size * size), size, nil, 16} return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16}
} }
func (c *aztecCode) Content() string { func (c *aztecCode) Content() string {
@ -29,7 +29,7 @@ func (c *aztecCode) Metadata() barcode.Metadata {
} }
func (c *aztecCode) ColorModel() color.Model { func (c *aztecCode) ColorModel() color.Model {
return utils.ColorModel(c.depth) return c.color.Model
} }
func (c *aztecCode) Bounds() image.Rectangle { 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 { func (c *aztecCode) At(x, y int) color.Color {
if c.GetBit(x*c.size + y) { 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) { func (c *aztecCode) set(x, y int) {

View File

@ -123,12 +123,12 @@ func drawBullsEye(matrix *aztecCode, center, size int) {
} }
// Encode returns an aztec barcode with the given content // 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) 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
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) bits := highlevelEncode(data)
eccBits := ((bits.Len() * minECCPercent) / 100) + 11 eccBits := ((bits.Len() * minECCPercent) / 100) + 11
totalSizeBits := bits.Len() + eccBits totalSizeBits := bits.Len() + eccBits
@ -220,7 +220,7 @@ func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, de
alignmentMap[origCenter+i] = center + newOffset + 1 alignmentMap[origCenter+i] = center + newOffset + 1
} }
} }
code := newAztecCode(matrixSize, depth) code := newAztecCode(matrixSize, color)
code.content = data code.content = data
// draw data bits // draw data bits

View File

@ -33,7 +33,7 @@ var encodingTable = map[rune][]bool{
} }
// Encode creates a codabar barcode for the given content // 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]$`) checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" { if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
return nil, fmt.Errorf("can not encode \"%s\"", 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]...) 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 // Encode creates a codabar barcode for the given content
func Encode(content string) (barcode.Barcode, error) { func Encode(content string) (barcode.Barcode, error) {
return EncodeWithDepth(content, 16) return EncodeWithColor(content, barcode.ColorScheme16)
} }

View File

@ -156,7 +156,7 @@ func getCodeIndexList(content []rune) *utils.BitList {
} }
// Encode creates a Code 128 barcode for the given content // 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) contentRunes := strToRunes(content)
if len(contentRunes) <= 0 || len(contentRunes) > 80 { 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)) 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 sum = sum % 103
result.AddBit(encodingTable[sum]...) result.AddBit(encodingTable[sum]...)
result.AddBit(encodingTable[stopSymbol]...) 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 // Encode creates a Code 128 barcode for the given content
func Encode(content string) (barcode.BarcodeIntCS, error) { func Encode(content string) (barcode.BarcodeIntCS, error) {
return EncodeWithDepth(content, 16) return EncodeWithColor(content, barcode.ColorScheme16)
} }
func EncodeWithoutChecksum(content string) (barcode.Barcode, error) { func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {

View File

@ -113,7 +113,7 @@ func prepare(content string) (string, error) {
// Encode returns a code39 barcode for the given content // 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 // 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 { if fullASCIIMode {
var err error var err error
content, err = prepare(content) content, err = prepare(content)
@ -148,11 +148,11 @@ func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, d
if err != nil { if err != nil {
checkSum = 0 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 // 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 // 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 Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16) return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
} }

View File

@ -74,7 +74,7 @@ func prepare(content string) (string, error) {
return result, nil 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 { if fullASCIIMode {
var err error var err error
content, err = prepare(content) content, err = prepare(content)
@ -102,13 +102,13 @@ func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, d
} }
result.AddBit(true) 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 // 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 // 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 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 { func getChecksum(content string, maxWeight int) rune {

View File

@ -1,8 +1,10 @@
package datamatrix package datamatrix
import ( import (
"github.com/boombuler/barcode/utils"
"strconv" "strconv"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/utils"
) )
type setValFunc func(byte) type setValFunc func(byte)
@ -11,15 +13,15 @@ type codeLayout struct {
matrix *utils.BitList matrix *utils.BitList
occupy *utils.BitList occupy *utils.BitList
size *dmCodeSize 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 := new(codeLayout)
result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows()) result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows()) result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
result.size = size result.size = size
result.depth = depth result.color = color
return result return result
} }
@ -161,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) {
} }
func (l *codeLayout) Merge() *datamatrixCode { func (l *codeLayout) Merge() *datamatrixCode {
result := newDataMatrixCodeWithDepth(l.size, l.depth) result := newDataMatrixCodeWithColor(l.size, l.color)
//dotted horizontal lines //dotted horizontal lines
for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) { for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) {

View File

@ -12,15 +12,15 @@ type datamatrixCode struct {
*utils.BitList *utils.BitList
*dmCodeSize *dmCodeSize
content string content string
depth int color barcode.ColorScheme
} }
func newDataMatrixCodeWithDepth(size *dmCodeSize, depth int) *datamatrixCode { func newDataMatrixCodeWithColor(size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", depth} return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", color}
} }
func newDataMatrixCode(size *dmCodeSize) *datamatrixCode { 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 { func (c *datamatrixCode) Content() string {
@ -32,7 +32,7 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
} }
func (c *datamatrixCode) ColorModel() color.Model { func (c *datamatrixCode) ColorModel() color.Model {
return utils.ColorModel(c.depth) return c.color.Model
} }
func (c *datamatrixCode) Bounds() image.Rectangle { 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 { func (c *datamatrixCode) At(x, y int) color.Color {
if c.get(x, y) { 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 { func (c *datamatrixCode) get(x, y int) bool {

View File

@ -8,7 +8,7 @@ import (
) )
// Encode returns a Datamatrix barcode for the given content // 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) data := encodeText(content)
var size *dmCodeSize var size *dmCodeSize
@ -23,7 +23,7 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
} }
data = addPadding(data, size.DataCodewords()) data = addPadding(data, size.DataCodewords())
data = ec.calcECC(data, size) data = ec.calcECC(data, size)
code := render(data, size, depth) code := render(data, size, color)
if code != nil { if code != nil {
code.content = content code.content = content
return code, nil 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 // Encode returns a Datamatrix barcode for the given content
func Encode(content string) (barcode.Barcode, error) { 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 { func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
cl := newCodeLayout(size, depth) cl := newCodeLayout(size, color)
cl.SetValues(data) cl.SetValues(data)
@ -74,11 +74,11 @@ func addPadding(data []byte, toCount int) []byte {
} }
for len(data) < toCount { for len(data) < toCount {
R := ((149 * (len(data) + 1)) % 253) + 1 R := ((149 * (len(data) + 1)) % 253) + 1
tmp := 129 + R; tmp := 129 + R
if (tmp > 254) { if tmp > 254 {
tmp = tmp - 254 tmp = tmp - 254
} }
data = append(data, byte(tmp)) data = append(data, byte(tmp))
} }
return data return data

View File

@ -158,7 +158,7 @@ func encodeEAN13(code string) *utils.BitList {
} }
// 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
func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) { func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
var checkSum int var checkSum int
if len(code) == 7 || len(code) == 12 { if len(code) == 7 || len(code) == 12 {
code += string(calcCheckNum(code)) code += string(calcCheckNum(code))
@ -175,12 +175,12 @@ func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) {
if len(code) == 8 { if len(code) == 8 {
result := encodeEAN8(code) result := encodeEAN8(code)
if result != nil { 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 { } else if len(code) == 13 {
result := encodeEAN13(code) result := encodeEAN13(code)
if result != nil { 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") 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 // Encode returns a EAN 8 or EAN 13 barcode for the given code
func Encode(code string) (barcode.BarcodeIntCS, error) { func Encode(code string) (barcode.BarcodeIntCS, error) {
return EncodeWithDepth(code, 16) return EncodeWithColor(code, barcode.ColorScheme16)
} }

View File

@ -15,7 +15,7 @@ const (
// Encodes the given data as PDF417 barcode. // Encodes the given data as PDF417 barcode.
// securityLevel should be between 0 and 8. The higher the number, the more // securityLevel should be between 0 and 8. The higher the number, the more
// additional error-correction codes are added. // 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 { if securityLevel >= 9 {
return nil, fmt.Errorf("Invalid security level %d", securityLevel) 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 := new(pdfBarcode)
barcode.data = data barcode.data = data
barcode.depth = depth barcode.color = color
codeWords, err := encodeData(dataWords, columns, sl) codeWords, err := encodeData(dataWords, columns, sl)
if err != nil { 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 // securityLevel should be between 0 and 8. The higher the number, the more
// additional error-correction codes are added. // additional error-correction codes are added.
func Encode(data string, securityLevel byte) (barcode.Barcode, error) { 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) { func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) {

View File

@ -12,7 +12,7 @@ type pdfBarcode struct {
data string data string
width int width int
code *utils.BitList code *utils.BitList
depth int color barcode.ColorScheme
} }
func (c *pdfBarcode) Metadata() barcode.Metadata { func (c *pdfBarcode) Metadata() barcode.Metadata {
@ -24,7 +24,7 @@ func (c *pdfBarcode) Content() string {
} }
func (c *pdfBarcode) ColorModel() color.Model { func (c *pdfBarcode) ColorModel() color.Model {
return utils.ColorModel(c.depth) return c.color.Model
} }
func (c *pdfBarcode) Bounds() image.Rectangle { 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 { func (c *pdfBarcode) At(x, y int) color.Color {
if c.code.GetBit((y/moduleHeight)*c.width + x) { 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
} }

View File

@ -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 // 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) bits, vi, err := mode.getEncoder()(content, level)
if err != nil { if err != nil {
return nil, err return nil, err
@ -63,23 +63,23 @@ func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding,
blocks := splitToBlocks(bits.IterateBytes(), vi) blocks := splitToBlocks(bits.IterateBytes(), vi)
data := blocks.interleave(vi) data := blocks.interleave(vi)
result := render(data, vi, depth) result := render(data, vi, color)
result.content = content result.content = content
return result, nil return result, nil
} }
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) { 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() dim := vi.modulWidth()
results := make([]*qrcode, 8) results := make([]*qrcode, 8)
for i := 0; i < 8; i++ { 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) { setAll := func(x int, y int, val bool) {
occupied.Set(x, y, true) occupied.Set(x, y, true)

View File

@ -13,7 +13,7 @@ type qrcode struct {
dimension int dimension int
data *utils.BitList data *utils.BitList
content string content string
depth int color barcode.ColorScheme
} }
func (qr *qrcode) Content() string { func (qr *qrcode) Content() string {
@ -25,7 +25,7 @@ func (qr *qrcode) Metadata() barcode.Metadata {
} }
func (qr *qrcode) ColorModel() color.Model { func (qr *qrcode) ColorModel() color.Model {
return utils.ColorModel(qr.depth) return qr.color.Model
} }
func (qr *qrcode) Bounds() image.Rectangle { 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 { func (qr *qrcode) At(x, y int) color.Color {
if qr.Get(x, y) { 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 { func (qr *qrcode) Get(x, y int) bool {
@ -159,14 +159,14 @@ func (qr *qrcode) calcPenaltyRule4() uint {
return uint(math.Min(floor, ceil) * 10) 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 := new(qrcode)
res.dimension = dim res.dimension = dim
res.data = utils.NewBitList(dim * dim) res.data = utils.NewBitList(dim * dim)
res.depth = depth res.color = color
return res return res
} }
func newBarcode(dim int) *qrcode { func newBarcode(dim int) *qrcode {
return newBarCodeWithDepth(dim, 16) return newBarCodeWithColor(dim, barcode.ColorScheme16)
} }

View File

@ -78,8 +78,8 @@ func AddCheckSum(content string) (string, error) {
return content + string(utils.IntToRune(sum%10)), nil return content + string(utils.IntToRune(sum%10)), nil
} }
// Encode creates a codabar barcode for the given content and depth // Encode creates a codabar barcode for the given content and color scheme
func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barcode, error) { func EncodeWithColor(content string, interleaved bool, color barcode.ColorScheme) (barcode.Barcode, error) {
if content == "" { if content == "" {
return nil, errors.New("content is empty") 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...) resBits.AddBit(mode.end...)
if interleaved { if interleaved {
return utils.New1DCodeWithDepth(barcode.Type2of5Interleaved, content, resBits, depth), nil return utils.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil
} else { } 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 // Encode creates a codabar barcode for the given content
func Encode(content string, interleaved bool) (barcode.Barcode, error) { func Encode(content string, interleaved bool) (barcode.Barcode, error) {
return EncodeWithDepth(content, interleaved, 16) return EncodeWithColor(content, interleaved, barcode.ColorScheme16)
} }

View File

@ -12,7 +12,7 @@ type base1DCode struct {
*BitList *BitList
kind string kind string
content string content string
depth int color barcode.ColorScheme
} }
type base1DCodeIntCS struct { type base1DCodeIntCS struct {
@ -29,7 +29,7 @@ func (c *base1DCode) Metadata() barcode.Metadata {
} }
func (c *base1DCode) ColorModel() color.Model { func (c *base1DCode) ColorModel() color.Model {
return ColorModel(c.depth) return c.color.Model
} }
func (c *base1DCode) Bounds() image.Rectangle { 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 { func (c *base1DCode) At(x, y int) color.Color {
if c.GetBit(x) { 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 { 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 // 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 { 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 // 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 { func New1DCodeIntCheckSumWithColor(codeKind, content string, bars *BitList, checksum int, color barcode.ColorScheme) barcode.BarcodeIntCS {
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, depth}, checksum} 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 // 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 { 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 // 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 { func New1DCodeWithColor(codeKind, content string, bars *BitList, color barcode.ColorScheme) barcode.Barcode {
return &base1DCode{bars, codeKind, content, depth} return &base1DCode{bars, codeKind, content, color}
} }