Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to generate a random RGBA color and opacity in Kotlin

1 Answer

0 votes
import kotlin.random.Random

/*
    Generate a random RGBA color string.
    Produces full‑range RGB values and a floating‑point opacity.
*/

// Generate a random integer in the range 0..255
fun randomChannel(): Int =
    Random.nextInt(0, 256)

// Generate a random opacity in the range 0.0..1.0
fun randomOpacity(): Double =
    Random.nextDouble()   // full floating‑point precision

// Build a full random RGBA color string
fun randomRgbaColor(): String {
    val r = randomChannel()
    val g = randomChannel()
    val b = randomChannel()
    val a = randomOpacity()

    // Format as rgba(r, g, b, a) with two decimal places
    return "rgba($r, $g, $b, ${"%.2f".format(a)})"
}

fun main() {
    val color = randomRgbaColor()
    println(color)
}


/*
run:

rgba(201, 145, 45, 0.29)

*/

 



answered Aug 25 by avibootz
...