Skip to content

[SvgDrawVisitor] - Problem with Locale when String.format(...) is used #860

@paulo-maia

Description

@paulo-maia

Dear @johnmay,

I noticed a weird problem when trying to use a transparent background color and writing the Depiction to SVG format.

The problem is that you assume that the Locale used by String.format() will be en/US of any other locale (ROOT) that uses the dot (.) as a decimal separator, however in my Locale (pt/PT) the default decimal separator is the comma (,). This causes the method toStr(Color col):

String toStr(Color col) {
        if (col.getAlpha() == 255) {
            return String.format("#%06X", (0xFFFFFF & col.getRGB()));
        } else {
            return String.format("rgba(%d,%d,%d,%.2f)", col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha()/255d);
        }
    }

to write something like rgba(255,0,0,0,00) to the SVG tree instead of rgba(255,0,0,0.00) when the alpha channel in the col is different than 255 (0.00 for alpha value of 0).

What happens as a result is that the element will be colored weirdly (in my case was always coloring red instead of being transparent) I noticed this in the background rectangle, but this will happen for other SVG filled elements.

The fix is quite easy, simply use LOCALE.ROOT as an argument to the String.format(...) method:

return String.format(Locale.ROOT, "rgba(%d,%d,%d,%.2f)", col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha()/255d);

thanks for the great library

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions