Skip to content

Instantly share code, notes, and snippets.

@andrew
Created March 6, 2009 16:49
Show Gist options
  • Save andrew/74970 to your computer and use it in GitHub Desktop.
Save andrew/74970 to your computer and use it in GitHub Desktop.
# takes a hex value of a colour (0xffffff) and returns either black or white, which ever is easier to see
def black_or_white(hex)
rgb = {}
%w(r g b).inject(hex) {|a,i| rest, rgb[i] = a.divmod 256; rest}
r = rgb['r']/255
g = rgb['g']/255
b = rgb['b']/255
[r, g, b].max > 0.55 ? "#000000" : "#FFFFFF"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment