diff --git a/Colorized-barcode.md b/Colorized-barcode.md new file mode 100644 index 0000000..7b153e8 --- /dev/null +++ b/Colorized-barcode.md @@ -0,0 +1,43 @@ +To colorize a barcode you could use the following code: + +```golang +package main + +import ( + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/qr" + "image/color" + "image/png" + "os" +) + +type coloredBarcode struct { + barcode.Barcode + cm color.Model + fg color.Color + bg color.Color +} + +func NewColoredBarcode(bc barcode.Barcode, cm color.Model, foreground color.Color, background color.Color) barcode.Barcode { + return &coloredBarcode{bc, cm, foreground, background} +} +func (bc *coloredBarcode) ColorModel() color.Model { + return bc.cm +} +func (bc *coloredBarcode) At(x, y int) color.Color { + c := bc.Barcode.At(x, y) + if c == color.Black { + return bc.fg + } else { + return bc.bg + } +} + +func main() { + bc, _ := qr.Encode("foobar", qr.L, qr.Auto) + bc = NewColoredBarcode(bc, color.RGBAModel, color.RGBA{255, 0, 0, 255}, color.RGBA{255, 255, 0, 255}) + f, _ := os.Create("foobar.png") + defer f.Close() + png.Encode(f, bc) +} +``` \ No newline at end of file