Skip to content

Instantly share code, notes, and snippets.

@studio3104
Last active August 29, 2015 14:19
Show Gist options
  • Save studio3104/524030aeb510b24addda to your computer and use it in GitHub Desktop.
Save studio3104/524030aeb510b24addda to your computer and use it in GitHub Desktop.
n種類のカラーコードのちょうど平均のカラーコードを出す
#!/usr/bin/env ruby
def colorcodeaverage(*codes)
sum = [ 0, 0, 0 ]
codes.each do |code|
c = code.scan(/.{1,2}/)
sum = sum.map.with_index { |s,i| s + c[i].to_i(16) }
end
sum.map { |s| ( s / codes.size ).to_s(16).rjust(2,'0') }.join.upcase
end
p colorcodeaverage('cc11cc', '221111', '000000')
#=> "4F0B49"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment