@@ -5,8 +5,9 @@ import android.graphics.Matrix
55import android.graphics.Path
66import android.graphics.RectF
77import com.opensource.svgaplayer.proto.ShapeEntity
8+ import org.json.JSONArray
89import org.json.JSONObject
9- import java.util.HashMap
10+ import java.util.*
1011
1112/* *
1213 * Created by cuiminghui on 2017/2/22.
@@ -139,12 +140,24 @@ internal class SVGAVideoShapeEntity {
139140 val styles = Styles ()
140141 it.optJSONArray(" fill" )?.let {
141142 if (it.length() == 4 ) {
142- styles.fill = Color .argb((it.optDouble(3 ) * 255 ).toInt(), (it.optDouble(0 ) * 255 ).toInt(), (it.optDouble(1 ) * 255 ).toInt(), (it.optDouble(2 ) * 255 ).toInt())
143+ val mulValue = checkValueRange(it)
144+ styles.fill = Color .argb(
145+ (it.optDouble(3 ) * mulValue).toInt(),
146+ (it.optDouble(0 ) * mulValue).toInt(),
147+ (it.optDouble(1 ) * mulValue).toInt(),
148+ (it.optDouble(2 ) * mulValue).toInt()
149+ )
143150 }
144151 }
145152 it.optJSONArray(" stroke" )?.let {
146153 if (it.length() == 4 ) {
147- styles.stroke = Color .argb((it.optDouble(3 ) * 255 ).toInt(), (it.optDouble(0 ) * 255 ).toInt(), (it.optDouble(1 ) * 255 ).toInt(), (it.optDouble(2 ) * 255 ).toInt())
154+ val mulValue = checkValueRange(it)
155+ styles.stroke = Color .argb(
156+ (it.optDouble(3 ) * mulValue).toInt(),
157+ (it.optDouble(0 ) * mulValue).toInt(),
158+ (it.optDouble(1 ) * mulValue).toInt(),
159+ (it.optDouble(2 ) * mulValue).toInt()
160+ )
148161 }
149162 }
150163 styles.strokeWidth = it.optDouble(" strokeWidth" , 0.0 ).toFloat()
@@ -161,14 +174,41 @@ internal class SVGAVideoShapeEntity {
161174 }
162175 }
163176
177+ // 检查色域范围是否是 0-1
178+ private fun checkValueRange (obj : JSONArray ): Float {
179+ return if (
180+ obj.optDouble(3 ) <= 1 &&
181+ obj.optDouble(0 ) <= 1 &&
182+ obj.optDouble(1 ) <= 1 &&
183+ obj.optDouble(2 ) <= 1
184+ ) {
185+ 255f
186+ } else {
187+ 1f
188+ }
189+ }
190+
164191 private fun parseStyles (obj : ShapeEntity ) {
165192 obj.styles?.let {
166193 val styles = Styles ()
167194 it.fill?.let {
168- styles.fill = Color .argb(((it.a ? : 0.0f ) * 255 ).toInt(), ((it.r ? : 0.0f ) * 255 ).toInt(), ((it.g ? : 0.0f ) * 255 ).toInt(), ((it.b ? : 0.0f ) * 255 ).toInt())
195+ val mulValue = checkValueRange(it)
196+ styles.fill = Color .argb(
197+ ((it.a ? : 0f ) * mulValue).toInt(),
198+ ((it.r ? : 0f ) * mulValue).toInt(),
199+ ((it.g ? : 0f ) * mulValue).toInt(),
200+ ((it.b ? : 0f ) * mulValue).toInt()
201+ )
169202 }
170203 it.stroke?.let {
171- styles.stroke = Color .argb(((it.a ? : 0.0f ) * 255 ).toInt(), ((it.r ? : 0.0f ) * 255 ).toInt(), ((it.g ? : 0.0f ) * 255 ).toInt(), ((it.b ? : 0.0f ) * 255 ).toInt())
204+ val mulValue = checkValueRange(it)
205+ styles.stroke = Color .argb(
206+ ((it.a ? : 0f ) * mulValue).toInt(),
207+ ((it.r ? : 0f ) * mulValue).toInt(),
208+ ((it.g ? : 0f ) * mulValue).toInt(),
209+ ((it.b ? : 0f ) * mulValue).toInt()
210+ )
211+
172212 }
173213 styles.strokeWidth = it.strokeWidth ? : 0.0f
174214 it.lineCap?.let {
@@ -194,6 +234,20 @@ internal class SVGAVideoShapeEntity {
194234 }
195235 }
196236
237+ // 检查色域范围是否是 0-1
238+ private fun checkValueRange (color : ShapeEntity .ShapeStyle .RGBAColor ): Float {
239+ return if (
240+ (color.a ? : 0f ) <= 1 &&
241+ (color.r ? : 0f ) <= 1 &&
242+ (color.g ? : 0f ) <= 1 &&
243+ (color.b ? : 0f ) <= 1
244+ ) {
245+ 255f
246+ } else {
247+ 1f
248+ }
249+ }
250+
197251 private fun parseTransform (obj : JSONObject ) {
198252 obj.optJSONObject(" transform" )?.let {
199253 val transform = Matrix ()
@@ -252,8 +306,7 @@ internal class SVGAVideoShapeEntity {
252306 (this .args?.get(" d" ) as ? String )?.let {
253307 SVGAPathEntity (it).buildPath(sharedPath)
254308 }
255- }
256- else if (this .type == Type .ellipse) {
309+ } else if (this .type == Type .ellipse) {
257310 val xv = this .args?.get(" x" ) as ? Number ? : return
258311 val yv = this .args?.get(" y" ) as ? Number ? : return
259312 val rxv = this .args?.get(" radiusX" ) as ? Number ? : return
@@ -263,8 +316,7 @@ internal class SVGAVideoShapeEntity {
263316 val rx = rxv.toFloat()
264317 val ry = ryv.toFloat()
265318 sharedPath.addOval(RectF (x - rx, y - ry, x + rx, y + ry), Path .Direction .CW )
266- }
267- else if (this .type == Type .rect) {
319+ } else if (this .type == Type .rect) {
268320 val xv = this .args?.get(" x" ) as ? Number ? : return
269321 val yv = this .args?.get(" y" ) as ? Number ? : return
270322 val wv = this .args?.get(" width" ) as ? Number ? : return
0 commit comments