Logo

Programming-Idioms

History of Idiom 154 > diff from v16 to v17

Edit summary for version 17 by steenslag:
[Ruby] shaved off a line

Version 16

2016-11-12, 23:01:20

Version 17

2016-11-12, 23:25:18

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 radix base
Extra Keywords
hexa hexadecimal css avg mean radix base
Code
rgbs = c1[1..-1].scan(/../), c2[1..-1].scan(/../)
middle_hex_vals = rgbs.transpose.map{|h1, h2| "%02X" % ((h1.hex + h2.hex)/2)}
c = "#" + middle_hex_vals.join
Code
rgbs = c1[1..-1].scan(/../), c2[1..-1].scan(/../)
c = "#%02X%02X%02X" % rgbs.transpose.map{|h1, h2| ((h1.hex + h2.hex)/2)}