Skip to content

Specifying alpha channel in css color via hex causes result color to be black #1065

@nitz

Description

@nitz

Description

If a CSS color is specified using a hex string that includes the alpha channel, the resulting color is black regardless of value. Specifying the alpha channel another way (e.g.: via rgb()) seems to work as expected though.

Example data

Reproduction example. (via LINQPad)

void Main()
{
	string template = 
"""
<svg viewBox="0 0 192 64" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <style type="text/css">
    text {{
      fill: {0};
    }}
    rect {{
      fill: #ffffff;
    }}
  </style>
  <rect width="100%" height="100%" />
  <text x="32" y="32" class="name">{1}</text>
</svg>
""";

	var svgs = new string[] {
		string.Format(template, "rgb(255, 0, 0, 255)", "Rgb red w/ alpha"),
		string.Format(template, "rgb(255, 0, 0, 128)", "Rgb red w/ alpha 128"),
		string.Format(template, "rgb(255, 0, 0)", "Rgb red w/ *no* alpha"),
		string.Format(template, "#ff0000ff", "Hex red w/ alpha 0xFF"),
		string.Format(template, "#ff000080", "Hex red w/ alpha 0x80"),
		string.Format(template, "#ff0000", "Hex red w/ *no* alpha"),
	};

	foreach (var svg in svgs)
	{
		var svgDoc = SvgDocument.FromSvg<SvgDocument>(svg);
		var bitmap = svgDoc.Draw();
		bitmap.Dump();
	}
}

Output result:
image

Used Versions

Svg v3.4.4 (Nuget)
.NET v7.0.5 (Roslyn v4.6.0-3.23259.8)
OS Windows 10 Pro For Workstations 22H2 19045.3086

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions