Skip to content

Commit

Permalink
Remove class Color / renatem ToBitmap to avoid name collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Oct 13, 2023
1 parent 21459f5 commit 18e3f8e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 143 deletions.
120 changes: 0 additions & 120 deletions QrCodeGenerator/Color.cs

This file was deleted.

20 changes: 7 additions & 13 deletions QrCodeGenerator/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal string ToGraphicsPath(int border)
return path.ToString();
}

internal byte[] ToBitmap(int border, int scale, Color foreground, Color background)
internal byte[] ToBmpBitmap(int border, int scale, int foreground, int background)
{
if (scale < 1)
{
Expand Down Expand Up @@ -163,14 +163,14 @@ internal byte[] ToBitmap(int border, int scale, Color foreground, Color backgrou
// NOTE: Color table
// Alpha isn't useful here
// Foreground - Dark
buf[54] = foreground.Blue;
buf[55] = foreground.Green;
buf[56] = foreground.Red;
buf[54] = (byte)foreground; // blue
buf[55] = (byte)(foreground >> 8); // green
buf[56] = (byte)(foreground >> 16); // red

// Background - Light
buf[58] = background.Blue;
buf[59] = background.Green;
buf[60] = background.Red;
buf[58] = (byte)background; // blue
buf[59] = (byte)(background >> 8); // green
buf[60] = (byte)(background >> 16); // red;

var scaledBorder = border * scale;

Expand Down Expand Up @@ -246,12 +246,6 @@ internal byte[] ToBitmap(int border, int scale, Color foreground, Color backgrou
return buf;
}

internal byte[] ToBitmap(int border, int scale)
{
return ToBitmap(border, scale, Color.Black, Color.White);
}


// Append a SVG/XAML path for the QR code to the provided string builder
private void CreatePath(StringBuilder path, bool[,] modules, int border)
{
Expand Down
42 changes: 33 additions & 9 deletions QrCodeGenerator/QrCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,30 +422,54 @@ public string ToGraphicsPath(int border = 0)
return AsGraphics().ToGraphicsPath(border);
}

/// <inheritdoc cref="ToBitmap(int, int)"/>
/// <summary>
/// Creates 1 bpp bitmap (BMP) data.
/// Creates a bitmap in the BMP format.
/// <para>
/// The bitmap uses 1 bit per pixel and a color table with 2 entries.
/// </para>
/// <para>
/// Color values can be created with <see cref="RgbColor(byte, byte, byte)"/>.
/// </para>
/// </summary>
/// <param name="border">The border width, as a factor of the module (QR code pixel) size.</param>
/// <param name="scale">The width and height, in pixels, of each module.</param>
/// <param name="foreground">The foreground (dark modules) color.</param>
/// <param name="background">The background (light modules) color.</param>
public byte[] ToBitmap(int border, int scale, Color foreground, Color background)
/// <param name="foreground">The foreground color (dark modules), in RGB value (little endian).</param>
/// <param name="background">The background color (light modules), in RGB value (little endian).</param>
/// <returns>Bitmap data</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="border"/> is negative,
/// <paramref name="scale"/> is less than 1 or the resulting image is wider than 32,768 pixels.</exception>
public byte[] ToBmpBitmap(int border, int scale, int foreground, int background)
{
return AsGraphics().ToBitmap(border, scale, foreground, background);
return AsGraphics().ToBmpBitmap(border, scale, foreground, background);
}

/// <summary>
/// Creates 1 bpp bitmap (BMP) data using black for dark modules and white for light modules.
/// Creates bitmap in the BMP format data using black for dark modules and white for light modules.
/// <para>
/// The bitmap uses 1 bit per pixel and a color table with 2 entries.
/// </para>
/// </summary>
/// <param name="border">The border width, as a factor of the module (QR code pixel) size.</param>
/// <param name="scale">The width and height, in pixels, of each module.</param>
/// <returns>Bitmap data</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="border"/> is negative,
/// <paramref name="scale"/> is less than 1 or the resulting image is wider than 32,768 pixels.</exception>
public byte[] ToBitmap(int border = 0, int scale = 1)
public byte[] ToBmpBitmap(int border = 0, int scale = 1)
{
return ToBitmap(border, scale, Color.Black, Color.White);
return ToBmpBitmap(border, scale, 0x000000, 0xffffff);
}

/// <summary>
/// Creates an RGB color value in little endian format.
/// </summary>
/// <param name="red">Red component.</param>
/// <param name="green">Green component.</param>
/// <param name="blue">Blue component.</param>
/// <returns>RGB color value</returns>
public int RgbColor(byte red, byte green, byte blue)
{

return (red << 16) | (green << 8) | blue;
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion QrCodeGeneratorTest/QrCodeBitmapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void ToMonochromeBitmap_CreatesValidBitmap(

var qrCode = QrCode.EncodeText(stubText, stubEcc);

var actualBitmap = qrCode.ToBitmap(stubBorder, stubScale);
var actualBitmap = qrCode.ToBmpBitmap(stubBorder, stubScale);
var actualBase64 = Convert.ToBase64String(actualBitmap);

Assert.Equal(expectedBitmapBase64, actualBase64);
Expand Down

0 comments on commit 18e3f8e

Please sign in to comment.