Explorar o código

Merge pull request #14 from onodera-punpun/master

Add WriteColorFile
tfh %!s(int64=8) %!d(string=hai) anos
pai
achega
6eeeae4c62
Modificáronse 2 ficheiros con 38 adicións e 4 borrados
  1. 7 1
      README.md
  2. 31 3
      qrcode.go

+ 7 - 1
README.md

@@ -27,7 +27,13 @@ A command-line tool `qrcode` will be built into `$GOPATH/bin/`.
 
         err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
 
-Both examples use the `qrcode.Medium` error Recovery Level and create a 256x256 pixel, black on white QR Code.
+- **Create a PNG image with custom colors and write to file:**
+
+        err := qrcode.WriteColorFile("https://example.org", qrcode.Medium, 256, color.Black, color.White, "qr.png")
+
+All examples use the qrcode.Medium error Recovery Level and create a fixed
+256x256px size QR Code. The last function creates a white on black instead of black
+on white QR Code.
 
 The maximum capacity of a QR Code varies according to the content encoded and
 the error recovery level. The maximum capacity is 2,953 bytes, 4,296

+ 31 - 3
qrcode.go

@@ -12,7 +12,7 @@ obscured codes. There are four levels of error recovery: qrcode.{Low, Medium,
 High, Highest}. QR Codes with a higher recovery level are more robust to damage,
 at the cost of being physically larger.
 
-Two functions cover most use cases:
+Three functions cover most use cases:
 
 - Create a PNG image:
 
@@ -23,8 +23,13 @@ Two functions cover most use cases:
 
 	err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
 
-Both examples use the qrcode.Medium error Recovery Level and create a fixed
-256x256px size, black on white QR Code.
+- Create a PNG image with custom colors and write to file:
+
+	err := qrcode.WriteColorFile("https://example.org", qrcode.Medium, 256, color.Black, color.White, "qr.png")
+
+All examples use the qrcode.Medium error Recovery Level and create a fixed
+256x256px size QR Code. The last function creates a white on black instead of black
+on white QR Code.
 
 To generate a variable sized image instead, specify a negative size (in place of
 the 256 above), such as -4 or -5. Larger negative numbers create larger images:
@@ -94,6 +99,29 @@ func WriteFile(content string, level RecoveryLevel, size int, filename string) e
 	return q.WriteFile(size, filename)
 }
 
+// WriteColorFile encodes, then writes a QR Code to the given filename in PNG format.
+// With WriteColorFile you can also specify the colors you want to use.
+//
+// size is both the image width and height in pixels. If size is too small then
+// a larger image is silently written. Negative values for size cause a variable
+// sized image to be written: See the documentation for Image().
+func WriteColorFile(content string, level RecoveryLevel, size int, background,
+	foreground color.Color, filename string) error {
+
+	var q *QRCode
+
+	q, err := New(content, level)
+
+	q.BackgroundColor = background
+	q.ForegroundColor = foreground
+
+	if err != nil {
+		return err
+	}
+
+	return q.WriteFile(size, filename)
+}
+
 // A QRCode represents a valid encoded QRCode.
 type QRCode struct {
 	// Original content encoded.