Reputation: 313
Very simple question, i hope theres a simple answer. I am creating a mobile app that receives css styles from a website and translates the colour into the valid view.setBackgroundColor()
.
So for example once i have extracted a substring from the style that sais "background-color:red;"
, How would i convert that colour "red"
into the correct rgb value which i believe would be view.setBackgroundColor(Color.rgb(255,0,0))
or the correct hexadecimal value which i believe would be view.setBackgroundColor(Color.parseColor("#ff0000"))
according to the W3schools color picker?
Upvotes: 0
Views: 430
Reputation: 313
I gave georkost's answer an upvote because its one way of doing it. However in the end i added the css colors and their translated color hashes to my colors.xml file using the xml copied from This link and then i used the following code.
val cssColor = context.resources.getIdentifier(namedColor.toLowerCase(
Locale.ENGLISH), "color", context.packageName)
if (cssColor != 0) {
// outputting this if condition is met
view.setTextColor( ContextCompat.getColor(context, cssColor) )
}
Upvotes: 0
Reputation: 618
Just create an enum from the values you get. You could create an enum class that will translate the values you get from site to actual hex or rgb based values
when(serverValue) {
"red" -> "#FFFFFF"
"green" -> "#FFFABA"
}
The snippet is just to give you an idea
Upvotes: 1