diff --git a/aztec/azteccode.go b/aztec/azteccode.go index 8ffa7fd..c757ae6 100644 --- a/aztec/azteccode.go +++ b/aztec/azteccode.go @@ -13,10 +13,11 @@ type aztecCode struct { *utils.BitList size int content []byte + color barcode.ColorScheme } -func newAztecCode(size int) *aztecCode { - return &aztecCode{utils.NewBitList(size * size), size, nil} +func newAztecCode(size int, color barcode.ColorScheme) *aztecCode { + return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16} } func (c *aztecCode) Content() string { @@ -28,7 +29,11 @@ func (c *aztecCode) Metadata() barcode.Metadata { } 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 { @@ -37,9 +42,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 c.color.Foreground } - return color.White + return c.color.Background } func (c *aztecCode) set(x, y int) { diff --git a/aztec/encoder.go b/aztec/encoder.go index e62f8fb..a63650d 100644 --- a/aztec/encoder.go +++ b/aztec/encoder.go @@ -124,6 +124,11 @@ 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 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) 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, color) code.content = data // draw data bits 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 +} diff --git a/codabar/encoder.go b/codabar/encoder.go index 8e0f3f6..74689d0 100644 --- a/codabar/encoder.go +++ b/codabar/encoder.go @@ -32,8 +32,8 @@ 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 -func Encode(content string) (barcode.Barcode, error) { +// 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, "!") != "!" { 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.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) } diff --git a/code128/encode.go b/code128/encode.go index 3a00f6c..9a101b6 100644 --- a/code128/encode.go +++ b/code128/encode.go @@ -155,8 +155,8 @@ func getCodeIndexList(content []rune) *utils.BitList { return result } -// Encode creates a Code 128 barcode for the given content -func Encode(content string) (barcode.BarcodeIntCS, error) { +// 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 { 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 result.AddBit(encodingTable[sum]...) 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) { + 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)) @@ -199,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 } diff --git a/code39/encoder.go b/code39/encoder.go index 63b3e71..f474173 100644 --- a/code39/encoder.go +++ b/code39/encoder.go @@ -111,9 +111,9 @@ 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 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 { 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.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) } diff --git a/code93/encoder.go b/code93/encoder.go index 87c4395..27140c3 100644 --- a/code93/encoder.go +++ b/code93/encoder.go @@ -74,9 +74,9 @@ func prepare(content string) (string, error) { 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 -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 { var err error content, err = prepare(content) @@ -104,7 +104,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B } 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 { 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}, +} diff --git a/datamatrix/codelayout.go b/datamatrix/codelayout.go index 923b135..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,13 +13,15 @@ type codeLayout struct { matrix *utils.BitList occupy *utils.BitList size *dmCodeSize + color barcode.ColorScheme } -func newCodeLayout(size *dmCodeSize) *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.color = color return result } @@ -159,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) { } func (l *codeLayout) Merge() *datamatrixCode { - result := newDataMatrixCode(l.size) + 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 8f53021..7e7ca26 100644 --- a/datamatrix/datamatrixcode.go +++ b/datamatrix/datamatrixcode.go @@ -12,10 +12,15 @@ type datamatrixCode struct { *utils.BitList *dmCodeSize 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 { - 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 { @@ -27,7 +32,11 @@ func (c *datamatrixCode) Metadata() barcode.Metadata { } 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 { @@ -36,9 +45,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 c.color.Foreground } - return color.White + return c.color.Background } func (c *datamatrixCode) get(x, y int) bool { diff --git a/datamatrix/encoder.go b/datamatrix/encoder.go index ea852e8..9501bee 100644 --- a/datamatrix/encoder.go +++ b/datamatrix/encoder.go @@ -7,8 +7,8 @@ import ( "github.com/boombuler/barcode" ) -// Encode returns a Datamatrix barcode for the given content -func Encode(content string) (barcode.Barcode, error) { +// 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) 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, color) 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 EncodeWithColor(content, barcode.ColorScheme16) +} + +func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode { + cl := newCodeLayout(size, color) cl.SetValues(data) @@ -69,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 a2190f1..4a80706 100644 --- a/ean/encoder.go +++ b/ean/encoder.go @@ -157,8 +157,8 @@ func encodeEAN13(code string) *utils.BitList { return result } -// Encode returns a EAN 8 or EAN 13 barcode for the given code -func Encode(code string) (barcode.BarcodeIntCS, error) { +// 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 { 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.New1DCodeIntCheckSumWithColor(barcode.TypeEAN8, code, result, checkSum, color), nil } } else if len(code) == 13 { result := encodeEAN13(code) 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") } + +// 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) +} diff --git a/pdf417/encoder.go b/pdf417/encoder.go index f7922d4..3425ac7 100644 --- a/pdf417/encoder.go +++ b/pdf417/encoder.go @@ -12,10 +12,10 @@ 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 Encode(data string, securityLevel byte) (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,6 +34,7 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) { barcode := new(pdfBarcode) barcode.data = data + barcode.color = color 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 EncodeWithColor(data, securityLevel, barcode.ColorScheme16) +} + 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..a594bd2 100644 --- a/pdf417/pdfcode.go +++ b/pdf417/pdfcode.go @@ -12,6 +12,7 @@ type pdfBarcode struct { data string width int code *utils.BitList + color barcode.ColorScheme } func (c *pdfBarcode) Metadata() barcode.Metadata { @@ -23,7 +24,11 @@ func (c *pdfBarcode) Content() string { } 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 { @@ -34,7 +39,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 c.color.Foreground } - return color.White + return c.color.Background } diff --git a/qr/encoder.go b/qr/encoder.go index 2c6ab21..81ceb88 100644 --- a/qr/encoder.go +++ b/qr/encoder.go @@ -54,8 +54,8 @@ func (e Encoding) String() string { return "" } -// 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) { +// 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 { 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, color) 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 EncodeWithColor(content, level, mode, barcode.ColorScheme16) +} + +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] = newBarcode(dim) + results[i] = newBarCodeWithColor(dim, color) } - occupied := newBarcode(dim) + 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 1360760..2fb44ab 100644 --- a/qr/qrcode.go +++ b/qr/qrcode.go @@ -13,6 +13,7 @@ type qrcode struct { dimension int data *utils.BitList content string + color barcode.ColorScheme } func (qr *qrcode) Content() string { @@ -24,7 +25,11 @@ func (qr *qrcode) Metadata() barcode.Metadata { } 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 { @@ -33,9 +38,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 qr.color.Foreground } - return color.White + return qr.color.Background } func (qr *qrcode) Get(x, y int) bool { @@ -158,9 +163,14 @@ func (qr *qrcode) calcPenaltyRule4() uint { return uint(math.Min(floor, ceil) * 10) } -func newBarcode(dim int) *qrcode { +func newBarCodeWithColor(dim int, color barcode.ColorScheme) *qrcode { res := new(qrcode) res.dimension = dim res.data = utils.NewBitList(dim * dim) + res.color = color return res } + +func newBarcode(dim int) *qrcode { + return newBarCodeWithColor(dim, barcode.ColorScheme16) +} diff --git a/scaledbarcode.go b/scaledbarcode.go index 152b180..491cc3f 100644 --- a/scaledbarcode.go +++ b/scaledbarcode.go @@ -49,11 +49,22 @@ 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) { + 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 { 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 +83,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 +98,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 +115,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 +127,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) } diff --git a/twooffive/encoder.go b/twooffive/encoder.go index 8ad0946..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 -func Encode(content string, interleaved bool) (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,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.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil } 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) +} diff --git a/utils/base1dcode.go b/utils/base1dcode.go index a335c0c..1b77e4f 100644 --- a/utils/base1dcode.go +++ b/utils/base1dcode.go @@ -12,6 +12,7 @@ type base1DCode struct { *BitList kind string content string + color barcode.ColorScheme } type base1DCodeIntCS struct { @@ -28,7 +29,11 @@ func (c *base1DCode) Metadata() barcode.Metadata { } 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 { @@ -37,9 +42,9 @@ func (c *base1DCode) Bounds() image.Rectangle { func (c *base1DCode) At(x, y int) color.Color { if c.GetBit(x) { - return color.Black + return c.color.Foreground } - return color.White + return c.color.Background } 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 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 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} }