Skip to content

Commit

Permalink
Add overload for color to PngByteQRCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Jun 11, 2024
1 parent d7b9c81 commit ed0c16f
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 @@ -43,6 +43,19 @@ public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true)
}
}

#if !NETSTANDARD1_3
/// <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>
/// <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 @@ -65,6 +65,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 ed0c16f

Please sign in to comment.