|
1 | 1 | package com.stroketext; |
2 | 2 |
|
3 | 3 | import android.graphics.Canvas; |
| 4 | +import android.graphics.Color; |
4 | 5 | import android.graphics.Paint; |
5 | 6 | import android.graphics.Typeface; |
6 | 7 | import android.text.Layout; |
@@ -133,17 +134,17 @@ public void setFontSize(float fontSize) { |
133 | 134 | } |
134 | 135 |
|
135 | 136 | public void setTextColor(String color) { |
136 | | - int parsedColor = android.graphics.Color.parseColor(color); |
| 137 | + int parsedColor = parseColor(color); |
137 | 138 | if (this.textColor != parsedColor) { |
138 | | - this.textColor = android.graphics.Color.parseColor(color); |
| 139 | + this.textColor = parsedColor; |
139 | 140 | invalidate(); |
140 | 141 | } |
141 | 142 | } |
142 | 143 |
|
143 | 144 | public void setStrokeColor(String color) { |
144 | | - int parsedColor = android.graphics.Color.parseColor(color); |
| 145 | + int parsedColor = parseColor(color); |
145 | 146 | if (this.strokeColor != parsedColor) { |
146 | | - this.strokeColor = android.graphics.Color.parseColor(color); |
| 147 | + this.strokeColor = parsedColor; |
147 | 148 | invalidate(); |
148 | 149 | } |
149 | 150 | } |
@@ -202,4 +203,23 @@ public void setCustomWidth(float width) { |
202 | 203 | invalidate(); |
203 | 204 | } |
204 | 205 | } |
| 206 | + |
| 207 | + private int parseColor(String color) { |
| 208 | + if (color.startsWith("#")) { |
| 209 | + return Color.parseColor(color); |
| 210 | + } else if (color.startsWith("rgb")) { |
| 211 | + return parseRgbColor(color); |
| 212 | + } |
| 213 | + |
| 214 | + return 0xFF000000; |
| 215 | + } |
| 216 | + |
| 217 | + private int parseRgbColor(String color) { |
| 218 | + String[] parts = color.replaceAll("[rgba()\\s]", "").split(","); |
| 219 | + int r = Integer.parseInt(parts[0]); |
| 220 | + int g = Integer.parseInt(parts[1]); |
| 221 | + int b = Integer.parseInt(parts[2]); |
| 222 | + int a = parts.length > 3 ? (int) (Float.parseFloat(parts[3]) * 255) : 255; |
| 223 | + return Color.argb(a, r, g, b); |
| 224 | + } |
205 | 225 | } |
0 commit comments