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 TypeScript

2 Answers

0 votes
function rgbToHex(r: number, g: number, b: number): string {
    return `#${r.toString(16).padStart(2, '0').toUpperCase()}${g.toString(16).padStart(2, '0').toUpperCase()}${b.toString(16).padStart(2, '0').toUpperCase()}`;
}

// Color 1: RGB(255, 100, 50)
const r1: number = 255, g1: number = 100, b1: number = 50;

// Color 2: RGB(50, 170, 200)
const r2: number = 50, g2: number = 170, b2: number = 200;

// Calculate average RGB values
const avgR: number = Math.floor((r1 + r2) / 2);
const avgG: number = Math.floor((g1 + g2) / 2);
const avgB: number = Math.floor((b1 + b2) / 2);

// Get hex representation
const averageColor: string = rgbToHex(avgR, avgG, avgB);
console.log(`Average Color (hex): ${averageColor}`);


 
     
/*
run:
     
"Average Color (hex): #98877D" 
     
*/

 



answered Jun 18, 2025 by avibootz
0 votes
const c1: string = "#FF6432"; // RGB(255, 100, 50)
const c2: string = "#32AAC8"; // RGB(50, 170, 200)

let hexColor: string = "#";

for (let i: number = 0; i < 3; i++) {
    const sub1: string = c1.substring(1 + 2 * i, 3 + 2 * i);
    const sub2: string = c2.substring(1 + 2 * i, 3 + 2 * i);

    const i1: number = parseInt(sub1, 16);
    const i2: number = parseInt(sub2, 16);
    const avg: number = Math.floor((i1 + i2) / 2);

    const hex: string = avg.toString(16).toUpperCase();
    const padHex: string = hex.padStart(2, '0');

    hexColor += padHex;
}

console.log(`Average Color (hex): ${hexColor}`);

 
     
/*
run:
     
"Average Color (hex): #98877D" 
     
*/

 



answered Jun 18, 2025 by avibootz
...