Logo

Programming-Idioms

History of Idiom 154 > diff from v23 to v24

Edit summary for version 24 by programming-idioms.org:
[D] Homogeneous sample values in demo links

Version 23

2017-07-27, 23:41:25

Version 24

2017-08-22, 12:13:10

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;
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))("#");
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
Demo URL
https://ideone.com/RJ2fLd