Created Colorized barcode (markdown)
							parent
							
								
									6c3ae9a576
								
							
						
					
					
						commit
						41c121428e
					
				
							
								
								
									
										43
									
								
								Colorized-barcode.md
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
								
									
										
										
									
									
								
							
						
						
									
										43
									
								
								Colorized-barcode.md
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user