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 Rust

1 Answer

0 votes
fn average_rgb(c1: (u8, u8, u8, u8), c2: (u8, u8, u8, u8)) -> (u8, u8, u8, u8) {
    let avg_r = (c1.0 as u16 + c2.0 as u16) / 2;
    let avg_g = (c1.1 as u16 + c2.1 as u16) / 2;
    let avg_b = (c1.2 as u16 + c2.2 as u16) / 2;
    let avg_a = (c1.3 as u16 + c2.3 as u16) / 2;
    (avg_r as u8, avg_g as u8, avg_b as u8, avg_a as u8)
}

fn rgb_to_hex(r: u8, g: u8, b: u8) -> String {
    format!("#{:02X}{:02X}{:02X}", r, g, b)
}

fn main() {
    let color1 = (255, 100, 50, 255); // RGBA
    let color2 = (50, 170, 200, 255);

    let (r, g, b, a) = average_rgb(color1, color2);

    println!("Average Color: R={}, G={}, B={}, A={}", r, g, b, a);
    println!("Average Color (hex): {}", rgb_to_hex(r, g, b));
}

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

 



answered Jun 18, 2025 by avibootz
...