Skip to content

Commit

Permalink
Add overload for supplying Color to PngByteQRCode.GetGraphic (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude committed Jun 20, 2024
2 parents 04d96ff + da38537 commit 81fad40
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions QRCoder/PngByteQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true)
return png.GetBytes();
}

#if !NETSTANDARD1_3
/// <summary>
/// Creates a 2-color PNG of the QR code, using 1-bit indexed color. Colors may contain transparency.
/// </summary>
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <param name="darkColor">The color of the dark modules.</param>
/// <param name="lightColor">The color of the light modules.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the QR code graphic as a PNG byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true)
=> GetGraphic(pixelsPerModule, new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A }, new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A }, drawQuietZones);
#endif

/// <summary>
/// Creates a 2-color PNG of the QR code, using 1-bit indexed color. Accepts 3-byte RGB colors for normal images and 4-byte RGBA-colors for transparent images.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ namespace QRCoder
public PngByteQRCode(QRCoder.QRCodeData data) { }
public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) { }
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true) { }
public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) { }
}
public static class PngByteQRCodeHelper
{
Expand Down
1 change: 1 addition & 0 deletions QRCoderApiTests/net60-windows/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ namespace QRCoder
public PngByteQRCode(QRCoder.QRCodeData data) { }
public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) { }
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true) { }
public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) { }
}
public static class PngByteQRCodeHelper
{
Expand Down
1 change: 1 addition & 0 deletions QRCoderApiTests/net60/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ namespace QRCoder
public PngByteQRCode(QRCoder.QRCodeData data) { }
public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) { }
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true) { }
public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) { }
}
public static class PngByteQRCodeHelper
{
Expand Down
24 changes: 24 additions & 0 deletions QRCoderTests/PngByteQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ public void can_render_pngbyte_qrcode_color()
#endif
}

#if !NETCOREAPP1_1
[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_drawing_color()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, Color.Red, Color.Blue);

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("0144b1d40aa6eeb6cb07df42822ea0a7");
#else
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("88d394b2405499869feb69b81593e703");
}
#endif
}
#endif


[Fact]
[Category("QRRenderer/PngByteQRCode")]
Expand Down

0 comments on commit 81fad40

Please sign in to comment.