Skip to content

Commit

Permalink
add color set temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KangSpace committed Sep 15, 2022
1 parent 74dfade commit b67e8f3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
31 changes: 23 additions & 8 deletions core/output/image_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type ImageOutput struct {
*BaseOutput
}

func NewOutput(output *BaseOutput) *ImageOutput {
out := &ImageOutput{BaseOutput: output}
out.initImage(output.Size)
return out
}

func NewPNGOutput(size int) *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: PNG, Size: size}}
out.initImage(size)
Expand Down Expand Up @@ -77,21 +83,26 @@ func (out *ImageOutput) Init(version *model.Version, qz *model.QuietZone) {

// Write : write data
func (out *ImageOutput) Write(x int, y int, black bool) {
setColor := image.White
if black {
setColor = image.Black
}
setColor := out.getWriteColor(black)
out.image.Set(x, y, setColor)
out.modules[x][y] = &black
}

// WriteModule : write data
func (out *ImageOutput) WriteModule(x int, y int, black bool, pixelSize int) {
setColor := image.White
setColor := out.getWriteColor(black)
out.WriteModuleColor(x, y, black, setColor, pixelSize)
}

// getWriteColor: get color by BaseOutput.CodeColor
func (out *ImageOutput) getWriteColor(black bool) color.Color {
if black {
setColor = image.Black
if out.BaseOutput.CodeColor.DataColor != nil {
return out.BaseOutput.CodeColor.DataColor
}
return image.Black.C
}
out.WriteModuleColor(x, y, black, setColor, pixelSize)
return image.White.C
}

func (out *ImageOutput) WriteModuleColor(x int, y int, dark bool, setColor color.Color, pixelSize int) {
Expand Down Expand Up @@ -240,9 +251,13 @@ func (out *ImageOutput) ResizeToFit(moduleSize int, quietZoneSize int, pixelSize
return
}

func (out *ImageOutput) GetColor() CodeColor {
return DefaultCodeColor
}

// Clone : Shallow copy BaseOutput and modules from output, init new image instance
func (out *ImageOutput) Clone() Output {
clone := &ImageOutput{BaseOutput: &BaseOutput{Type: out.Type, Size: out.Size, Options: out.Options}}
clone := &ImageOutput{BaseOutput: &BaseOutput{Type: out.Type, Size: out.Size, Options: out.Options, CodeColor: out.CodeColor}}
clone.initImage(out.Size)
return clone
}
30 changes: 27 additions & 3 deletions core/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,34 @@ type BaseOutput struct {
// image type
Type Type
// image width/height
Size int
Options []*Option
modules [][]*bool
Size int
Options []*Option
CodeColor CodeColor
modules [][]*bool
}

// CodeColor color for qrcode
type CodeColor struct {
AlignmentColor color.Color
TimingPatternColor color.Color
QuietZoneColor color.Color
DataColor color.Color
}

//
var (
// DefaultCodeColor default color is black for QRCode
DefaultCodeColor = CodeColor{image.Black, image.Black, image.White, image.Black}
// BlueCodeColor #007FFF/rgba(0,127,255,1)
BlueCodeColor = CodeColor{color.RGBA{0, 127, 255, 1}, color.RGBA{0, 127, 255, 1}, image.White, color.RGBA{0, 127, 255, 1}}
// RedCodeColor #FF5219/rgba(255,82,25,1)
RedCodeColor = CodeColor{color.RGBA{255, 82, 25, 1}, color.RGBA{255, 82, 25, 1}, image.White, color.RGBA{255, 82, 25, 1}}
// OrangeCodeColor #FF9200/rgba(255,146,0,1)
OrangeCodeColor = CodeColor{color.RGBA{255, 146, 0, 1}, color.RGBA{255, 146, 0, 1}, image.White, color.RGBA{255, 146, 0, 1}}
// GreenCodeColor #00B042/rgba(0,176,66,1)
GreenCodeColor = CodeColor{color.RGBA{0, 176, 66, 1}, color.RGBA{0, 176, 66, 1}, image.White, color.RGBA{0, 176, 66, 1}}
)

// LogoOption : Option for add logo image at center of QRCode
func LogoOption(logoImage string) *Option {
return &Option{Name: LogoOptionName, Value: logoImage}
Expand Down Expand Up @@ -87,6 +110,7 @@ type Output interface {
Save(fileName string) error
SaveToWriter(w io.Writer) error
SaveToBase64() (string, error)
GetColor() CodeColor
}

// EvalPenalty :Evaluate Penalty for QRCode.
Expand Down
14 changes: 14 additions & 0 deletions test/qrcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,20 @@ func TestByteQRCode(t *testing.T) {
t.Fatal(err)
}
}
func TestByteQRCodeWithColor(t *testing.T) {
data := "https://kangspace.org"
fmt.Println(len(data))
fileNamePrefix := "byte_color"
fileName := gqrcodePath + fileNamePrefix + ".gif"
out := output.NewOutput(&output.BaseOutput{Type: output.PNG, Size: 400, CodeColor: output.BlueCodeColor})
qrcode, err := gqrcode.NewQRCodeAutoQuiet(data)
if err != nil {
t.Fatal(err)
}
qrcode.Encode(out, fileName)
fmt.Printf("version:%v, size:%d, ec:%v mode:%s \n", qrcode.Version, out.Size, qrcode.ErrorCorrection, qrcode.Mode.GetMode())
fmt.Println("fileName," + fileName)
}

// TestByteQRCodeBase64 :Test Base64 output string
func TestByteQRCodeBase64(t *testing.T) {
Expand Down

0 comments on commit b67e8f3

Please sign in to comment.