Merge pull request #84 from zhaori96/allow-other-depths

Allow other color schemes and models for barcodes.
This commit is contained in:
Florian 2024-08-03 07:01:56 +02:00 committed by GitHub
commit ca3e24f327
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 233 additions and 71 deletions

View File

@ -13,10 +13,11 @@ type aztecCode struct {
*utils.BitList *utils.BitList
size int size int
content []byte content []byte
color barcode.ColorScheme
} }
func newAztecCode(size int) *aztecCode { func newAztecCode(size int, color barcode.ColorScheme) *aztecCode {
return &aztecCode{utils.NewBitList(size * size), size, nil} return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16}
} }
func (c *aztecCode) Content() string { func (c *aztecCode) Content() string {
@ -28,7 +29,11 @@ func (c *aztecCode) Metadata() barcode.Metadata {
} }
func (c *aztecCode) ColorModel() color.Model { func (c *aztecCode) ColorModel() color.Model {
return color.Gray16Model return c.color.Model
}
func (c *aztecCode) ColorScheme() barcode.ColorScheme {
return c.color
} }
func (c *aztecCode) Bounds() image.Rectangle { func (c *aztecCode) Bounds() image.Rectangle {
@ -37,9 +42,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 color.Black return c.color.Foreground
} }
return color.White return c.color.Background
} }
func (c *aztecCode) set(x, y int) { func (c *aztecCode) set(x, y int) {

View File

@ -124,6 +124,11 @@ 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 EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16)
}
// 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) bits := highlevelEncode(data)
eccBits := ((bits.Len() * minECCPercent) / 100) + 11 eccBits := ((bits.Len() * minECCPercent) / 100) + 11
totalSizeBits := bits.Len() + eccBits totalSizeBits := bits.Len() + eccBits
@ -215,7 +220,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba
alignmentMap[origCenter+i] = center + newOffset + 1 alignmentMap[origCenter+i] = center + newOffset + 1
} }
} }
code := newAztecCode(matrixSize) code := newAztecCode(matrixSize, color)
code.content = data code.content = data
// draw data bits // draw data bits

View File

@ -1,6 +1,8 @@
package barcode package barcode
import "image" import (
"image"
)
const ( const (
TypeAztec = "Aztec" TypeAztec = "Aztec"
@ -40,3 +42,7 @@ type BarcodeIntCS interface {
Barcode Barcode
CheckSum() int CheckSum() int
} }
type BarcodeColor interface {
ColorScheme() ColorScheme
}

View File

@ -32,8 +32,8 @@ var encodingTable = map[rune][]bool{
'D': []bool{true, false, true, false, false, true, true, false, false, true}, '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 Encode(content string) (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,5 +45,10 @@ func Encode(content string) (barcode.Barcode, error) {
} }
resBits.AddBit(encodingTable[r]...) resBits.AddBit(encodingTable[r]...)
} }
return utils.New1DCode(barcode.TypeCodabar, content, resBits), 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 EncodeWithColor(content, barcode.ColorScheme16)
} }

View File

@ -155,8 +155,8 @@ func getCodeIndexList(content []rune) *utils.BitList {
return result 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 Encode(content string) (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,10 +180,19 @@ func Encode(content string) (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.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), 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 EncodeWithColor(content, barcode.ColorScheme16)
} }
func EncodeWithoutChecksum(content string) (barcode.Barcode, 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) 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))
@ -199,5 +208,5 @@ func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
result.AddBit(encodingTable[idx]...) result.AddBit(encodingTable[idx]...)
} }
result.AddBit(encodingTable[stopSymbol]...) result.AddBit(encodingTable[stopSymbol]...)
return utils.New1DCode(barcode.TypeCode128, content, result), nil return utils.New1DCodeWithColor(barcode.TypeCode128, content, result, color), nil
} }

View File

