@@ -477,10 +477,7 @@ func (svg *svgParser) parseDefs(l *xml.Lexer) {
477477 stopColor := svg .parseColor (tag .attrs ["stop-color" ])
478478 if v , ok := tag .attrs ["stop-opacity" ]; ok {
479479 stopOpacity := svg .parseNumber (v )
480- stopColor .R = uint8 (float64 (stopColor .R ) / float64 (stopColor .A ) * stopOpacity * 255.0 )
481- stopColor .G = uint8 (float64 (stopColor .G ) / float64 (stopColor .A ) * stopOpacity * 255.0 )
482- stopColor .B = uint8 (float64 (stopColor .B ) / float64 (stopColor .A ) * stopOpacity * 255.0 )
483- stopColor .A = uint8 (stopOpacity * 255.0 )
480+ stopColor = ToOpacity (stopColor , stopOpacity )
484481 }
485482 grad .Add (offset , stopColor )
486483 }
@@ -552,10 +549,7 @@ func (svg *svgParser) parseDefs(l *xml.Lexer) {
552549 stopColor := svg .parseColor (tag .attrs ["stop-color" ])
553550 if v , ok := tag .attrs ["stop-opacity" ]; ok {
554551 stopOpacity := svg .parseNumber (v )
555- stopColor .R = uint8 (float64 (stopColor .R ) / float64 (stopColor .A ) * stopOpacity * 255.0 )
556- stopColor .G = uint8 (float64 (stopColor .G ) / float64 (stopColor .A ) * stopOpacity * 255.0 )
557- stopColor .B = uint8 (float64 (stopColor .B ) / float64 (stopColor .A ) * stopOpacity * 255.0 )
558- stopColor .A = uint8 (stopOpacity * 255.0 )
552+ stopColor = ToOpacity (stopColor , stopOpacity )
559553 }
560554 grad .Add (offset , stopColor )
561555 }
@@ -898,15 +892,77 @@ func (svg *svgParser) setAttribute(key, val string) {
898892 // Set a temporary fill so that DrawPath doesn't early return
899893 svg .ctx .SetFill (Paint {Color : Black })
900894 } else {
895+ alpha := float64 (svg .ctx .Style .Fill .Color .A ) / 255.0
901896 svg .ctx .SetFill (svg .parsePaint (val ))
897+ if alpha != 1.0 {
898+ svg .ctx .Style .Fill .Color = ToOpacity (svg .ctx .Style .Fill .Color , alpha )
899+ if svg .ctx .Style .Fill .IsGradient () {
900+ switch grad := svg .ctx .Style .Fill .Gradient .(type ) {
901+ case * LinearGradient :
902+ for i := range grad .Grad {
903+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
904+ }
905+ case * RadialGradient :
906+ for i := range grad .Grad {
907+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
908+ }
909+ }
910+ }
911+ }
912+ }
913+ case "fill-opacity" :
914+ alpha := svg .parseNumber (val )
915+ svg .ctx .Style .Fill .Color = ToOpacity (svg .ctx .Style .Fill .Color , alpha )
916+ if svg .ctx .Style .Fill .IsGradient () {
917+ switch grad := svg .ctx .Style .Stroke .Gradient .(type ) {
918+ case * LinearGradient :
919+ for i := range grad .Grad {
920+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
921+ }
922+ case * RadialGradient :
923+ for i := range grad .Grad {
924+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
925+ }
926+ }
902927 }
903928 case "stroke" :
904929 if id := svg .parseUrlID (val ); id != "" {
905930 svg .activeDefs ["stroke" ] = svg .defs [id ]
906931 // Set a temporary stroke so that DrawPath doesn't early return
907932 svg .ctx .SetStroke (Paint {Color : Black })
908933 } else {
934+ alpha := float64 (svg .ctx .Style .Stroke .Color .A ) / 255.0
909935 svg .ctx .SetStroke (svg .parsePaint (val ))
936+ if alpha != 1.0 {
937+ svg .ctx .Style .Stroke .Color = ToOpacity (svg .ctx .Style .Stroke .Color , alpha )
938+ if svg .ctx .Style .Stroke .IsGradient () {
939+ switch grad := svg .ctx .Style .Stroke .Gradient .(type ) {
940+ case * LinearGradient :
941+ for i := range grad .Grad {
942+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
943+ }
944+ case * RadialGradient :
945+ for i := range grad .Grad {
946+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
947+ }
948+ }
949+ }
950+ }
951+ }
952+ case "stroke-opacity" :
953+ alpha := svg .parseNumber (val )
954+ svg .ctx .Style .Stroke .Color = ToOpacity (svg .ctx .Style .Stroke .Color , alpha )
955+ if svg .ctx .Style .Stroke .IsGradient () {
956+ switch grad := svg .ctx .Style .Stroke .Gradient .(type ) {
957+ case * LinearGradient :
958+ for i := range grad .Grad {
959+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
960+ }
961+ case * RadialGradient :
962+ for i := range grad .Grad {
963+ grad .Grad [i ].Color = ToOpacity (grad .Grad [i ].Color , alpha )
964+ }
965+ }
910966 }
911967 case "stroke-width" :
912968 svg .ctx .SetStrokeWidth (svg .parseDimension (val , svg .diagonal ))
0 commit comments