Skip to content

Instantly share code, notes, and snippets.

@gorrotowi
Last active April 20, 2019 19:08
Show Gist options
  • Save gorrotowi/99a372b85010bf8e15dab14b4e3448d3 to your computer and use it in GitHub Desktop.
Save gorrotowi/99a372b85010bf8e15dab14b4e3448d3 to your computer and use it in GitHub Desktop.
Color hex kotlin function
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
when (resultCode) {
Activity.RESULT_OK -> {
val place = data?.let { Autocomplete.getPlaceFromIntent(it) }
Log.e("PlaceResult", "${place?.toString()}")
}
Activity.RESULT_CANCELED -> {
Log.e("PlaceCanceled", "No Place selected")
}
AutocompleteActivity.RESULT_ERROR -> {
val statusPlace = data?.let { Autocomplete.getStatusFromIntent(it) }
Log.e("AutocompleateError", "${statusPlace?.statusMessage}")
}
}
}
}
fun getColorIntWithAlpha(hexColor: String, alpha: Float): Int =
Color.parseColor(hexColor.alphaColor(alpha))
fun String.alphaColor(percentage: Float = 0f): String = if (percentage < 0 || percentage > 1) {
fail("The percentage need to be a float number between 0.0 and 1.0")
} else {
val alphInt = ((percentage * 255) + 0.000001).roundToInt()
val alphaHex = String.format("%02X", alphInt)
this.replace("#", "#$alphaHex")
}
fun fail(message: String): Nothing {
throw IllegalArgumentException(message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment