Logo

Programming-Idioms

History of Idiom 154 > diff from v22 to v23

Edit summary for version 23 by Bzzzzzzz:
New D implementation by user [Bzzzzzzz]

Version 22

2016-11-27, 23:00:18

Version 23

2017-07-27, 23:41:25

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 # .
Assume linear computations, ignore gamma corrections.

Illustration

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 # .
Assume linear computations, ignore gamma corrections.

Illustration
Extra Keywords
hexa hexadecimal css avg mean radix base
Extra Keywords
hexa hexadecimal css avg mean radix base
Imports
import std.algorithm, std.range; 
import std.conv, std.array, std.format;
Code
string c = roundRobin(c1.dropOne.chunks(2), c2.dropOne.chunks(2))
    .map!(a => a.to!int(16)).array
    .chunks(2)
    .map!(a => ((a[0] + a[1]) / 2))
    .fold!((a,b) => a ~= "%.2X".format(b))("#");
Demo URL
http://ideone.com/X53I2O