Splitties icon indicating copy to clipboard operation
Splitties copied to clipboard

ColorInt extensions

Open Ribesg opened this issue 6 years ago • 2 comments

Would be nice to get some things similar to these and/or these :)

Ribesg avatar Feb 27 '19 12:02 Ribesg

Don't you think this would create overlap with AndroidX Core KTX component# functions used for destructuring?

LouisCAD avatar Feb 27 '19 14:02 LouisCAD

Right, such extensions should really add value. My only example right now is setting the alpha of a color int.

inline fun Int.alpha(alpha: Double): Int {
    require(alpha in 0.0..1.0)
    return ColorUtils.setAlphaComponent(this, (alpha * 0xFF).toInt())
}

ColorUtils and the Color class are overall pretty verbose, in some cases you can write pretty long code for nothing much. The above extension allows me to replace this:

ColorUtils.setAlphaComponent(color(R.color.red), (.25 * 0xFF).toInt())

with this:

color(R.color.red).alpha(.25)

Obviously KTX provides everything needed for getters, we have Int.alpha, etc

Ribesg avatar Feb 27 '19 14:02 Ribesg