diff --git a/QRCoder/BitmapByteQRCode.cs b/QRCoder/BitmapByteQRCode.cs
index 2faf6fb5..33bd843e 100644
--- a/QRCoder/BitmapByteQRCode.cs
+++ b/QRCoder/BitmapByteQRCode.cs
@@ -46,7 +46,7 @@ public BitmapByteQRCode(QRCodeData data) : base(data) { }
/// The number of pixels each dark/light module of the QR code will occupy in the final QR code image.
/// Returns the QR code graphic as a bitmap byte array.
public byte[] GetGraphic(int pixelsPerModule)
- => GetGraphic(pixelsPerModule, [0x00, 0x00, 0x00], [0xFF, 0xFF, 0xFF]);
+ => GetGraphicCore(pixelsPerModule, [0x00, 0x00, 0x00], [0xFF, 0xFF, 0xFF]);
///
/// Returns the QR code graphic as a bitmap byte array.
@@ -56,7 +56,18 @@ public byte[] GetGraphic(int pixelsPerModule)
/// The color of the light modules in HTML hex format.
/// Returns the QR code graphic as a bitmap byte array.
public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex)
+#if HAS_SPAN
+ {
+ Span scratch = stackalloc byte[8];
+ Span darkColor = scratch.Slice(0, 3);
+ Span lightColor = scratch.Slice(3, 3);
+ darkColorHtmlHex.HexColorToBytes(darkColor, allowExtraBytes: true);
+ lightColorHtmlHex.HexColorToBytes(lightColor, allowExtraBytes: true);
+ return GetGraphicCore(pixelsPerModule, darkColor, lightColor);
+ }
+#else
=> GetGraphic(pixelsPerModule, darkColorHtmlHex.HexColorToByteArray(), lightColorHtmlHex.HexColorToByteArray());
+#endif
///
/// Returns the QR code graphic as a bitmap byte array.
@@ -66,6 +77,14 @@ public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
/// The color of the light modules as an RGB byte array.
/// Returns the QR code graphic as a bitmap byte array.
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightColorRgb)
+ => GetGraphicCore(pixelsPerModule, darkColorRgb, lightColorRgb);
+
+ private byte[] GetGraphicCore(int pixelsPerModule,
+#if HAS_SPAN
+ ReadOnlySpan darkColorRgb, ReadOnlySpan lightColorRgb)
+#else
+ byte[] darkColorRgb, byte[] lightColorRgb)
+#endif
{
// Pre-calculate color/module bytes
int bytesPerModule = pixelsPerModule * 3;
diff --git a/QRCoder/Extensions/StringExtensions.cs b/QRCoder/Extensions/StringExtensions.cs
index 1e9ce717..8f76fc9e 100644
--- a/QRCoder/Extensions/StringExtensions.cs
+++ b/QRCoder/Extensions/StringExtensions.cs
@@ -42,6 +42,7 @@ internal static bool StartsWith(this string value, char c)
=> value.Length > 0 && value[0] == c;
#endif
+#if !HAS_SPAN
///
/// Converts a hex color string to a byte array.
///
@@ -54,13 +55,30 @@ internal static byte[] HexColorToByteArray(this string colorString)
offset = 1;
byte[] byteColor = new byte[(colorString.Length - offset) / 2];
for (int i = 0; i < byteColor.Length; i++)
-#if HAS_SPAN
- byteColor[i] = byte.Parse(colorString.AsSpan(i * 2 + offset, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
-#else
byteColor[i] = byte.Parse(colorString.Substring(i * 2 + offset, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
-#endif
return byteColor;
}
+#else
+ ///
+ /// Converts a hex color string into a caller-provided buffer without allocating.
+ ///
+ /// Color in HEX format like #ffffff.
+ /// The buffer receiving the color bytes.
+ /// Whether to parse and discard color bytes beyond the buffer length.
+ internal static void HexColorToBytes(this string colorString, Span color, bool allowExtraBytes = false)
+ {
+ var offset = colorString.StartsWith('#') ? 1 : 0;
+ var byteCount = (colorString.Length - offset) / 2;
+ if (byteCount < color.Length || (!allowExtraBytes && byteCount != color.Length))
+ throw new ArgumentException("Color length must match the destination buffer length", nameof(color));
+
+ for (int i = 0; i < color.Length; i++)
+ color[i] = byte.Parse(colorString.AsSpan(i * 2 + offset, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
+
+ for (int i = color.Length; i < byteCount; i++)
+ _ = byte.Parse(colorString.AsSpan(i * 2 + offset, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
+ }
+#endif
#if NETSTANDARD1_3
///
diff --git a/QRCoder/PdfByteQRCode.cs b/QRCoder/PdfByteQRCode.cs
index 8e26f20a..c0c73f6d 100644
--- a/QRCoder/PdfByteQRCode.cs
+++ b/QRCoder/PdfByteQRCode.cs
@@ -48,10 +48,18 @@ public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
var pdfMediaSize = imgSize * 72 / (float)dpi;
// Parse colors
+#if HAS_SPAN
+ Span scratch = stackalloc byte[8];
+ Span darkColor = scratch.Slice(0, 3);
+ Span lightColor = scratch.Slice(3, 3);
+ darkColorHtmlHex.HexColorToBytes(darkColor);
+ lightColorHtmlHex.HexColorToBytes(lightColor);
+#else
var darkColor = darkColorHtmlHex.HexColorToByteArray();
var lightColor = lightColorHtmlHex.HexColorToByteArray();
ValidateColor(darkColor);
ValidateColor(lightColor);
+#endif
//Create PDF document
using var stream = new MemoryStream();
@@ -237,6 +245,7 @@ private void CreatePathFromModules(StringBuilder pathCommands)
}
}
+#if !HAS_SPAN
///
/// Validates that an RGB color contains exactly three bytes.
///
@@ -246,6 +255,7 @@ private static void ValidateColor(byte[] color)
if (color.Length != 3)
throw new ArgumentException("Color must be a 3-byte RGB array", nameof(color));
}
+#endif
}
///
diff --git a/QRCoderTests/BitmapByteQRCodeRendererTests.cs b/QRCoderTests/BitmapByteQRCodeRendererTests.cs
index c7d529ea..af4ae78f 100644
--- a/QRCoderTests/BitmapByteQRCodeRendererTests.cs
+++ b/QRCoderTests/BitmapByteQRCodeRendererTests.cs
@@ -21,6 +21,54 @@ public void can_render_bitmapbyte_qrcode_color_bytearray()
bmp.ShouldMatchApprovedImage();
}
+ [Theory]
+ [InlineData(1, "#123456", "#E7F0FA")]
+ [InlineData(3, "#123456", "#E7F0FA")]
+ [InlineData(1, "123456", "E7F0FA")]
+ [InlineData(3, "123456", "E7F0FA")]
+ [InlineData(1, "#123456", "#e7F0fA")]
+ [InlineData(3, "#123456", "#e7F0fA")]
+ [InlineData(1, "#123456Z", "#E7F0FAZ")]
+ [InlineData(3, "#123456Z", "#E7F0FAZ")]
+ [InlineData(1, "#12345678", "#E7F0FA90")]
+ [InlineData(3, "#12345678", "#E7F0FA90")]
+ public void equivalent_hex_and_rgb_colors_render_identical_bitmap_bytes(int pixelsPerModule, string darkColor, string lightColor)
+ {
+ using var gen = new QRCodeGenerator();
+ using var data = gen.CreateQrCode("Equivalent bitmap colors", QRCodeGenerator.ECCLevel.L);
+ using var bitmapCode = new BitmapByteQRCode(data);
+
+ var expected = bitmapCode.GetGraphic(pixelsPerModule, new byte[] { 0x12, 0x34, 0x56 }, new byte[] { 0xE7, 0xF0, 0xFA });
+
+ bitmapCode.GetGraphic(pixelsPerModule, darkColor, lightColor).ShouldBe(expected);
+ }
+
+ [Theory]
+ [InlineData(1)]
+ [InlineData(3)]
+ public void default_colors_render_identical_bitmap_bytes_to_explicit_rgb_colors(int pixelsPerModule)
+ {
+ using var gen = new QRCodeGenerator();
+ using var data = gen.CreateQrCode("Default bitmap colors", QRCodeGenerator.ECCLevel.L);
+ using var bitmapCode = new BitmapByteQRCode(data);
+
+ var expected = bitmapCode.GetGraphic(pixelsPerModule, new byte[] { 0, 0, 0 }, new byte[] { 255, 255, 255 });
+
+ bitmapCode.GetGraphic(pixelsPerModule).ShouldBe(expected);
+ }
+
+ [Theory]
+ [InlineData("#123456GG", "#E7F0FA")]
+ [InlineData("#123456", "#E7F0FAGG")]
+ public void rejects_invalid_hex_digits_in_extra_color_bytes(string darkColor, string lightColor)
+ {
+ using var gen = new QRCodeGenerator();
+ using var data = gen.CreateQrCode("Invalid extra bitmap color bytes", QRCodeGenerator.ECCLevel.L);
+ using var bitmapCode = new BitmapByteQRCode(data);
+
+ Should.Throw(() => bitmapCode.GetGraphic(1, darkColor, lightColor));
+ }
+
[Fact]
public void can_render_bitmapbyte_qrcode_drawing_color()
{
diff --git a/QRCoderTests/PdfByteQRCodeRendererTests.cs b/QRCoderTests/PdfByteQRCodeRendererTests.cs
index 68708928..bd60351f 100644
--- a/QRCoderTests/PdfByteQRCodeRendererTests.cs
+++ b/QRCoderTests/PdfByteQRCodeRendererTests.cs
@@ -32,6 +32,50 @@ public void can_render_pdfbyte_qrcode_custom_dpi()
pdfCodeGfx.ShouldMatchApproved("pdf");
}
+ [Theory]
+ [InlineData("123456", "#123456")]
+ [InlineData("#aBcDeF", "#ABCDEF")]
+ [InlineData("#123456Z", "#123456")]
+ [InlineData("# 1 2 3", "#010203")]
+ public void equivalent_hex_colors_render_identical_pdf_bytes(string color, string expectedColor)
+ {
+ using var gen = new QRCodeGenerator();
+ using var data = gen.CreateQrCode("Equivalent PDF colors", QRCodeGenerator.ECCLevel.L);
+ using var pdfCode = new PdfByteQRCode(data);
+
+ pdfCode.GetGraphic(3, color, "#FFFFFF").ShouldBe(pdfCode.GetGraphic(3, expectedColor, "#FFFFFF"));
+ pdfCode.GetGraphic(3, "#000000", color).ShouldBe(pdfCode.GetGraphic(3, "#000000", expectedColor));
+ }
+
+ [Theory]
+ [InlineData("")]
+ [InlineData("#")]
+ [InlineData("#1234")]
+ [InlineData("#12345678")]
+ public void rejects_colors_with_incorrect_rgb_length(string color)
+ {
+ using var gen = new QRCodeGenerator();
+ using var data = gen.CreateQrCode("Invalid PDF colors", QRCodeGenerator.ECCLevel.L);
+ using var pdfCode = new PdfByteQRCode(data);
+
+ Should.Throw(() => pdfCode.GetGraphic(3, color, "#FFFFFF"));
+ Should.Throw(() => pdfCode.GetGraphic(3, "#000000", color));
+ }
+
+ [Theory]
+ [InlineData("#GG3456")]
+ [InlineData("#12GG56")]
+ [InlineData("#1234GG")]
+ public void rejects_colors_with_invalid_hex_digits(string color)
+ {
+ using var gen = new QRCodeGenerator();
+ using var data = gen.CreateQrCode("Invalid PDF colors", QRCodeGenerator.ECCLevel.L);
+ using var pdfCode = new PdfByteQRCode(data);
+
+ Should.Throw(() => pdfCode.GetGraphic(3, color, "#FFFFFF"));
+ Should.Throw(() => pdfCode.GetGraphic(3, "#000000", color));
+ }
+
[Fact]
public void renders_identical_pdf_bytes_across_cultures()
{