Logo

Programming-Idioms

History of Idiom 154 > diff from v12 to v13

Edit summary for version 13 by programming-idioms.org:
[Ruby] Buggy impl

Version 12

2016-11-12, 17:49:28

Version 13

2016-11-12, 21:07:54

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| ((h1.hex + h2.hex)/2).to_s(16)}
c =  "#{middle_hex_vals.join}"
Code
rgbs = c1[1..-1].scan(/../), c2[1..-1].scan(/../)
middle_hex_vals = rgbs .transpose.map{|h1, h2| ((h1.hex + h2.hex)/2).to_s(16)}
c =  "#{middle_hex_vals.join}"
Comments bubble
BUG this doesn't correctly pad zeroes to ensure each value has exactly 2 hex digits.