Skip to content

Instantly share code, notes, and snippets.

@Cosmo
Created June 4, 2012 13:37
Show Gist options
  • Save Cosmo/2868435 to your computer and use it in GitHub Desktop.
Save Cosmo/2868435 to your computer and use it in GitHub Desktop.
def color_with(value=nil, options={})
if options.is_a?(Numeric)
alpha = options.to_f
else
alpha = options.delete(:alpha) || 1
end
if value
if (value.is_a?(String) && value[0] == "#")
value = value.gsub("#", "").to_i(16)
end
if value.is_a?(Fixnum)
red = ((value & 0xFF0000) >> 16) / 255.0
green = ((value & 0xFF00) >> 8) / 255.0
blue = (value & 0xFF) / 255.0
UIColor.colorWithRed(red, green: green, blue: blue, alpha: alpha)
end
else
UIColor.clearColor
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment