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,955 questions

51,897 answers

573 users

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

1 Answer

0 votes
object AverageColorHex {
  case class RGBA(r: Int, g: Int, b: Int, a: Int)

  def averageRGB(c1: RGBA, c2: RGBA): RGBA = {
    RGBA(
      (c1.r + c2.r) / 2,
      (c1.g + c2.g) / 2,
      (c1.b + c2.b) / 2,
      (c1.a + c2.a) / 2
    )
  }

  def rgbToHex(r: Int, g: Int, b: Int): String =
    f"#$r%02X$g%02X$b%02X"

  def main(args: Array[String]): Unit = {
    val color1 = RGBA(255, 100, 50, 255)
    val color2 = RGBA(50, 170, 200, 255)

    val average = averageRGB(color1, color2)
    val hex = rgbToHex(average.r, average.g, average.b)

    println(s"Average Color: R=${average.r}, G=${average.g}, B=${average.b}, A=${average.a}")
    println(s"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
...