Skip to content

Commit 69201de

Browse files
committed
feat rgb support
1 parent 5901d02 commit 69201de

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

android/src/main/java/com/stroketext/StrokeTextView.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.stroketext;
22

33
import android.graphics.Canvas;
4+
import android.graphics.Color;
45
import android.graphics.Paint;
56
import android.graphics.Typeface;
67
import android.text.Layout;
@@ -133,17 +134,17 @@ public void setFontSize(float fontSize) {
133134
}
134135

135136
public void setTextColor(String color) {
136-
int parsedColor = android.graphics.Color.parseColor(color);
137+
int parsedColor = parseColor(color);
137138
if (this.textColor != parsedColor) {
138-
this.textColor = android.graphics.Color.parseColor(color);
139+
this.textColor = parsedColor;
139140
invalidate();
140141
}
141142
}
142143

143144
public void setStrokeColor(String color) {
144-
int parsedColor = android.graphics.Color.parseColor(color);
145+
int parsedColor = parseColor(color);
145146
if (this.strokeColor != parsedColor) {
146-
this.strokeColor = android.graphics.Color.parseColor(color);
147+
this.strokeColor = parsedColor;
147148
invalidate();
148149
}
149150
}
@@ -202,4 +203,23 @@ public void setCustomWidth(float width) {
202203
invalidate();
203204
}
204205
}
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+
}
205225
}

0 commit comments

Comments
 (0)