Skip to content

Commit 938aa89

Browse files
committed
fix: apply opacity to currentColor for fill and stroke on Apple platforms
When currentColor is used as a fill or stroke brush, the fillOpacity and strokeOpacity values were ignored because the color was passed directly without applying the opacity multiplier. This creates a copy of the color with the correct alpha using CGColorCreateCopyWithAlpha.
1 parent d355e7d commit 938aa89

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

apple/Brushes/RNSVGContextBrush.mm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ - (BOOL)applyFillColor:(CGContextRef)context opacity:(CGFloat)opacity
4949
BOOL fillColor;
5050

5151
if (brush.class == RNSVGBrush.class) {
52-
CGContextSetFillColorWithColor(context, [element getCurrentColor]);
52+
CGColorRef currentColor = [element getCurrentColor];
53+
CGColorRef colorWithOpacity = CGColorCreateCopyWithAlpha(currentColor, opacity * CGColorGetAlpha(currentColor));
54+
CGContextSetFillColorWithColor(context, colorWithOpacity);
55+
CGColorRelease(colorWithOpacity);
5356
fillColor = YES;
5457
} else {
5558
fillColor = [brush applyFillColor:context opacity:opacity];
@@ -70,7 +73,10 @@ - (BOOL)applyStrokeColor:(CGContextRef)context opacity:(CGFloat)opacity
7073
BOOL strokeColor;
7174

7275
if (brush.class == RNSVGBrush.class) {
73-
CGContextSetStrokeColorWithColor(context, [element getCurrentColor]);
76+
CGColorRef currentColor = [element getCurrentColor];
77+
CGColorRef colorWithOpacity = CGColorCreateCopyWithAlpha(currentColor, opacity * CGColorGetAlpha(currentColor));
78+
CGContextSetStrokeColorWithColor(context, colorWithOpacity);
79+
CGColorRelease(colorWithOpacity);
7480
strokeColor = YES;
7581
} else {
7682
strokeColor = [brush applyStrokeColor:context opacity:opacity];

apple/RNSVGRenderable.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,11 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
555555

556556
if (self.fill) {
557557
if (self.fill.class == RNSVGBrush.class) {
558-
CGContextSetFillColorWithColor(context, [self getCurrentColor]);
558+
CGColorRef currentColor = [self getCurrentColor];
559+
CGColorRef colorWithOpacity =
560+
CGColorCreateCopyWithAlpha(currentColor, self.fillOpacity * CGColorGetAlpha(currentColor));
561+
CGContextSetFillColorWithColor(context, colorWithOpacity);
562+
CGColorRelease(colorWithOpacity);
559563
fillColor = YES;
560564
} else {
561565
fillColor = [self.fill applyFillColor:context opacity:self.fillOpacity];
@@ -603,7 +607,11 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
603607
BOOL strokeColor;
604608

605609
if (self.stroke.class == RNSVGBrush.class) {
606-
CGContextSetStrokeColorWithColor(context, [self getCurrentColor]);
610+
CGColorRef currentColor = [self getCurrentColor];
611+
CGColorRef colorWithOpacity =
612+
CGColorCreateCopyWithAlpha(currentColor, self.strokeOpacity * CGColorGetAlpha(currentColor));
613+
CGContextSetStrokeColorWithColor(context, colorWithOpacity);
614+
CGColorRelease(colorWithOpacity);
607615
strokeColor = YES;
608616
} else {
609617
strokeColor = [self.stroke applyStrokeColor:context opacity:self.strokeOpacity];

apple/Text/RNSVGTSpan.mm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
134134
if (self.inlineSize != nil && self.inlineSize.value != 0) {
135135
if (self.fill) {
136136
if (self.fill.class == RNSVGBrush.class) {
137-
CGColorRef color = [self getCurrentColor];
137+
CGColorRef currentColor = [self getCurrentColor];
138+
CGColorRef color = CGColorCreateCopyWithAlpha(currentColor, self.fillOpacity * CGColorGetAlpha(currentColor));
138139
[self drawWrappedText:context gc:gc rect:rect color:color];
140+
CGColorRelease(color);
139141
} else {
140142
CGColorRef color = [self.fill getColorWithOpacity:self.fillOpacity];
141143
[self drawWrappedText:context gc:gc rect:rect color:color];
@@ -144,8 +146,11 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
144146
}
145147
if (self.stroke) {
146148
if (self.stroke.class == RNSVGBrush.class) {
147-
CGColorRef color = [self getCurrentColor];
149+
CGColorRef currentColor = [self getCurrentColor];
150+
CGColorRef color =
151+
CGColorCreateCopyWithAlpha(currentColor, self.strokeOpacity * CGColorGetAlpha(currentColor));
148152
[self drawWrappedText:context gc:gc rect:rect color:color];
153+
CGColorRelease(color);
149154
} else {
150155
CGColorRef color = [self.stroke getColorWithOpacity:self.strokeOpacity];
151156
[self drawWrappedText:context gc:gc rect:rect color:color];

0 commit comments

Comments
 (0)