MCPcopy Create free account
hub / github.com/boombuler/barcode / EncodeWithColor

Function EncodeWithColor

ean/encoder.go:161–187  ·  view source on GitHub ↗

Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme

(code string, color barcode.ColorScheme)

Source from the content-addressed store, hash-verified

159
160// Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme
161func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
162 var checkSum int
163 if len(code) == 7 || len(code) == 12 {
164 code += string(calcCheckNum(code))
165 checkSum = utils.RuneToInt(calcCheckNum(code))
166 } else if len(code) == 8 || len(code) == 13 {
167 check := code[0 : len(code)-1]
168 check += string(calcCheckNum(check))
169 if check != code {
170 return nil, errors.New("checksum missmatch")
171 }
172 checkSum = utils.RuneToInt(rune(code[len(code)-1]))
173 }
174
175 if len(code) == 8 {
176 result := encodeEAN8(code)
177 if result != nil {
178 return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN8, code, result, checkSum, color), nil
179 }
180 } else if len(code) == 13 {
181 result := encodeEAN13(code)
182 if result != nil {
183 return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN13, code, result, checkSum, color), nil
184 }
185 }
186 return nil, errors.New("invalid ean code data")
187}
188
189// Encode returns a EAN 8 or EAN 13 barcode for the given code
190func Encode(code string) (barcode.BarcodeIntCS, error) {

Callers 1

EncodeFunction · 0.70

Calls 5

RuneToIntFunction · 0.92
calcCheckNumFunction · 0.85
encodeEAN8Function · 0.85
encodeEAN13Function · 0.85

Tested by

no test coverage detected