Logo

Programming-Idioms

History of Idiom 154 > diff from v5 to v6

Edit summary for version 6 by programming-idioms.org:
[Java] Missing code at the end (too long)

Version 5

2016-10-30, 21:37:24

Version 6

2016-10-30, 21:44:51

Idiom #154 Halfway between two hex color codes

Find color c, the average between colors c1, c2.

c, c1, c2 are strings of hex color codes: 7 chars, beginning with a number sign # .

Idiom #154 Halfway between two hex color codes

Find color c, the average between colors c1, c2.

c, c1, c2 are strings of hex color codes: 7 chars, beginning with a number sign # .

Extra Keywords
hexa hexadecimal css avg mean
Extra Keywords
hexa hexadecimal css avg mean
Code
String r1 = c1.substring(1,3);
String g1 = c1.substring(3,5);
String b1 = c1.substring(5,7);

String r2 = c2.substring(1,3);
String g2 = c2.substring(3,5);
String b2 = c2.substring(5,7);

String r = String.format("%02X", (Integer.parseInt(r1, 16) + Integer.parseInt(r2, 16))/2 );
String g = String.format("%02X", (Integer.parseInt(g1, 16) + Integer.parseInt(g2, 16))/2 );
String b = String.format("%02X", (Integer.parseInt(b1, 16) + Integer.parseInt(b2, 16))/2 );

String c = "#" + r + g 
Code
String r1 = c1.substring(1,3);
String g1 = c1.substring(3,5);
String b1 = c1.substring(5,7);

String r2 = c2.substring(1,3);
String g2 = c2.substring(3,5);
String b2 = c2.substring(5,7);

String r = String.format("%02X", (Integer.parseInt(r1, 16)+Integer.parseInt(r2, 16))/2 );
String g = String.format("%02X", (Integer.parseInt(g1, 16)+Integer.parseInt(g2, 16))/2 );
String b = String.format("%02X", (Integer.parseInt(b1, 16)+Integer.parseInt(b2, 16))/2 );

String c = "#" + r + g + b;