From 41c121428ed8a0f41666eb5097a03dc5761344cd Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 7 Dec 2017 14:44:32 +0100 Subject: [PATCH] Created Colorized barcode (markdown) --- Colorized-barcode.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Colorized-barcode.md 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