Refactor to use ColorScheme instead of 'depth' approach
This commit is contained in:
1 parent
e966fa7106
commit
30509e0dd7
16 files changed
+83
-81
No files matched your search
+6
-6
@@ -55,7 +55,7 @@ func (e Encoding) String() string {
|
||||
}
|
||||
|
||||
// Encode returns a QR barcode with the given content, error correction level and uses the given encoding
|
||||
func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding, depth int) (barcode.Barcode, error) {
|
||||
func EncodeWithColor(content string, level ErrorCorrectionLevel, mode Encoding, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
bits, vi, err := mode.getEncoder()(content, level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -63,23 +63,23 @@ func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding,
|
||||
|
||||
blocks := splitToBlocks(bits.IterateBytes(), vi)
|
||||
data := blocks.interleave(vi)
|
||||
result := render(data, vi, depth)
|
||||
result := render(data, vi, color)
|
||||
result.content = content
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(content, level, mode, 16)
|
||||
return EncodeWithColor(content, level, mode, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func render(data []byte, vi *versionInfo, depth int) *qrcode {
|
||||
func render(data []byte, vi *versionInfo, color barcode.ColorScheme) *qrcode {
|
||||
dim := vi.modulWidth()
|
||||
results := make([]*qrcode, 8)
|
||||
for i := 0; i < 8; i++ {
|
||||
results[i] = newBarCodeWithDepth(dim, depth)
|
||||
results[i] = newBarCodeWithColor(dim, color)
|
||||
}
|
||||
|
||||
occupied := newBarCodeWithDepth(dim, depth)
|
||||
occupied := newBarCodeWithColor(dim, color)
|
||||
|
||||
setAll := func(x int, y int, val bool) {
|
||||
occupied.Set(x, y, true)
|
||||
|
||||
+7
-7
@@ -13,7 +13,7 @@ type qrcode struct {
|
||||
dimension int
|
||||
data *utils.BitList
|
||||
content string
|
||||
depth int
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
func (qr *qrcode) Content() string {
|
||||
@@ -25,7 +25,7 @@ func (qr *qrcode) Metadata() barcode.Metadata {
|
||||
}
|
||||
|
||||
func (qr *qrcode) ColorModel() color.Model {
|
||||
return utils.ColorModel(qr.depth)
|
||||
return qr.color.Model
|
||||
}
|
||||
|
||||
func (qr *qrcode) Bounds() image.Rectangle {
|
||||
@@ -34,9 +34,9 @@ func (qr *qrcode) Bounds() image.Rectangle {
|
||||
|
||||
func (qr *qrcode) At(x, y int) color.Color {
|
||||
if qr.Get(x, y) {
|
||||
return utils.BlackColor(qr.depth)
|
||||
return qr.color.Foreground
|
||||
}
|
||||
return utils.WhiteColor(qr.depth)
|
||||
return qr.color.Background
|
||||
}
|
||||
|
||||
func (qr *qrcode) Get(x, y int) bool {
|
||||
@@ -159,14 +159,14 @@ func (qr *qrcode) calcPenaltyRule4() uint {
|
||||
return uint(math.Min(floor, ceil) * 10)
|
||||
}
|
||||
|
||||
func newBarCodeWithDepth(dim int, depth int) *qrcode {
|
||||
func newBarCodeWithColor(dim int, color barcode.ColorScheme) *qrcode {
|
||||
res := new(qrcode)
|
||||
res.dimension = dim
|
||||
res.data = utils.NewBitList(dim * dim)
|
||||
res.depth = depth
|
||||
res.color = color
|
||||
return res
|
||||
}
|
||||
|
||||
func newBarcode(dim int) *qrcode {
|
||||
return newBarCodeWithDepth(dim, 16)
|
||||
return newBarCodeWithColor(dim, barcode.ColorScheme16)
|
||||
}
|
||||
Reference in new issue
Block a user