Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EndroidQrCodeProvider: allow transparent for color #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Buffele
Copy link

@Buffele Buffele commented Dec 20, 2024

This enables support for the EndroidQrCodeProvider to allow transparent to be passed for color or bgcolor.

It is useful for bgcolor, but probably not too much for color.
That could be restricted. However, I do not really think that is needed.

@NicolasCARPi
Copy link
Collaborator

Hello,

Thank you for your contribution. Please also document this functionality in the documentation.

Copy link
Collaborator

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, thanks for the fix. I always forget transparency in PHP is sort of backwards!

Also passing non-hex characters to hexdec (i.e. tr, an, sp) currently raises a deprecation warning so probably something else to catch in the future when they make it a more dramatic failure.

Would it be worth adding some check to confirm that $split is exactly 3 elements long too? Or perhaps checking that $color is exactly 6 characters if it isn't transparent...

@RobThree
Copy link
Owner

RobThree commented Dec 20, 2024

Maybe I'm mistaken but Endroid QR appears to support Alpha?

Then shouldn't this work?

$split = str_split($color, 2);
if (sizeof($split) >= 3) {
  $r = hexdec($split[0]);
  $g = hexdec($split[1]);
  $b = hexdec($split[2]);
  $a = sizeof($split) >= 4 ? hexdec($split[3]) : 0;
}

Handling could be a lot better, maybe even a regex. But the general idea should work, shouldn't it?

Edit:

function parseColor(string $color) : Color {
  if (preg_match('/^#?([0-9a-f]{3,8})$/i', trim($color), $matches) !== false) {
    $len = strlen($matches[1]);
    if ($len !== 5 && $len !== 7) {
        list($r, $g, $b, $a) = str_split($matches[1], $len <= 4 ? 1 : 2);
        return new Color(hexdec($r), hexdec($g), hexdec($b), hexdec($a));
    }
  }
  throw new Exception('Invalid color format');
}

Should handle optional starting # and then RGB, RGBA, RRGGBB and RRGGBBAA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants