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

Function EncodeWithColor

code128/encode.go:159–184  ·  view source on GitHub ↗

Encode creates a Code 128 barcode for the given content and color scheme

(content string, color barcode.ColorScheme)

Source from the content-addressed store, hash-verified

157
158// Encode creates a Code 128 barcode for the given content and color scheme
159func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
160 contentRunes := strToRunes(content)
161 if len(contentRunes) <= 0 || len(contentRunes) > 80 {
162 return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
163 }
164 idxList := getCodeIndexList(contentRunes)
165
166 if idxList == nil {
167 return nil, fmt.Errorf("\"%s\" could not be encoded", content)
168 }
169
170 result := new(utils.BitList)
171 sum := 0
172 for i, idx := range idxList.GetBytes() {
173 if i == 0 {
174 sum = int(idx)
175 } else {
176 sum += i * int(idx)
177 }
178 result.AddBit(encodingTable[idx]...)
179 }
180 sum = sum % 103
181 result.AddBit(encodingTable[sum]...)
182 result.AddBit(encodingTable[stopSymbol]...)
183 return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode128, content, result, sum, color), nil
184}
185
186// Encode creates a Code 128 barcode for the given content
187func Encode(content string) (barcode.BarcodeIntCS, error) {

Callers 1

EncodeFunction · 0.70

Calls 5

strToRunesFunction · 0.85
getCodeIndexListFunction · 0.85
GetBytesMethod · 0.80
AddBitMethod · 0.80

Tested by

no test coverage detected