Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme
(code string, color barcode.ColorScheme)
| 159 | |
| 160 | // Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme |
| 161 | func 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 |
| 190 | func Encode(code string) (barcode.BarcodeIntCS, error) { |
no test coverage detected