-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(android): Add WebColor utility for parsing color (#3947)
- Loading branch information
1 parent
b2816d8
commit 3746404
Showing
2 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.getcapacitor.util; | ||
|
||
import android.graphics.Color; | ||
|
||
public class WebColor { | ||
|
||
/** | ||
* Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. | ||
* @param colorString The hexadecimal color string. The format is an RGB or RGBA hex string. | ||
* @return The corresponding color as an int. | ||
*/ | ||
public static int parseColor(String colorString) { | ||
String formattedColor = colorString; | ||
if (colorString.charAt(0) != '#') { | ||
formattedColor = "#" + formattedColor; | ||
} | ||
|
||
if (formattedColor.length() != 7 && formattedColor.length() != 9) { | ||
throw new IllegalArgumentException("The encoded color space is invalid or unknown"); | ||
} else if (formattedColor.length() == 7) { | ||
return Color.parseColor(formattedColor); | ||
} else { | ||
// Convert to Android format #AARRGGBB from #RRGGBBAA | ||
formattedColor = "#" + formattedColor.substring(7) + formattedColor.substring(1, 7); | ||
return Color.parseColor(formattedColor); | ||
} | ||
} | ||
} |