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

Percentage RGB color not rendering #29

Open
jjyoh opened this issue May 7, 2020 · 2 comments
Open

Percentage RGB color not rendering #29

jjyoh opened this issue May 7, 2020 · 2 comments

Comments

@jjyoh
Copy link

jjyoh commented May 7, 2020

Percentage RGB color (e.g. <path style="fill:rgb(100%,100%,100%);">) is not rendering.

@jjyoh
Copy link
Author

jjyoh commented May 8, 2020

Here's a script to replace the % format to the 255.

//Find if there is any percentage RGB
const regexRGB = /rgba?\s*\(\s*(\d{1,3})\%\s*\,\s*(\d{1,3})\%\s*\,\s*(\d{1,3})\%\s*\,*\s*(?:,\s*(\d?(?:\.\d+)?))?\s*\)/gm;
let m;

while ((m = regexRGB.exec(stringSVG)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regexRGB.lastIndex) {
      regexRGB.lastIndex++;
    }
    rgb = m[0]
    r = m[1], g = m[2], b = m[3], a = m[4]

  new_r = Math.round(255 * (parseInt(r) / 100))
  new_g = Math.round(255 * (parseInt(g) / 100))
  new_b = Math.round(255 * (parseInt(b) / 100))
    new_rgb = 'rgb(' + new_r.toString() + ',' + new_g.toString() + ',' +  new_b.toString() + ')'

    if (a) {
      new_rgb = 'rgba(' + new_r.toString() + ',' + new_g.toString() + ',' +  new_b.toString() + ',' + a.toString() + ')'
    }

    stringSVG = stringSVG.replace(rgb, new_rgb)

}

@fuzhenn
Copy link
Owner

fuzhenn commented Jun 11, 2020

Great!

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

No branches or pull requests

2 participants