codebude / QRCoder

A pure C# Open Source QR Code implementation
MIT License
4.46k stars 1.09k forks source link

Why not use Color struct instead of byte[] in color parameters? #470

Closed doggy8088 closed 3 months ago

doggy8088 commented 11 months ago

Type of issue

[ ] Bug
[x] Question (e.g. about handling/usage)
[ ] Request for new feature/improvement

Expected Behavior

In the GetGraphic method from the QRCoder.PngByteQRCode class, the darkColorRgba and the lightColorRgba use byte[] to represent RGBA color. I expected to use Color struct. It's much easier.

// QRCoder.PngByteQRCode
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true)
{
    using (PngBuilder pngBuilder = new PngBuilder())
    {
        int num = (base.QrCodeData.ModuleMatrix.Count - ((!drawQuietZones) ? 8 : 0)) * pixelsPerModule;
        pngBuilder.WriteHeader(num, num, 1, PngBuilder.ColorType.Indexed);
        pngBuilder.WritePalette(darkColorRgba, lightColorRgba);
        pngBuilder.WriteScanlines(DrawScanlines(pixelsPerModule, drawQuietZones));
        pngBuilder.WriteEnd();
        return pngBuilder.GetBytes();
    }
}

Current Behavior

The byte[] is not convenient on the usage.

Possible Solution (optional)

Provide pleomorphism that accept Color struct.

Your Environment

codebude commented 3 months ago

I decided to pass colors as byte[] to keep the PngByteQRCode free from any Windows dependencies. By adding System.Drawing just for the Color-class we had a dependency that isn't available on all platforms/in all environments. This would add another layer of complexity - in the codebase as also on the documentation side.

You can see in the compatibility matrix, that PngByteQRCode is one of the few renderers that works on all platforms and I really want to keep this feature: https://github.com/codebude/QRCoder/wiki/Advanced-usage---QR-Code-renderers#2-overview-of-the-different-renderers