@ -111,9 +111,9 @@ func prepare(content string) (string, error) {
return result, nil 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 // 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 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,5 +148,11 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
if err != nil { if err != nil {
checkSum = 0 checkSum = 0
} }
return utils.New1DCodeIntCheckSum(barcode.TypeCode39, content, result, int(checkSum)), 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 EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
} }

View File

@ -74,9 +74,9 @@ func prepare(content string) (string, error) {
return result, nil return result, nil
} }
// Encode returns a code93 barcode for the given content // 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 // 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 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)
@ -104,7 +104,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
} }
result.AddBit(true) result.AddBit(true)
return utils.New1DCode(barcode.TypeCode93, content, result), 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 EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
} }
func getChecksum(content string, maxWeight int) rune { func getChecksum(content string, maxWeight int) rune {

39
color_scheme.go Normal file
View File

@ -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},
}

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,13 +13,15 @@ type codeLayout struct {
matrix *utils.BitList matrix *utils.BitList
occupy *utils.BitList occupy *utils.BitList
size *dmCodeSize size *dmCodeSize
color barcode.ColorScheme
} }
func newCodeLayout(size *dmCodeSize) *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.color = color
return result return result
} }
@ -159,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) {
} }
func (l *codeLayout) Merge() *datamatrixCode { func (l *codeLayout) Merge() *datamatrixCode {
result := newDataMatrixCode(l.size) 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,10 +12,15 @@ type datamatrixCode struct {
*utils.BitList *utils.BitList
*dmCodeSize *dmCodeSize
content string content string
color barcode.ColorScheme
}
func newDataMatrixCodeWithColor(size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
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, ""} return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", barcode.ColorScheme16}
} }
func (c *datamatrixCode) Content() string { func (c *datamatrixCode) Content() string {
@ -27,7 +32,11 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
} }
func (c *datamatrixCode) ColorModel() color.Model { func (c *datamatrixCode) ColorModel() color.Model {
return color.Gray16Model return c.color.Model
}
func (c *datamatrixCode) ColorScheme() barcode.ColorScheme {
return c.color
} }
func (c *datamatrixCode) Bounds() image.Rectangle { func (c *datamatrixCode) Bounds() image.Rectangle {
@ -36,9 +45,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 color.Black return c.color.Foreground
} }
return color.White return c.color.Background
} }
func (c *datamatrixCode) get(x, y int) bool { func (c *datamatrixCode) get(x, y int) bool {

View File

@ -7,8 +7,8 @@ import (
"github.com/boombuler/barcode" "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 Encode(content string) (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 Encode(content string) (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) code := render(data, size, color)
if code != nil { if code != nil {
code.content = content code.content = content
return code, nil return code, nil
@ -31,8 +31,13 @@ func Encode(content string) (barcode.Barcode, error) {
return nil, errors.New("unable to render barcode") return nil, errors.New("unable to render barcode")
} }
func render(data []byte, size *dmCodeSize) *datamatrixCode { // Encode returns a Datamatrix barcode for the given content
cl := newCodeLayout(size) func Encode(content string) (barcode.Barcode, error) {
return EncodeWithColor(content, barcode.ColorScheme16)
}
func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
cl := newCodeLayout(size, color)
cl.SetValues(data) cl.SetValues(data)
@ -69,8 +74,8 @@ 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
} }

View File

@ -157,8 +157,8 @@ func encodeEAN13(code string) *utils.BitList {
return result 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 Encode(code string) (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,13 +175,18 @@ func Encode(code string) (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.New1DCodeIntCheckSum(barcode.TypeEAN8, code, result, checkSum), 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.New1DCodeIntCheckSum(barcode.TypeEAN13, code, result, checkSum), 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")
} }
// Encode returns a EAN 8 or EAN 13 barcode for the given code
func Encode(code string) (barcode.BarcodeIntCS, error) {
return EncodeWithColor(code, barcode.ColorScheme16)
}

View File

@ -12,10 +12,10 @@ const (
padding_codeword = 900 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 // 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 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,6 +34,7 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
barcode := new(pdfBarcode) barcode := new(pdfBarcode)
barcode.data = data barcode.data = data
barcode.color = color
codeWords, err := encodeData(dataWords, columns, sl) codeWords, err := encodeData(dataWords, columns, sl)
if err != nil { if err != nil {
@ -70,6 +71,13 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
return barcode, nil 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 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) {
dataCount := len(dataWords) dataCount := len(dataWords)

View File

@ -12,6 +12,7 @@ type pdfBarcode struct {
data string data string
width int width int
code *utils.BitList code *utils.BitList
color barcode.ColorScheme
} }
func (c *pdfBarcode) Metadata() barcode.Metadata { func (c *pdfBarcode) Metadata() barcode.Metadata {
@ -23,7 +24,11 @@ func (c *pdfBarcode) Content() string {
} }
func (c *pdfBarcode) ColorModel() color.Model { func (c *pdfBarcode) ColorModel() color.Model {
return color.Gray16Model return c.color.Model
}
func (c *pdfBarcode) ColorScheme() barcode.ColorScheme {
return c.color
} }
func (c *pdfBarcode) Bounds() image.Rectangle { func (c *pdfBarcode) Bounds() image.Rectangle {
@ -34,7 +39,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 color.Black return c.color.Foreground
} }
return color.White return c.color.Background
} }

View File

@ -54,8 +54,8 @@ func (e Encoding) String() string {
return "" 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 Encode(content string, level ErrorCorrectionLevel, mode Encoding) (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,19 +63,23 @@ func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.
blocks := splitToBlocks(bits.IterateBytes(), vi) blocks := splitToBlocks(bits.IterateBytes(), vi)
data := blocks.interleave(vi) data := blocks.interleave(vi)
result := render(data, vi) result := render(data, vi, color)
result.content = content result.content = content
return result, nil return result, nil
} }
func render(data []byte, vi *versionInfo) *qrcode { func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
return EncodeWithColor(content, level, mode, barcode.ColorScheme16)
}
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] = newBarcode(dim) results[i] = newBarCodeWithColor(dim, color)
} }
occupied := newBarcode(dim) 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,6 +13,7 @@ type qrcode struct {
dimension int dimension int
data *utils.BitList data *utils.BitList
content string content string
color barcode.ColorScheme
} }
func (qr *qrcode) Content() string { func (qr *qrcode) Content() string {
@ -24,7 +25,11 @@ func (qr *qrcode) Metadata() barcode.Metadata {
} }
func (qr *qrcode) ColorModel() color.Model { func (qr *qrcode) ColorModel() color.Model {
return color.Gray16Model return qr.color.Model
}
func (c *qrcode) ColorScheme() barcode.ColorScheme {
return c.color
} }
func (qr *qrcode) Bounds() image.Rectangle { func (qr *qrcode) Bounds() image.Rectangle {
@ -33,9 +38,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 color.Black return qr.color.Foreground
} }
return color.White return qr.color.Background
} }
func (qr *qrcode) Get(x, y int) bool { func (qr *qrcode) Get(x, y int) bool {
@ -158,9 +163,14 @@ func (qr *qrcode) calcPenaltyRule4() uint {
return uint(math.Min(floor, ceil) * 10) return uint(math.Min(floor, ceil) * 10)
} }
func newBarcode(dim 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.color = color
return res return res
} }
func newBarcode(dim int) *qrcode {
return newBarCodeWithColor(dim, barcode.ColorScheme16)
}

