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 Go

1 Answer

0 votes
package main

import (
    "fmt"
    "image/color"
)

// AverageRGB calculates the average of two RGB colors.
func AverageRGB(c1, c2 color.RGBA) color.RGBA {
    return color.RGBA{
        R: uint8((int(c1.R) + int(c2.R)) / 2),
        G: uint8((int(c1.G) + int(c2.G)) / 2),
        B: uint8((int(c1.B) + int(c2.B)) / 2),
        A: uint8((int(c1.A) + int(c2.A)) / 2),
    }
}

// RGBToHex converts RGB to a hex string.
func RGBToHex(c color.RGBA) string {
    return fmt.Sprintf("#%02X%02X%02X", c.R, c.G, c.B)
}

func main() {
    color1 := color.RGBA{R: 255, G: 100, B: 50, A: 255}
    color2 := color.RGBA{R: 50, G: 170, B: 200, A: 255}

    averageColor := AverageRGB(color1, color2)

    fmt.Printf("Average Color: R=%d, G=%d, B=%d, A=%d\n",
        averageColor.R, averageColor.G, averageColor.B, averageColor.A)

    hex := RGBToHex(averageColor)
    fmt.Printf("Average Color (hex): %s\n", hex)
}



/*
run:

Average Color: R=152, G=135, B=125, A=255
Average Color (hex): #98877D

*/

 



answered Jun 18, 2025 by avibootz
...