Skip to content

Commit

Permalink
Add ScaleWithFill
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaori96 committed Jul 31, 2024
1 parent 30509e0 commit 2c9211e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions scaledbarcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ func (bc *intCSscaledBC) CheckSum() int {

// Scale returns a resized barcode with the given width and height.
func Scale(bc Barcode, width, height int) (Barcode, error) {
return ScaleWithFill(bc, width, height, color.White)
}

// Scale returns a resized barcode with the given width, height and fill color.
func ScaleWithFill(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
switch bc.Metadata().Dimensions {
case 1:
return scale1DCode(bc, width, height)
return scale1DCode(bc, width, height, fill)
case 2:
return scale2DCode(bc, width, height)
return scale2DCode(bc, width, height, fill)
}

return nil, errors.New("unsupported barcode format")
Expand All @@ -72,7 +77,7 @@ func newScaledBC(wrapped Barcode, wrapperFunc wrapFunc, rect image.Rectangle) Ba
return result
}

func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
func scale2DCode(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
orgBounds := bc.Bounds()
orgWidth := orgBounds.Max.X - orgBounds.Min.X
orgHeight := orgBounds.Max.Y - orgBounds.Min.Y
Expand All @@ -87,12 +92,12 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {

wrap := func(x, y int) color.Color {
if x < offsetX || y < offsetY {
return color.White
return fill
}
x = (x - offsetX) / factor
y = (y - offsetY) / factor
if x >= orgWidth || y >= orgHeight {
return color.White
return fill
}
return bc.At(x, y)
}
Expand All @@ -104,7 +109,7 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
), nil
}

func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
func scale1DCode(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
orgBounds := bc.Bounds()
orgWidth := orgBounds.Max.X - orgBounds.Min.X
factor := int(float64(width) / float64(orgWidth))
Expand All @@ -116,12 +121,12 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) {

wrap := func(x, y int) color.Color {
if x < offsetX {
return color.White
return fill
}
x = (x - offsetX) / factor

if x >= orgWidth {
return color.White
return fill
}
return bc.At(x, 0)
}
Expand Down

0 comments on commit 2c9211e

Please sign in to comment.