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

Support parsing "rgba(...)" colors. #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/nanosvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ void nsvgDelete(NSVGimage* image);

#define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
#define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
#define NSVG_RGBA(r, g, b, a) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16) | ((unsigned int)a << 24))

#ifdef _MSC_VER
#pragma warning (disable: 4996) // Switch off security warnings
Expand Down Expand Up @@ -1236,6 +1237,19 @@ static unsigned int nsvg__parseColorRGB(const char* str)
}
}

static unsigned int nsvg__parseColorRGBA(const char* str)
{
int r = -1, g = -1, b = -1;
float a = -1;
char s1[32]="", s2[32]="", s3[32]="";
sscanf(str + 5, "%d%[%%, \t]%d%[%%, \t]%d%[%%, \t]%f", &r, s1, &g, s2, &b, s3, &a);
if (strchr(s1, '%')) {
return NSVG_RGBA((r*255)/100,(g*255)/100,(b*255)/100,(a*255)/100);
} else {
return NSVG_RGBA(r,g,b,(a*255));
}
}

typedef struct NSVGNamedColor {
const char* name;
unsigned int color;
Expand Down Expand Up @@ -1417,6 +1431,8 @@ static unsigned int nsvg__parseColor(const char* str)
return nsvg__parseColorHex(str);
else if (len >= 4 && str[0] == 'r' && str[1] == 'g' && str[2] == 'b' && str[3] == '(')
return nsvg__parseColorRGB(str);
else if (len >= 5 && str[0] == 'r' && str[1] == 'g' && str[2] == 'b' && str[3] == 'a' && str[4] == '(')
return nsvg__parseColorRGBA(str);
return nsvg__parseColorName(str);
}

Expand Down Expand Up @@ -1734,6 +1750,13 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value)
} else {
attr->hasFill = 1;
attr->fillColor = nsvg__parseColor(value);
// if the fillColor has an alpha value then use it to
// set the fillOpacity
if (attr->fillColor & 0xFF000000) {
attr->fillOpacity = ((attr->fillColor >> 24) & 0xFF) / 255.0;
// remove the alpha value from the color
attr->fillColor &= 0x00FFFFFF;
}
}
} else if (strcmp(name, "opacity") == 0) {
attr->opacity = nsvg__parseOpacity(value);
Expand All @@ -1748,6 +1771,13 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value)
} else {
attr->hasStroke = 1;
attr->strokeColor = nsvg__parseColor(value);
// if the strokeColor has an alpha value then use it to
// set the strokeOpacity
if (attr->strokeColor & 0xFF000000) {
attr->strokeOpacity = ((attr->strokeColor >> 24) & 0xFF) / 255.0;
// remove the alpha value from the color
attr->strokeColor &= 0x00FFFFFF;
}
}
} else if (strcmp(name, "stroke-width") == 0) {
attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
Expand Down