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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to find the average between RGB colors c1 and c2 in Kotlin

1 Answer

0 votes
data class RGBA(val r: Int, val g: Int, val b: Int, val a: Int)

fun averageColor(c1: RGBA, c2: RGBA): RGBA {
    return RGBA(
        (c1.r + c2.r) / 2,
        (c1.g + c2.g) / 2,
        (c1.b + c2.b) / 2,
        (c1.a + c2.a) / 2
    )
}

fun rgbToHex(r: Int, g: Int, b: Int): String {
    return String.format("#%02X%02X%02X", r, g, b)
}

fun main() {
    val color1 = RGBA(255, 100, 50, 255)
    val color2 = RGBA(50, 170, 200, 255)

    val avg = averageColor(color1, color2)
    val hex = rgbToHex(avg.r, avg.g, avg.b)

    println("Average Color: R=${avg.r}, G=${avg.g}, B=${avg.b}, A=${avg.a}")
    println("Average Color (hex): $hex")
}
 
 
  
/*
run:
  
Average Color: R=152, G=135, B=125, A=255
Average Color (hex): #98877D

*/

 



answered Jun 18, 2025 by avibootz
...