View File

@ -49,11 +49,22 @@ func (bc *intCSscaledBC) CheckSum() int {
// Scale returns a resized barcode with the given width and height. // Scale returns a resized barcode with the given width and height.
func Scale(bc Barcode, width, height int) (Barcode, error) { func Scale(bc Barcode, width, height int) (Barcode, error) {
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.
func ScaleWithFill(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
switch bc.Metadata().Dimensions { switch bc.Metadata().Dimensions {
case 1: case 1:
return scale1DCode(bc, width, height) return scale1DCode(bc, width, height, fill)
case 2: case 2:
return scale2DCode(bc, width, height) return scale2DCode(bc, width, height, fill)
} }
return nil, errors.New("unsupported barcode format") return nil, errors.New("unsupported barcode format")
@ -72,7 +83,7 @@ func newScaledBC(wrapped Barcode, wrapperFunc wrapFunc, rect image.Rectangle) Ba
return result 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() orgBounds := bc.Bounds()
orgWidth := orgBounds.Max.X - orgBounds.Min.X orgWidth := orgBounds.Max.X - orgBounds.Min.X
orgHeight := orgBounds.Max.Y - orgBounds.Min.Y orgHeight := orgBounds.Max.Y - orgBounds.Min.Y
@ -87,12 +98,12 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
wrap := func(x, y int) color.Color { wrap := func(x, y int) color.Color {
if x < offsetX || y < offsetY { if x < offsetX || y < offsetY {
return color.White return fill
} }
x = (x - offsetX) / factor x = (x - offsetX) / factor
y = (y - offsetY) / factor y = (y - offsetY) / factor
if x >= orgWidth || y >= orgHeight { if x >= orgWidth || y >= orgHeight {
return color.White return fill
} }
return bc.At(x, y) return bc.At(x, y)
} }
@ -104,7 +115,7 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
), nil ), 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() orgBounds := bc.Bounds()
orgWidth := orgBounds.Max.X - orgBounds.Min.X orgWidth := orgBounds.Max.X - orgBounds.Min.X
factor := int(float64(width) / float64(orgWidth)) factor := int(float64(width) / float64(orgWidth))
@ -116,12 +127,12 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
wrap := func(x, y int) color.Color { wrap := func(x, y int) color.Color {
if x < offsetX { if x < offsetX {
return color.White return fill
} }
x = (x - offsetX) / factor x = (x - offsetX) / factor
if x >= orgWidth { if x >= orgWidth {
return color.White return fill
} }
return bc.At(x, 0) return bc.At(x, 0)
} }

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 // Encode creates a codabar barcode for the given content and color scheme
func Encode(content string, interleaved bool) (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,8 +131,13 @@ func Encode(content string, interleaved bool) (barcode.Barcode, error) {
resBits.AddBit(mode.end...) resBits.AddBit(mode.end...)
if interleaved { if interleaved {
return utils.New1DCode(barcode.Type2of5Interleaved, content, resBits), nil return utils.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil
} else { } else {
return utils.New1DCode(barcode.Type2of5, content, resBits), 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 EncodeWithColor(content, interleaved, barcode.ColorScheme16)
}

View File

@ -12,6 +12,7 @@ type base1DCode struct {
*BitList *BitList
kind string kind string
content string content string
color barcode.ColorScheme
} }
type base1DCodeIntCS struct { type base1DCodeIntCS struct {
@ -28,7 +29,11 @@ func (c *base1DCode) Metadata() barcode.Metadata {
} }
func (c *base1DCode) ColorModel() color.Model { func (c *base1DCode) ColorModel() color.Model {
return color.Gray16Model return c.color.Model
}
func (c *base1DCode) ColorScheme() barcode.ColorScheme {
return c.color
} }
func (c *base1DCode) Bounds() image.Rectangle { func (c *base1DCode) Bounds() image.Rectangle {
@ -37,9 +42,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 color.Black return c.color.Foreground
} }
return color.White return c.color.Background
} }
func (c *base1DCodeIntCS) CheckSum() int { func (c *base1DCodeIntCS) CheckSum() int {
@ -48,10 +53,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}, 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 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 // 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} 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 New1DCodeWithColor(codeKind, content string, bars *BitList, color barcode.ColorScheme) barcode.Barcode {
return &base1DCode{bars, codeKind, content, color}
} }